1
0

test_switchbot.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import logging
  2. import unittest.mock
  3. import pytest
  4. import switchbot_mqtt
  5. # pylint: disable=protected-access
  6. @pytest.mark.parametrize("mac_address", ["aa:bb:cc:dd:ee:ff"])
  7. @pytest.mark.parametrize(
  8. "action", [switchbot_mqtt._SwitchbotAction.ON, switchbot_mqtt._SwitchbotAction.OFF]
  9. )
  10. @pytest.mark.parametrize("command_successful", [True, False])
  11. def test__send_command(caplog, mac_address, action, command_successful):
  12. with unittest.mock.patch("switchbot.Switchbot") as switchbot_device_mock:
  13. switchbot_device_mock().turn_on.return_value = command_successful
  14. switchbot_device_mock().turn_off.return_value = command_successful
  15. switchbot_device_mock.reset_mock()
  16. with caplog.at_level(logging.INFO):
  17. switchbot_mqtt._send_command(mac_address, action)
  18. switchbot_device_mock.assert_called_once_with(mac=mac_address)
  19. assert len(caplog.records) == 1
  20. logger, log_level, log_message = caplog.record_tuples[0]
  21. assert logger == "switchbot_mqtt"
  22. if command_successful:
  23. assert log_level == logging.INFO
  24. else:
  25. assert log_level == logging.ERROR
  26. assert "failed" in log_message
  27. assert mac_address in log_message
  28. if action == switchbot_mqtt._SwitchbotAction.ON:
  29. switchbot_device_mock().turn_on.assert_called_once_with()
  30. assert not switchbot_device_mock().turn_off.called
  31. assert "on" in log_message
  32. else:
  33. switchbot_device_mock().turn_off.assert_called_once_with()
  34. assert not switchbot_device_mock().turn_on.called
  35. assert "off" in log_message