1
0

test_sensehat.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # coding: utf-8
  2. from __future__ import absolute_import
  3. import pytest
  4. import salt.beacons.sensehat as sensehat
  5. from tests.support.mock import MagicMock
  6. @pytest.fixture(autouse=True)
  7. def setup_loader(request):
  8. setup_loader_modules = {
  9. sensehat: {
  10. "__salt__": {
  11. "sensehat.get_humidity": MagicMock(return_value=80),
  12. "sensehat.get_temperature": MagicMock(return_value=30),
  13. "sensehat.get_pressure": MagicMock(return_value=1500),
  14. },
  15. }
  16. }
  17. with pytest.helpers.loader_mock(request, setup_loader_modules) as loader_mock:
  18. yield loader_mock
  19. def test_non_list_config():
  20. config = {}
  21. ret = sensehat.validate(config)
  22. assert ret == (False, "Configuration for sensehat beacon must be a list.")
  23. def test_empty_config():
  24. config = [{}]
  25. ret = sensehat.validate(config)
  26. assert ret == (False, "Configuration for sensehat beacon requires sensors.")
  27. def test_sensehat_humidity_match():
  28. config = [{"sensors": {"humidity": "70%"}}]
  29. ret = sensehat.validate(config)
  30. assert ret == (True, "Valid beacon configuration")
  31. ret = sensehat.beacon(config)
  32. assert ret == [{"tag": "sensehat/humidity", "humidity": 80}]
  33. def test_sensehat_temperature_match():
  34. config = [{"sensors": {"temperature": 20}}]
  35. ret = sensehat.validate(config)
  36. assert ret == (True, "Valid beacon configuration")
  37. ret = sensehat.beacon(config)
  38. assert ret == [{"tag": "sensehat/temperature", "temperature": 30}]
  39. def test_sensehat_temperature_match_range():
  40. config = [{"sensors": {"temperature": [20, 29]}}]
  41. ret = sensehat.validate(config)
  42. assert ret == (True, "Valid beacon configuration")
  43. ret = sensehat.beacon(config)
  44. assert ret == [{"tag": "sensehat/temperature", "temperature": 30}]
  45. def test_sensehat_pressure_match():
  46. config = [{"sensors": {"pressure": "1400"}}]
  47. ret = sensehat.validate(config)
  48. assert ret == (True, "Valid beacon configuration")
  49. ret = sensehat.beacon(config)
  50. assert ret == [{"tag": "sensehat/pressure", "pressure": 1500}]
  51. def test_sensehat_no_match():
  52. config = [{"sensors": {"pressure": "1600"}}]
  53. ret = sensehat.validate(config)
  54. assert ret == (True, "Valid beacon configuration")
  55. ret = sensehat.beacon(config)
  56. assert ret == []