test_remote_control.py 3.7 KB

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