test_sensehat.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt testing libs
  5. from tests.support.unit import TestCase
  6. from tests.support.mock import MagicMock
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. # Salt libs
  9. import salt.beacons.sensehat as sensehat
  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__': {'sensehat.get_humidity': self.HUMIDITY_MOCK,
  25. 'sensehat.get_temperature': self.TEMPERATURE_MOCK,
  26. 'sensehat.get_pressure': self.PRESSURE_MOCK
  27. },
  28. }
  29. }
  30. def test_non_list_config(self):
  31. config = {}
  32. ret = sensehat.validate(config)
  33. self.assertEqual(ret, (False, 'Configuration for sensehat beacon'
  34. ' must be a list.'))
  35. def test_empty_config(self):
  36. config = [{}]
  37. ret = sensehat.validate(config)
  38. self.assertEqual(ret, (False, 'Configuration for sensehat '
  39. 'beacon requires sensors.'))
  40. def test_sensehat_humidity_match(self):
  41. config = [{'sensors': {'humidity': '70%'}}]
  42. ret = sensehat.validate(config)
  43. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  44. ret = sensehat.beacon(config)
  45. self.assertEqual(ret, [{'tag': 'sensehat/humidity',
  46. 'humidity': 80}])
  47. def test_sensehat_temperature_match(self):
  48. config = [{'sensors': {'temperature': 20}}]
  49. ret = sensehat.validate(config)
  50. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  51. ret = sensehat.beacon(config)
  52. self.assertEqual(ret, [{'tag': 'sensehat/temperature',
  53. 'temperature': 30}])
  54. def test_sensehat_temperature_match_range(self):
  55. config = [{'sensors': {'temperature': [20, 29]}}]
  56. ret = sensehat.validate(config)
  57. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  58. ret = sensehat.beacon(config)
  59. self.assertEqual(ret, [{'tag': 'sensehat/temperature',
  60. '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',
  67. 'pressure': 1500}])
  68. def test_sensehat_no_match(self):
  69. config = [{'sensors': {'pressure': '1600'}}]
  70. ret = sensehat.validate(config)
  71. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  72. ret = sensehat.beacon(config)
  73. self.assertEqual(ret, [])