test_remote_control.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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("button_index", (-1, 2 ** 6, 8.15))
  24. def test__send_command_invalid_button_index(button_index):
  25. remote_control = intertechno_cc1101.RemoteControl(address=12345678)
  26. with pytest.raises(ValueError):
  27. remote_control._send_command(button_index=button_index, command=0b01)
  28. @pytest.mark.parametrize("address", [12345678])
  29. @pytest.mark.parametrize("button_index", [7])
  30. def test_turn_on(address, button_index):
  31. remote_control = intertechno_cc1101.RemoteControl(address=address)
  32. with unittest.mock.patch.object(
  33. remote_control, "_send_command"
  34. ) as send_command_mock:
  35. remote_control.turn_on(button_index=button_index)
  36. send_command_mock.assert_called_once_with(button_index=button_index, command=0b01)
  37. @pytest.mark.parametrize("address", [12345678])
  38. @pytest.mark.parametrize("button_index", [7])
  39. def test_turn_off(address, button_index):
  40. remote_control = intertechno_cc1101.RemoteControl(address=address)
  41. with unittest.mock.patch.object(
  42. remote_control, "_send_command"
  43. ) as send_command_mock:
  44. remote_control.turn_off(button_index=button_index)
  45. send_command_mock.assert_called_once_with(button_index=button_index, command=0b00)