Skip to content

Commit

Permalink
improve coverage and preperation for using SwitcherApi
Browse files Browse the repository at this point in the history
  • Loading branch information
YogevBokobza committed Oct 30, 2024
1 parent 130dbc2 commit a7ccea8
Showing 1 changed file with 87 additions and 1 deletion.
88 changes: 87 additions & 1 deletion tests/test_api_tcp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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")
Expand All @@ -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''):
Expand All @@ -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]
Expand Down

0 comments on commit a7ccea8

Please sign in to comment.