test_remote_control.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import unittest.mock
  2. import pytest
  3. import intertechno_cc1101
  4. # pylint: disable=protected-access
  5. @pytest.mark.parametrize("address", (-1, 2 ** 26, 21.42))
  6. def test___init___invalid_address(address):
  7. with pytest.raises(ValueError):
  8. intertechno_cc1101.RemoteControl(address=address)
  9. @pytest.mark.parametrize(
  10. ("address", "button_index", "command", "expected_message"),
  11. ((12345678, 0, 0b00, 790123392), (2 ** 26 - 1, 0b1111, 0b01, 4294967263)),
  12. )
  13. def test__send_command(address, button_index, command, expected_message):
  14. remote_control = intertechno_cc1101.RemoteControl(address=address)
  15. with unittest.mock.patch(
  16. "intertechno_cc1101._encode_message", return_value=b"dummy"
  17. ) as encode_message_mock, unittest.mock.patch(
  18. "intertechno_cc1101._cc1101_transmit"
  19. ) as transmit_mock:
  20. remote_control._send_command(button_index=button_index, command=command)
  21. encode_message_mock.assert_called_once_with(expected_message)
  22. transmit_mock.assert_called_once_with(b"dummy")
  23. @pytest.mark.parametrize("address", [12345678])
  24. @pytest.mark.parametrize("button_index", [7])
  25. def test_turn_on(address, button_index):
  26. remote_control = intertechno_cc1101.RemoteControl(address=address)
  27. with unittest.mock.patch.object(
  28. remote_control, "_send_command"
  29. ) as send_command_mock:
  30. remote_control.turn_on(button_index=button_index)
  31. send_command_mock.assert_called_once_with(button_index=button_index, command=0b01)
  32. @pytest.mark.parametrize("address", [12345678])
  33. @pytest.mark.parametrize("button_index", [7])
  34. def test_turn_off(address, button_index):
  35. remote_control = intertechno_cc1101.RemoteControl(address=address)
  36. with unittest.mock.patch.object(
  37. remote_control, "_send_command"
  38. ) as send_command_mock:
  39. remote_control.turn_off(button_index=button_index)
  40. send_command_mock.assert_called_once_with(button_index=button_index, command=0b00)