import unittest.mock import pytest import intertechno_cc1101 # pylint: disable=protected-access @pytest.mark.parametrize("address", (-1, 2 ** 26, 21.42)) def test___init___invalid_address(address): with pytest.raises(ValueError): intertechno_cc1101.RemoteControl(address=address) @pytest.mark.parametrize( ("address", "button_index", "command", "expected_message"), ((12345678, 0, 0b00, 790123392), (2 ** 26 - 1, 0b1111, 0b01, 4294967263)), ) @pytest.mark.parametrize("power_setting", (0xC6, 0x84)) def test__send_command(address, button_index, command, expected_message, power_setting): remote_control = intertechno_cc1101.RemoteControl(address=address) with unittest.mock.patch( "intertechno_cc1101._encode_message", return_value=b"dummy" ) as encode_message_mock, unittest.mock.patch( "intertechno_cc1101._cc1101_transmit" ) as transmit_mock: remote_control._send_command( button_index=button_index, command=command, power_setting=power_setting, ) encode_message_mock.assert_called_once_with(expected_message) transmit_mock.assert_called_once_with(b"dummy", power_setting=power_setting) @pytest.mark.parametrize("button_index", (-1, 2 ** 6, 8.15)) def test__send_command_invalid_button_index(button_index): remote_control = intertechno_cc1101.RemoteControl(address=12345678) with pytest.raises(ValueError): remote_control._send_command( button_index=button_index, command=0b01, power_setting=0xC6 ) @pytest.mark.parametrize("address", [12345678]) @pytest.mark.parametrize("button_index", [7]) def test_turn_on(address, button_index): remote_control = intertechno_cc1101.RemoteControl(address=address) with unittest.mock.patch.object( remote_control, "_send_command" ) as send_command_mock: remote_control.turn_on(button_index=button_index) send_command_mock.assert_called_once_with( button_index=button_index, command=0b01, power_setting=0xC6 ) @pytest.mark.parametrize("address", [12345678]) @pytest.mark.parametrize("button_index", [7]) @pytest.mark.parametrize("power_setting", [0x60]) def test_turn_on_custom_power(address, button_index, power_setting): remote_control = intertechno_cc1101.RemoteControl(address=address) with unittest.mock.patch.object( remote_control, "_send_command" ) as send_command_mock: remote_control.turn_on(button_index=button_index, power_setting=power_setting) send_command_mock.assert_called_once_with( button_index=button_index, command=0b01, power_setting=power_setting, ) @pytest.mark.parametrize("address", [12345678]) @pytest.mark.parametrize("button_index", [7]) def test_turn_off(address, button_index): remote_control = intertechno_cc1101.RemoteControl(address=address) with unittest.mock.patch.object( remote_control, "_send_command" ) as send_command_mock: remote_control.turn_off(button_index=button_index) send_command_mock.assert_called_once_with( button_index=button_index, command=0b00, power_setting=0xC6 ) @pytest.mark.parametrize("address", [12345678]) @pytest.mark.parametrize("button_index", [7]) @pytest.mark.parametrize("power_setting", [0x60]) def test_turn_off_custom_power(address, button_index, power_setting): remote_control = intertechno_cc1101.RemoteControl(address=address) with unittest.mock.patch.object( remote_control, "_send_command" ) as send_command_mock: remote_control.turn_off(button_index=button_index, power_setting=power_setting) send_command_mock.assert_called_once_with( button_index=button_index, command=0b00, power_setting=power_setting, )