Skip to content

Commit 268e09b

Browse files
authored
Update test_oig_cloud_api.py
1 parent 8e3d4bd commit 268e09b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tests/test_oig_cloud_api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
1+
import unittest
2+
from unittest.mock import patch, AsyncMock
3+
from custom_components.oig_cloud.api.oig_cloud_api import OigCloudApi
14

5+
class TestOigCloudApi(unittest.TestCase):
6+
def setUp(self):
7+
self.api = OigCloudApi("username", "password", False, None)
8+
9+
@patch("custom_components.oig_cloud.api.oig_cloud_api.aiohttp.ClientSession")
10+
@patch("custom_components.oig_cloud.api.oig_cloud_api.datetime")
11+
@patch("custom_components.oig_cloud.api.oig_cloud_api.tracer")
12+
async def test_get_stats(self, mock_tracer, mock_datetime, mock_session):
13+
mock_datetime.datetime.now.return_value = mock_datetime.datetime(2025, 1, 27, 8, 34, 57)
14+
mock_response = AsyncMock()
15+
mock_response.status = 200
16+
mock_response.json.return_value = {"key": "value"}
17+
mock_session.return_value.__aenter__.return_value.get.return_value = mock_response
18+
19+
result = await self.api.get_stats()
20+
self.assertEqual(result, {"key": "value"})
21+
self.assertEqual(self.api.last_state, {"key": "value"})
22+
self.assertEqual(self.api.box_id, "key")
23+
24+
@patch("custom_components.oig_cloud.api.oig_cloud_api.aiohttp.ClientSession")
25+
@patch("custom_components.oig_cloud.api.oig_cloud_api.datetime")
26+
@patch("custom_components.oig_cloud.api.oig_cloud_api.tracer")
27+
async def test_get_stats_cache(self, mock_tracer, mock_datetime, mock_session):
28+
mock_datetime.datetime.now.return_value = mock_datetime.datetime(2025, 1, 27, 8, 34, 57)
29+
self.api._last_update = mock_datetime.datetime(2025, 1, 27, 8, 34, 30)
30+
self.api.last_state = {"cached_key": "cached_value"}
31+
32+
result = await self.api.get_stats()
33+
self.assertEqual(result, {"cached_key": "cached_value"})
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)