test_sensehat.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt libs
  5. import salt.beacons.sensehat as sensehat
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.mock import MagicMock
  8. # Salt testing libs
  9. from tests.support.unit import TestCase
  10. class SensehatBeaconTestCase(TestCase, LoaderModuleMockMixin):
  11. """
  12. Test case for salt.beacons.[s]
  13. """
  14. def setup_loader_modules(self):
  15. self.HUMIDITY_MOCK = MagicMock(return_value=80)
  16. self.TEMPERATURE_MOCK = MagicMock(return_value=30)
  17. self.PRESSURE_MOCK = MagicMock(return_value=1500)
  18. self.addCleanup(delattr, self, "HUMIDITY_MOCK")
  19. self.addCleanup(delattr, self, "TEMPERATURE_MOCK")
  20. self.addCleanup(delattr, self, "PRESSURE_MOCK")
  21. return {
  22. sensehat: {
  23. "__context__": {},
  24. "__salt__": {
  25. "sensehat.get_humidity": self.HUMIDITY_MOCK,
  26. "sensehat.get_temperature": self.TEMPERATURE_MOCK,
  27. "sensehat.get_pressure": self.PRESSURE_MOCK,
  28. },
  29. }
  30. }
  31. def test_non_list_config(self):
  32. config = {}
  33. ret = sensehat.validate(config)
  34. self.assertEqual(
  35. ret, (False, "Configuration for sensehat beacon must be a list.")
  36. )
  37. def test_empty_config(self):
  38. config = [{}]
  39. ret = sensehat.validate(config)
  40. self.assertEqual(
  41. ret, (False, "Configuration for sensehat beacon requires sensors.")
  42. )
  43. def test_sensehat_humidity_match(self):
  44. config = [{"sensors": {"humidity": "70%"}}]
  45. ret = sensehat.validate(config)
  46. self.assertEqual(ret, (True, "Valid beacon configuration"))
  47. ret = sensehat.beacon(config)
  48. self.assertEqual(ret, [{"tag": "sensehat/humidity", "humidity": 80}])
  49. def test_sensehat_temperature_match(self):
  50. config = [{"sensors": {"temperature": 20}}]
  51. ret = sensehat.validate(config)
  52. self.assertEqual(ret, (True, "Valid beacon configuration"))
  53. ret = sensehat.beacon(config)
  54. self.assertEqual(ret, [{"tag": "sensehat/temperature", "temperature": 30}])
  55. def test_sensehat_temperature_match_range(self):
  56. config = [{"sensors": {"temperature": [20, 29]}}]
  57. ret = sensehat.validate(config)
  58. self.assertEqual(ret, (True, "Valid beacon configuration"))
  59. ret = sensehat.beacon(config)
  60. self.assertEqual(ret, [{"tag": "sensehat/temperature", "temperature": 30}])
  61. def test_sensehat_pressure_match(self):
  62. config = [{"sensors": {"pressure": "1400"}}]
  63. ret = sensehat.validate(config)
  64. self.assertEqual(ret, (True, "Valid beacon configuration"))
  65. ret = sensehat.beacon(config)
  66. self.assertEqual(ret, [{"tag": "sensehat/pressure", "pressure": 1500}])
  67. def test_sensehat_no_match(self):
  68. config = [{"sensors": {"pressure": "1600"}}]
  69. ret = sensehat.validate(config)
  70. self.assertEqual(ret, (True, "Valid beacon configuration"))
  71. ret = sensehat.beacon(config)
  72. self.assertEqual(ret, [])