test_sensehat.py 2.2 KB

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