From a7ccea857a46b46b6af38ddcc4dc45d568ef6e8f Mon Sep 17 00:00:00 2001 From: YogevBokobza Date: Wed, 30 Oct 2024 12:03:48 +0200 Subject: [PATCH] improve coverage and preperation for using SwitcherApi --- tests/test_api_tcp_client.py | 88 +++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/tests/test_api_tcp_client.py b/tests/test_api_tcp_client.py index f4d1dc32..8dd3f96f 100644 --- a/tests/test_api_tcp_client.py +++ b/tests/test_api_tcp_client.py @@ -25,7 +25,7 @@ from assertpy import assert_that from pytest import fixture, mark, raises -from aioswitcher.api import Command, SwitcherType1Api, SwitcherType2Api +from aioswitcher.api import SWITCHER_TCP_PORT_TYPE1, SWITCHER_TCP_PORT_TYPE2, Command, SwitcherApi, SwitcherType1Api, SwitcherType2Api from aioswitcher.api.messages import ( SwitcherBaseResponse, SwitcherGetSchedulesResponse, @@ -56,6 +56,8 @@ device_id = "aaaaaa" device_key = "18" device_ip = "1.2.3.4" +device_port = SWITCHER_TCP_PORT_TYPE1 +device_port2 = SWITCHER_TCP_PORT_TYPE2 token_empty = "" token_not_empty = "zvVvd7JxtN7CgvkD1Psujw==" pytestmark = mark.asyncio @@ -181,6 +183,12 @@ async def test_get_state_function_with_valid_packets(reader_mock, writer_write, assert_that(response.unparsed_response).is_equal_to(get_state_response_packet) +async def test_get_state_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api1, device_ip, device_id, device_key, device_port).get_state() + + async def test_get_breeze_state_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root): login_response_packet = _load_dummy_packet(resource_path_root, "login2_response") get_breeze_state_response_packet = _load_dummy_packet(resource_path_root, "get_breeze_state") @@ -215,6 +223,12 @@ async def test_get_breeze_state_function_with_a_faulty_get_state_response_should assert_that(writer_write.call_count).is_equal_to(2) +async def test_get_breeze_state_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api2, device_ip, device_id, device_key, device_port2).get_breeze_state() + + async def test_control_breeze_device_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root): four_packets = _get_dummy_packets(resource_path_root, "login2_response", "get_breeze_state", "control_breeze_response", "control_breeze_swing_response") with patch.object(reader_mock, "read", side_effect=four_packets): @@ -225,6 +239,13 @@ async def test_control_breeze_device_function_with_valid_packets(reader_mock, wr assert_that(response.unparsed_response).is_equal_to(four_packets[-1]) +async def test_control_breeze_device_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + remote = SwitcherBreezeRemoteManager().get_remote('ELEC7022') + await SwitcherApi(device_type_api2, device_ip, device_id, device_key, device_port2).control_breeze_device(remote, DeviceState.ON, ThermostatMode.COOL, 24, ThermostatFanLevel.HIGH, ThermostatSwing.ON) + + async def test_control_breeze_device_update_state_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root): three_packets = _get_dummy_packets(resource_path_root, "login2_response", "get_breeze_state", "control_breeze_response") with patch.object(reader_mock, "read", side_effect=three_packets): @@ -367,6 +388,12 @@ async def test_turn_off_function_with_valid_packets(reader_mock, writer_write, c assert_that(response.unparsed_response).is_equal_to(two_packets[-1]) +async def test_control_device_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api1, device_ip, device_id, device_key, device_port).control_device(Command.ON, 0) + + async def test_set_name_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root): two_packets = _get_dummy_packets(resource_path_root, "login_response", "set_name_response") with patch.object(reader_mock, "read", side_effect=two_packets): @@ -376,6 +403,12 @@ async def test_set_name_function_with_valid_packets(reader_mock, writer_write, c assert_that(response.unparsed_response).is_equal_to(two_packets[-1]) +async def test_set_name_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api1, device_ip, device_id, device_key, device_port).set_device_name("my boiler") + + async def test_set_auto_shutdown_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root): two_packets = _get_dummy_packets(resource_path_root, "login_response", "set_auto_shutdown_response") with patch.object(reader_mock, "read", side_effect=two_packets): @@ -385,6 +418,12 @@ async def test_set_auto_shutdown_function_with_valid_packets(reader_mock, writer assert_that(response.unparsed_response).is_equal_to(two_packets[-1]) +async def test_set_auto_shutdown_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api1, device_ip, device_id, device_key, device_port).set_auto_shutdown(timedelta(hours=2, minutes=30)) + + async def test_get_schedules_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root): two_packets = _get_dummy_packets(resource_path_root, "login_response", "get_schedules_response") with patch.object(reader_mock, "read", side_effect=two_packets): @@ -394,6 +433,12 @@ async def test_get_schedules_function_with_valid_packets(reader_mock, writer_wri assert_that(response.unparsed_response).is_equal_to(two_packets[-1]) +async def test_get_schedules_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api1, device_ip, device_id, device_key, device_port).get_schedules() + + async def test_delete_schedule_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root): two_packets = _get_dummy_packets(resource_path_root, "login_response", "delete_schedule_response") with patch.object(reader_mock, "read", side_effect=two_packets): @@ -403,6 +448,12 @@ async def test_delete_schedule_function_with_valid_packets(reader_mock, writer_w assert_that(response.unparsed_response).is_equal_to(two_packets[-1]) +async def test_delete_schedule_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api1, device_ip, device_id, device_key, device_port).delete_schedule("0") + + async def test_create_schedule_function_with_valid_packets(reader_mock, writer_write, connected_api_type1, resource_path_root): two_packets = _get_dummy_packets(resource_path_root, "login_response", "create_schedule_response") with patch.object(reader_mock, "read", side_effect=two_packets): @@ -412,6 +463,12 @@ async def test_create_schedule_function_with_valid_packets(reader_mock, writer_w assert_that(response.unparsed_response).is_equal_to(two_packets[-1]) +async def test_create_schedule_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api1, device_ip, device_id, device_key, device_port).create_schedule("18:00", "19:00") + + async def test_stop_shutter_device_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root): two_packets = _get_dummy_packets(resource_path_root, "login_response", "stop_shutter_response") with patch.object(reader_mock, "read", side_effect=two_packets): @@ -430,6 +487,12 @@ async def test_stop_shutter_token_device_function_with_valid_packets(reader_mock assert_that(response.unparsed_response).is_equal_to(three_packets[-1]) +async def test_stop_shutter_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api2, device_ip, device_id, device_key, device_port2).stop_shutter(0) + + async def test_set_shutter_position_device_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root): two_packets = _get_dummy_packets(resource_path_root, "login_response", "set_shutter_position_response") with patch.object(reader_mock, "read", side_effect=two_packets): @@ -481,6 +544,12 @@ async def test_get_light_state_function_with_a_faulty_get_state_response_should_ assert_that(writer_write.call_count).is_equal_to(2) +async def test_get_light_state_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_token_api2, device_ip, device_id, device_key, device_port2, token_not_empty).get_light_state() + + async def test_set_light_function_with_valid_packets(reader_mock, writer_write, connected_api_token_type2, resource_path_root): three_packets = _get_dummy_packets(resource_path_root, "login_response", "login2_response", "set_light_response") with patch.object(reader_mock, "read", side_effect=three_packets): @@ -499,6 +568,12 @@ async def test_set_light_function_with_valid_packets_second_light(reader_mock, w assert_that(response.unparsed_response).is_equal_to(three_packets[-1]) +async def test_set_light_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_token_api2, device_ip, device_id, device_key, device_port2, token_not_empty).set_light(Command.ON, device_index2) + + async def test_get_shutter_state_function_with_valid_packets(reader_mock, writer_write, connected_api_type2, resource_path_root): login_response_packet = _load_dummy_packet(resource_path_root, "login2_response") get_state_response_packet = _load_dummy_packet(resource_path_root, "get_shutter_state_response") @@ -524,6 +599,12 @@ async def test_get_shutter_state_function_with_a_faulty_get_state_response_shoul assert_that(writer_write.call_count).is_equal_to(2) +async def test_get_shutter_state_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api2, device_ip, device_id, device_key, device_port2).get_shutter_state() + + async def test_set_position_function_with_a_faulty_get_state_response_should_raise_error(reader_mock, writer_write, connected_api_type2): with raises(RuntimeError, match="login request was not successful"): with patch.object(reader_mock, "read", return_value=b''): @@ -537,6 +618,11 @@ async def test_stop_position_function_with_a_faulty_get_state_response_should_ra await connected_api_type2.stop_shutter(device_index) writer_write.assert_called_once() +async def test_set_position_function_not_implemented(reader_mock, writer_mock): + with raises(NotImplementedError): + with patch("aioswitcher.api.open_connection", return_value=(reader_mock, writer_mock)): + await SwitcherApi(device_type_api2, device_ip, device_id, device_key, device_port2).set_position(50) + def _get_dummy_packets(resource_path_root, *packets): return [_load_dummy_packet(resource_path_root, packet) for packet in packets]