test_sensehat.py 3.3 KB

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