-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_external_session.py
More file actions
199 lines (153 loc) · 5.73 KB
/
test_external_session.py
File metadata and controls
199 lines (153 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""Test external session management."""
import json
from unittest.mock import AsyncMock, MagicMock
import aiohttp
import pytest
from openevsehttp.__main__ import OpenEVSE
from tests.common import load_fixture
pytestmark = pytest.mark.asyncio
TEST_URL_STATUS = "http://openevse.test.tld/status"
TEST_URL_CONFIG = "http://openevse.test.tld/config"
TEST_TLD = "openevse.test.tld"
async def test_external_session_provided():
"""Test that an external session is used when provided."""
# Create a mock session
mock_session = MagicMock(spec=aiohttp.ClientSession)
mock_session.closed = False
# Mock the response
mock_response = AsyncMock()
mock_response.status = 200
mock_response.text = AsyncMock(return_value=load_fixture("v4_json/status.json"))
# Mock the get method to return the response
mock_get = AsyncMock(return_value=mock_response)
mock_get.__aenter__ = AsyncMock(return_value=mock_response)
mock_get.__aexit__ = AsyncMock(return_value=None)
mock_session.get = MagicMock(return_value=mock_get)
# Create OpenEVSE instance with external session
charger = OpenEVSE(TEST_TLD, session=mock_session)
# Verify the session is stored
assert charger._session is mock_session
assert charger._session_external is True
# Make a request
await charger.process_request(TEST_URL_STATUS, method="get")
# Verify the external session was used
mock_session.get.assert_called_once()
async def test_no_external_session(mock_aioclient):
"""Test that a temporary session is created when none is provided."""
mock_aioclient.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v4_json/status.json"),
)
# Create OpenEVSE instance without external session
charger = OpenEVSE(TEST_TLD)
# Verify no session is stored
assert charger._session is None
assert charger._session_external is False
# Make a request - should create a temporary session
await charger.process_request(TEST_URL_STATUS, method="get")
async def test_external_session_with_update(mock_aioclient):
"""Test that external session is used during update."""
mock_aioclient.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v4_json/status.json"),
)
mock_aioclient.get(
TEST_URL_CONFIG,
status=200,
body=load_fixture("v4_json/config.json"),
)
# Create a real session for testing
async with aiohttp.ClientSession() as session:
# Create OpenEVSE instance with external session
charger = OpenEVSE(TEST_TLD, session=session)
# Verify the session is stored
assert charger._session is session
assert charger._session_external is True
# Update should use the external session
await charger.update()
# Verify status was updated
assert charger._status is not None
assert charger._config is not None
async def test_websocket_uses_external_session(mock_aioclient):
"""Test that websocket uses the external session."""
mock_aioclient.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v4_json/status.json"),
)
mock_aioclient.get(
TEST_URL_CONFIG,
status=200,
body=load_fixture("v4_json/config.json"),
)
# Create a real session for testing
async with aiohttp.ClientSession() as session:
# Create OpenEVSE instance with external session
charger = OpenEVSE(TEST_TLD, session=session)
# Update to initialize websocket
await charger.update()
# Verify websocket was created with the session
assert charger.websocket is not None
assert charger.websocket.session is session
assert charger.websocket._session_external is True
# Cleanup
await charger.ws_disconnect()
async def test_firmware_check_with_external_session(mock_aioclient):
"""Test that firmware_check uses external session."""
mock_aioclient.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v4_json/status.json"),
)
mock_aioclient.get(
TEST_URL_CONFIG,
status=200,
body=load_fixture("v4_json/config.json"),
)
github_response = {
"tag_name": "v4.2.0",
"body": "Release notes",
"html_url": "https://github.com/OpenEVSE/ESP32_WiFi_V4.x/releases/tag/v4.2.0",
}
mock_aioclient.get(
"https://api.github.com/repos/OpenEVSE/ESP32_WiFi_V4.x/releases/latest",
status=200,
body=json.dumps(github_response),
)
# Create OpenEVSE instance without external session (use mocked responses)
charger = OpenEVSE(TEST_TLD)
# Load config first
await charger.update()
# Check firmware - should use mocked session
result = await charger.firmware_check()
# Verify result
assert result is not None
assert result["latest_version"] == "v4.2.0"
async def test_session_not_closed_when_external(mock_aioclient):
"""Test that external session is not closed by the library."""
mock_aioclient.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v4_json/status.json"),
)
mock_aioclient.get(
TEST_URL_CONFIG,
status=200,
body=load_fixture("v4_json/config.json"),
)
# Create a real session
session = aiohttp.ClientSession()
try:
# Create OpenEVSE instance with external session
charger = OpenEVSE(TEST_TLD, session=session)
# Update to initialize websocket
await charger.update()
# Disconnect websocket
await charger.ws_disconnect()
# Session should still be open (not closed by library)
assert not session.closed
finally:
# Clean up the session ourselves
await session.close()