test_remote_control.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. @pytest.mark.parametrize("output_power_setting", (0xC6, 0x84))
  14. def test__send_command(
  15. address, button_index, command, expected_message, output_power_setting
  16. ):
  17. remote_control = intertechno_cc1101.RemoteControl(address=address)
  18. with unittest.mock.patch(
  19. "intertechno_cc1101._encode_message", return_value=b"dummy"
  20. ) as encode_message_mock, unittest.mock.patch(
  21. "intertechno_cc1101._cc1101_transmit"
  22. ) as transmit_mock:
  23. remote_control._send_command(
  24. button_index=button_index,
  25. command=command,
  26. output_power_setting=output_power_setting,
  27. )
  28. encode_message_mock.assert_called_once_with(expected_message)
  29. transmit_mock.assert_called_once_with(
  30. b"dummy", output_power_setting=output_power_setting
  31. )
  32. @pytest.mark.parametrize("button_index", (-1, 2 ** 6, 8.15))
  33. def test__send_command_invalid_button_index(button_index):
  34. remote_control = intertechno_cc1101.RemoteControl(address=12345678)
  35. with pytest.raises(ValueError):
  36. remote_control._send_command(
  37. button_index=button_index, command=0b01, output_power_setting=0xC6
  38. )
  39. @pytest.mark.parametrize("address", [12345678])
  40. @pytest.mark.parametrize("button_index", [7])
  41. def test_turn_on(address, button_index):
  42. remote_control = intertechno_cc1101.RemoteControl(address=address)
  43. with unittest.mock.patch.object(
  44. remote_control, "_send_command"
  45. ) as send_command_mock:
  46. remote_control.turn_on(button_index=button_index)
  47. send_command_mock.assert_called_once_with(
  48. button_index=button_index, command=0b01, output_power_setting=0xC6
  49. )
  50. @pytest.mark.parametrize("address", [12345678])
  51. @pytest.mark.parametrize("button_index", [7])
  52. @pytest.mark.parametrize("output_power_setting", [0x60])
  53. def test_turn_on_custom_output_power(address, button_index, output_power_setting):
  54. remote_control = intertechno_cc1101.RemoteControl(address=address)
  55. with unittest.mock.patch.object(
  56. remote_control, "_send_command"
  57. ) as send_command_mock:
  58. remote_control.turn_on(
  59. button_index=button_index, output_power_setting=output_power_setting
  60. )
  61. send_command_mock.assert_called_once_with(
  62. button_index=button_index,
  63. command=0b01,
  64. output_power_setting=output_power_setting,
  65. )
  66. @pytest.mark.parametrize("address", [12345678])
  67. @pytest.mark.parametrize("button_index", [7])
  68. def test_turn_off(address, button_index):
  69. remote_control = intertechno_cc1101.RemoteControl(address=address)
  70. with unittest.mock.patch.object(
  71. remote_control, "_send_command"
  72. ) as send_command_mock:
  73. remote_control.turn_off(button_index=button_index)
  74. send_command_mock.assert_called_once_with(
  75. button_index=button_index, command=0b00, output_power_setting=0xC6
  76. )
  77. @pytest.mark.parametrize("address", [12345678])
  78. @pytest.mark.parametrize("button_index", [7])
  79. @pytest.mark.parametrize("output_power_setting", [0x60])
  80. def test_turn_off_custom_output_power(address, button_index, output_power_setting):
  81. remote_control = intertechno_cc1101.RemoteControl(address=address)
  82. with unittest.mock.patch.object(
  83. remote_control, "_send_command"
  84. ) as send_command_mock:
  85. remote_control.turn_off(
  86. button_index=button_index, output_power_setting=output_power_setting
  87. )
  88. send_command_mock.assert_called_once_with(
  89. button_index=button_index,
  90. command=0b00,
  91. output_power_setting=output_power_setting,
  92. )