diff --git a/pyproject.toml b/pyproject.toml index fe2c3e92..e73ec99b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -103,15 +103,14 @@ disable_error_code = [ "arg-type", # 7 - "index", # 6 - "no-any-return", # 5 + "index", # 18 "no-untyped-def", # 16 "type-arg", # 15 ] # [[tool.mypy.overrides]] - module = "tests_rf.mocked_server.aiohttp" + module = "tests_rf.mocked_server.*" disable_error_code = [ "arg-type", # 2 (aiohttp,vendor) diff --git a/tests/tests/test_schedules.py b/tests/tests/test_schedules.py index 08113550..3f27fb02 100644 --- a/tests/tests/test_schedules.py +++ b/tests/tests/test_schedules.py @@ -8,6 +8,8 @@ import json from pathlib import Path +import voluptuous as vol + from evohomeasync2.schema.schedule import ( SCH_GET_SCHEDULE_DHW, SCH_GET_SCHEDULE_ZONE, @@ -22,13 +24,13 @@ WORK_DIR = Path(f"{TEST_DIR}/schedules") -def _test_schedule_schema(file_name: str, schema) -> dict: +def _test_schedule_schema(file_name: str, schema: vol.Schema) -> dict: def read_dict_from_file(file_name: str): with open(WORK_DIR.joinpath(file_name)) as f: data: dict = json.load(f) return data - return schema(read_dict_from_file(file_name)) + return schema(read_dict_from_file(file_name)) # type: ignore[no-any-return] def _test_schema_schedule_dhw() -> None: diff --git a/tests/tests_rf/helpers.py b/tests/tests_rf/helpers.py index 38c5eb23..68d9279f 100644 --- a/tests/tests_rf/helpers.py +++ b/tests/tests_rf/helpers.py @@ -68,7 +68,7 @@ async def should_work_v1( json: dict | None = None, content_type: str | None = "application/json", schema: vol.Schema | None = None, -) -> dict | list: +) -> dict | list | str: """Make a request that is expected to succeed.""" response: aiohttp.ClientResponse @@ -85,9 +85,12 @@ async def should_work_v1( assert response.content_type == content_type, content - if schema: # and response.content_type == "application/json" - return schema(content) - return content + if response.content_type != "application/json": + assert isinstance(content, str), content # mypy + return content + + assert isinstance(content, dict | list), content # mypy + return schema(content) if schema else content async def should_fail_v1( @@ -97,7 +100,7 @@ async def should_fail_v1( json: dict | None = None, content_type: str | None = "application/json", status: HTTPStatus | None = None, -) -> aiohttp.ClientResponse: +) -> dict | list | str | None: """Make a request that is expected to fail.""" response: aiohttp.ClientResponse @@ -107,13 +110,13 @@ async def should_fail_v1( response = await evo.broker._make_request(method, url, data=json) response.raise_for_status() - except aiohttp.ClientResponseError as exc: - assert exc.status == status, exc.status + except aiohttp.ClientResponseError as err: + assert err.status == status, err.status else: assert False, response.status if _DEBUG_DISABLE_STRICT_ASSERTS: - return response + return None # TODO: perform this transform in the broker if response.content_type == "application/json": @@ -127,8 +130,12 @@ async def should_fail_v1( assert "message" in content, content elif isinstance(content, list): assert "message" in content[0], content[0] + elif isinstance(content, str): + pass + else: + assert False, response.content_type - return content + return content # type: ignore[no-any-return] # version 2 helpers ################################################################### @@ -202,7 +209,7 @@ async def should_work( json: dict | None = None, content_type: str | None = "application/json", schema: vol.Schema | None = None, -) -> dict | list: +) -> dict | list | str: """Make a request that is expected to succeed.""" response: aiohttp.ClientResponse @@ -210,16 +217,16 @@ async def should_work( response, content = await evo.broker._client( method, f"{URL_BASE_2}/{url}", json=json ) - response.raise_for_status() assert response.content_type == content_type, content - if schema: - return schema(content) + if response.content_type != "application/json": + assert isinstance(content, str), content # mypy + return content - assert isinstance(content, dict | list), content - return content + assert isinstance(content, dict | list), content # mypy + return schema(content) if schema else content async def should_fail( @@ -229,33 +236,36 @@ async def should_fail( json: dict | None = None, content_type: str | None = "application/json", status: HTTPStatus | None = None, -) -> aiohttp.ClientResponse: +) -> dict | list | str | None: """Make a request that is expected to fail.""" response: aiohttp.ClientResponse - # if method == HTTPMethod.GET: - # content = await evo.broker.get( - # else: - # content = await evo.broker.put( - response, content = await evo.broker._client( method, f"{URL_BASE_2}/{url}", json=json ) try: response.raise_for_status() - except aiohttp.ClientResponseError as exc: - assert exc.status == status, exc.status + + except aiohttp.ClientResponseError as err: + assert err.status == status, err.status else: assert False, response.status if _DEBUG_DISABLE_STRICT_ASSERTS: - return response + return None assert response.content_type == content_type, response.content_type - if isinstance(content, list): + if isinstance(content, dict): + assert status in ( + HTTPStatus.NOT_FOUND, + HTTPStatus.METHOD_NOT_ALLOWED, + ), content + assert "message" in content, content # sometimes "code" too + + elif isinstance(content, list): assert status in ( HTTPStatus.BAD_REQUEST, HTTPStatus.NOT_FOUND, # CommTaskNotFound @@ -263,20 +273,13 @@ async def should_fail( ), content assert "message" in content[0], content[0] # sometimes "code" too - elif isinstance(content, dict): - assert status in ( - HTTPStatus.NOT_FOUND, - HTTPStatus.METHOD_NOT_ALLOWED, - ), content - assert "message" in content, content # sometimes "code" too - elif isinstance(content, str): # 404 assert status in (HTTPStatus.NOT_FOUND,), status else: - assert False, response.status + assert False, response.content_type - return response + return content async def wait_for_comm_task( @@ -293,6 +296,7 @@ async def wait_for_comm_task( start_time = dt.now() while True: response = await should_work(evo, HTTPMethod.GET, url) + assert isinstance(response, dict | list), response if response["state"] == "Succeeded": # type: ignore[call-overload] return True if (dt.now() - start_time).total_seconds() > timeout: diff --git a/tests/tests_rf/test_v2_task.py b/tests/tests_rf/test_v2_task.py index 34b16d4b..0d61cea5 100644 --- a/tests/tests_rf/test_v2_task.py +++ b/tests/tests_rf/test_v2_task.py @@ -61,7 +61,8 @@ async def _test_task_id(evo: evo2.EvohomeClient) -> None: # # PART 0: Get initial state... - old_status = await should_work(evo, HTTPMethod.GET, GET_URL) # HTTP 200 + old_status = await should_work(evo, HTTPMethod.GET, GET_URL) + assert isinstance(old_status, dict) # mypy # { # 'dhwId': '3933910', # 'temperatureStatus': {'isAvailable': False}, @@ -80,9 +81,9 @@ async def _test_task_id(evo: evo2.EvohomeClient) -> None: # } # HTTP 200 old_mode = { - SZ_MODE: old_status[SZ_STATE_STATUS][SZ_MODE], # type: ignore[call-overload] - SZ_STATE: old_status[SZ_STATE_STATUS][SZ_STATE], # type: ignore[call-overload] - SZ_UNTIL_TIME: old_status[SZ_STATE_STATUS].get(SZ_UNTIL), # type: ignore[call-overload] + SZ_MODE: old_status[SZ_STATE_STATUS][SZ_MODE], + SZ_STATE: old_status[SZ_STATE_STATUS][SZ_STATE], + SZ_UNTIL_TIME: old_status[SZ_STATE_STATUS].get(SZ_UNTIL), } # NOTE: untilTime/until # @@ -95,6 +96,7 @@ async def _test_task_id(evo: evo2.EvohomeClient) -> None: } result = await should_work(evo, HTTPMethod.PUT, PUT_URL, json=new_mode) + assert isinstance(result, dict | list) # mypy # {'id': '840367013'} # HTTP 201/Created task_id = result[0]["id"] if isinstance(result, list) else result["id"] diff --git a/tests/tests_rf/test_v2_urls.py b/tests/tests_rf/test_v2_urls.py index 05f1dc8f..1bd7d6fd 100644 --- a/tests/tests_rf/test_v2_urls.py +++ b/tests/tests_rf/test_v2_urls.py @@ -274,7 +274,7 @@ async def _test_schedule(evo: evo2.EvohomeClient) -> None: temp = schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] # type: ignore[call-overload] - schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] = temp + 1 # type: ignore[call-overload] + schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] = temp + 1 # type: ignore[call-overload,operator] _ = await should_work( evo, HTTPMethod.PUT, @@ -285,7 +285,7 @@ async def _test_schedule(evo: evo2.EvohomeClient) -> None: schedule = await should_work(evo, HTTPMethod.GET, url, schema=SCH_GET_SCHEDULE) assert ( schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] # type: ignore[call-overload] - == temp + 1 + == temp + 1 # type: ignore[operator] ) schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] = temp # type: ignore[call-overload]