11"""Unit tests for the high-level OverkizClient behaviour and responses."""
22
3- # ruff: noqa: ASYNC230, S106
3+ # ruff: noqa: S106
44# S106: Test credentials use dummy values.
5- # ASYNC230: Blocking open() is acceptable for reading test fixtures
65
76from __future__ import annotations
87
98import json
10- import os
9+ from pathlib import Path
1110from unittest .mock import AsyncMock , patch
1211
1312import aiohttp
2524from pyoverkiz .response_handler import check_response
2625from pyoverkiz .utils import create_local_server_config
2726
28- CURRENT_DIR = os . path . dirname ( os . path . abspath ( __file__ ))
27+ CURRENT_DIR = Path ( __file__ ). resolve (). parent
2928
3029
3130class TestOverkizClient :
@@ -60,9 +59,7 @@ async def test_get_api_type_local(self, local_client: OverkizClient):
6059 @pytest .mark .asyncio
6160 async def test_get_devices_basic (self , client : OverkizClient ):
6261 """Ensure the client can fetch and parse the basic devices fixture."""
63- with open (
64- os .path .join (CURRENT_DIR , "devices.json" ), encoding = "utf-8"
65- ) as raw_devices :
62+ with (CURRENT_DIR / "devices.json" ).open (encoding = "utf-8" ) as raw_devices :
6663 resp = MockResponse (raw_devices .read ())
6764
6865 with patch .object (aiohttp .ClientSession , "get" , return_value = resp ):
@@ -81,8 +78,7 @@ async def test_fetch_events_basic(
8178 self , client : OverkizClient , fixture_name : str , event_length : int
8279 ):
8380 """Parameterised test that fetches events fixture and checks the expected count."""
84- with open (
85- os .path .join (CURRENT_DIR , "fixtures/event/" + fixture_name ),
81+ with (CURRENT_DIR / "fixtures" / "event" / fixture_name ).open (
8682 encoding = "utf-8" ,
8783 ) as raw_events :
8884 resp = MockResponse (raw_events .read ())
@@ -94,8 +90,8 @@ async def test_fetch_events_basic(
9490 @pytest .mark .asyncio
9591 async def test_fetch_events_simple_cast (self , client : OverkizClient ):
9692 """Check that event state values from the cloud (strings) are cast to appropriate types."""
97- with open (
98- os . path . join ( CURRENT_DIR , "fixtures/event/events.json" ), encoding = "utf-8"
93+ with ( CURRENT_DIR / "fixtures" / "event" / "events.json" ). open (
94+ encoding = "utf-8" ,
9995 ) as raw_events :
10096 resp = MockResponse (raw_events .read ())
10197
@@ -195,8 +191,7 @@ async def test_backoff_retries_on_concurrent_requests(
195191 @pytest .mark .asyncio
196192 async def test_fetch_events_casting (self , client : OverkizClient , fixture_name : str ):
197193 """Validate that fetched event states are cast to the expected Python types for each data type."""
198- with open (
199- os .path .join (CURRENT_DIR , "fixtures/event/" + fixture_name ),
194+ with (CURRENT_DIR / "fixtures" / "event" / fixture_name ).open (
200195 encoding = "utf-8" ,
201196 ) as raw_events :
202197 resp = MockResponse (raw_events .read ())
@@ -264,8 +259,7 @@ async def test_get_setup(
264259 gateway_count : int ,
265260 ):
266261 """Ensure setup parsing yields expected device and gateway counts and device metadata."""
267- with open (
268- os .path .join (CURRENT_DIR , "fixtures/setup/" + fixture_name ),
262+ with (CURRENT_DIR / "fixtures" / "setup" / fixture_name ).open (
269263 encoding = "utf-8" ,
270264 ) as setup_mock :
271265 resp = MockResponse (setup_mock .read ())
@@ -316,8 +310,7 @@ async def test_get_setup(
316310 @pytest .mark .asyncio
317311 async def test_get_diagnostic_data (self , client : OverkizClient , fixture_name : str ):
318312 """Verify that diagnostic data can be fetched and is not empty."""
319- with open (
320- os .path .join (CURRENT_DIR , "fixtures/setup/" + fixture_name ),
313+ with (CURRENT_DIR / "fixtures" / "setup" / fixture_name ).open (
321314 encoding = "utf-8" ,
322315 ) as setup_mock :
323316 resp = MockResponse (setup_mock .read ())
@@ -329,8 +322,7 @@ async def test_get_diagnostic_data(self, client: OverkizClient, fixture_name: st
329322 @pytest .mark .asyncio
330323 async def test_get_diagnostic_data_redacted_by_default (self , client : OverkizClient ):
331324 """Ensure diagnostics are redacted when no argument is provided."""
332- with open (
333- os .path .join (CURRENT_DIR , "fixtures/setup/setup_tahoma_1.json" ),
325+ with (CURRENT_DIR / "fixtures" / "setup" / "setup_tahoma_1.json" ).open (
334326 encoding = "utf-8" ,
335327 ) as setup_mock :
336328 resp = MockResponse (setup_mock .read ())
@@ -349,8 +341,7 @@ async def test_get_diagnostic_data_redacted_by_default(self, client: OverkizClie
349341 @pytest .mark .asyncio
350342 async def test_get_diagnostic_data_without_masking (self , client : OverkizClient ):
351343 """Ensure diagnostics can be returned without masking when requested."""
352- with open (
353- os .path .join (CURRENT_DIR , "fixtures/setup/setup_tahoma_1.json" ),
344+ with (CURRENT_DIR / "fixtures" / "setup" / "setup_tahoma_1.json" ).open (
354345 encoding = "utf-8" ,
355346 ) as setup_mock :
356347 raw_setup = setup_mock .read ()
@@ -546,8 +537,7 @@ async def test_check_response_exception_handling(
546537 ):
547538 """Ensure client raises the correct error for various error fixtures/status codes."""
548539 if fixture_name :
549- with open (
550- os .path .join (CURRENT_DIR , "fixtures/exceptions/" + fixture_name ),
540+ with (CURRENT_DIR / "fixtures" / "exceptions" / fixture_name ).open (
551541 encoding = "utf-8" ,
552542 ) as raw_events :
553543 resp = MockResponse (raw_events .read (), status_code )
@@ -563,8 +553,7 @@ async def test_get_setup_options(
563553 client : OverkizClient ,
564554 ):
565555 """Check that setup options are parsed and return the expected number of Option instances."""
566- with open (
567- os .path .join (CURRENT_DIR , "fixtures/endpoints/setup-options.json" ),
556+ with (CURRENT_DIR / "fixtures" / "endpoints" / "setup-options.json" ).open (
568557 encoding = "utf-8" ,
569558 ) as raw_events :
570559 resp = MockResponse (raw_events .read ())
@@ -665,8 +654,7 @@ async def test_get_setup_option(
665654 instance : Option | None ,
666655 ):
667656 """Verify retrieval of a single setup option by name, including non-existent options."""
668- with open (
669- os .path .join (CURRENT_DIR , "fixtures/endpoints/" + fixture_name ),
657+ with (CURRENT_DIR / "fixtures" / "endpoints" / fixture_name ).open (
670658 encoding = "utf-8" ,
671659 ) as raw_events :
672660 resp = MockResponse (raw_events .read ())
@@ -696,8 +684,7 @@ async def test_get_action_groups(
696684 scenario_count : int ,
697685 ):
698686 """Ensure action groups (scenarios) are parsed correctly and contain actions and commands."""
699- with open (
700- os .path .join (CURRENT_DIR , "fixtures/action_groups/" + fixture_name ),
687+ with (CURRENT_DIR / "fixtures" / "action_groups" / fixture_name ).open (
701688 encoding = "utf-8" ,
702689 ) as action_group_mock :
703690 resp = MockResponse (action_group_mock .read ())
0 commit comments