Debian Codegraph @

src/tests/base_test_cases.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 """Contains base test cases for mocking devices and API calls."""
2
3 from __future__ import annotations
4 from typing import TYPE_CHECKING
5 import asyncio
6 import pytest
7 from unittest.mock import MagicMock, patch
8
9 from pyvesync import VeSync
10 from pyvesync.models.vesync_models import ResponseDeviceDetailsModel
11 from defaults import TestDefaults, API_DEFAULTS
12 from call_json import ALL_DEVICE_MAP_DICT, DeviceList
13
14 if TYPE_CHECKING:
15 from pyvesync.base_devices import VeSyncBaseDevice
16
17
18 class TestApiFunc:
19 """Test case to instantiate vs object and patch the session object."""
20 default_endpoint = '/endpoint'
21
22 @pytest.fixture(autouse=True, scope='function')
23 def setup(self, caplog):
24 """Fixture to instantiate VeSync object, start logging and start Mock.
25
26 This fixture mocks the ClientSession object directly.
27
28 Yields:
29 self: Class instance with mocked session object to test response handling.
30 """
31 self.mock_api_call = patch("pyvesync.vesync.ClientSession")
32 self.caplog = caplog
33 self.mock_api = self.mock_api_call.start()
34 self.loop = asyncio.new_event_loop()
35 self.mock = MagicMock()
36 self.manager = VeSync(API_DEFAULTS['EMAIL'], API_DEFAULTS['PASSWORD'])
37 self.manager.enabled = True
38 self.manager.redact = False
39 self.manager.auth._token = TestDefaults.token
40 self.manager.auth._account_id = TestDefaults.account_id
41 yield
42 self.mock.stop()
•43 self.loop.close()
44
45 async def run_coro(self, coro):
46 """Run a coroutine in the event loop."""
47 return await coro
48
49 def run_in_loop(self, func, *args, **kwargs):
50 """Run a function in the event loop."""
51 return self.loop.run_until_complete(self.run_coro(func(*args, **kwargs)))
52
53
54 class TestBase:
55 """Base class for tests with the call_api() method mocked.
56
57 Contains instantiated VeSync object and mocked API call for call_api() function.
58
59 Attributes:
60 mock_api (Mock): Mock for call_api() function.
61 manager (VeSync): Instantiated VeSync object that is logged in.
62 caplog (LogCaptureFixture): Pytest fixture for capturing logs.
63 """
64 overwrite = False
65 write_api = False
66
67 @pytest.fixture(autouse=True, scope='function')
68 def setup(self, caplog):
69 """Fixture to instantiate VeSync object, start logging and start Mock.
70
71 Attributes
72 ----------
73 self.mock_api : Mock
74 self.manager : VeSync
75 self.caplog : LogCaptureFixture
76
77 Yields
78 ------
79 Class instance with mocked call_api() function and VeSync object
80 """
81 self.loop = asyncio.new_event_loop()
82 self.mock_api_call = patch('pyvesync.vesync.VeSync.async_call_api')
83 self.caplog = caplog
84 self.mock_api = self.mock_api_call.start()
85 self.mock_api.return_value.ok = True
86 self.manager = VeSync(TestDefaults.email, TestDefaults.password)
87 self.manager.redact = False
88 self.manager.time_zone = TestDefaults.time_zone
89 self.manager.enabled = True
90 self.manager.auth._token = TestDefaults.token
91 self.manager.auth._account_id = TestDefaults.account_id
92 yield
93 self.mock_api_call.stop()
•94 self.loop.close()
95
96 async def run_coro(self, coro):
97 """Run a coroutine in the event loop."""
98 return await coro
99
100 def run_in_loop(self, func, *args, **kwargs) -> VeSyncBaseDevice | None:
101 """Run a function in the event loop."""
102 return self.loop.run_until_complete(self.run_coro(func(*args, **kwargs)))
103
104 def get_device(self, product_type: str, setup_entry: str) -> VeSyncBaseDevice:
105 """Get device from device details dict.
106
107 Args:
108 device_details (dict): Device details dictionary from call_json module.
109
110 Returns:
111 Device object from VeSync.devices
112 """
113 if len(self.manager.devices) > 0:
114 self.manager.devices.clear()
115 device_map = ALL_DEVICE_MAP_DICT[setup_entry]
116 device_config = DeviceList.device_list_item(device_map)
117 self.manager.devices.add_device_from_model(
118 ResponseDeviceDetailsModel.from_dict(device_config), self.manager
119 )
120 device_list = getattr(self.manager.devices, product_type)
121 assert len(device_list) == 1, f"Could not instantiate {product_type} device."
122 return device_list[0]