Skip to content

Commit 2d1ffdf

Browse files
committed
fix typo
* adjust tests
1 parent 5b2c6cd commit 2d1ffdf

File tree

3 files changed

+102
-69
lines changed

3 files changed

+102
-69
lines changed

openevsehttp/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,16 @@ def __init__(self, host: str, user: str = None, pwd: str = None) -> None:
170170
self._config = None
171171
self._override = None
172172
self._ws_listening = False
173-
self.websocket: Optional[OpenEVSEWebsocket] = None # type: ignore
173+
self.websocket: Optional[OpenEVSEWebsocket] = None
174174

175175
async def send_command(self, command: str) -> tuple | None:
176176
"""Send a RAPI command to the charger and parses the response."""
177177
auth = None
178178
url = f"{self.url}r"
179179
data = {"json": 1, "rapi": command}
180180

181-
if self._user and self._password:
182-
auth = aiohttp.BasicAuth(self._user, self._password)
181+
if self._user and self._pwd:
182+
auth = aiohttp.BasicAuth(self._user, self._pwd)
183183

184184
_LOGGER.debug("Posting data: %s to %s", command, url)
185185
async with aiohttp.ClientSession() as session:
@@ -202,8 +202,8 @@ async def update(self) -> None:
202202
auth = None
203203
urls = [f"{self.url}config"]
204204

205-
if self._user and self._password:
206-
auth = aiohttp.BasicAuth(self._user, self._password)
205+
if self._user and self._pwd:
206+
auth = aiohttp.BasicAuth(self._user, self._pwd)
207207

208208
if not self._ws_listening:
209209
urls = [f"{self.url}status", f"{self.url}config"]

tests/conftest.py

Lines changed: 91 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,27 @@ def test_charger_auth_err(status_mock_err, config_mock_err):
2626

2727

2828
@pytest.fixture(name="status_mock_err")
29-
def mock_status_err(requests_mock):
29+
def mock_status_err():
3030
"""Mock the status reply."""
31-
requests_mock.get(
32-
TEST_URL_STATUS,
33-
status_code=401,
34-
)
31+
with aioresponses() as client_mock:
32+
client_mock.get(
33+
TEST_URL_STATUS,
34+
status=401,
35+
)
36+
37+
yield client_mock
3538

3639

3740
@pytest.fixture(name="config_mock_err")
38-
def mock_config_err(requests_mock):
41+
def mock_config_err():
3942
"""Mock the config reply."""
40-
requests_mock.get(
41-
TEST_URL_CONFIG,
42-
status_code=401,
43-
)
43+
with aioresponses() as client_mock:
44+
client_mock.get(
45+
TEST_URL_CONFIG,
46+
status=401,
47+
)
48+
49+
yield client_mock
4450

4551

4652
@pytest.fixture(name="test_charger")
@@ -56,87 +62,114 @@ def test_charger_v2(status_mock_v2, config_mock_v2):
5662

5763

5864
@pytest.fixture(name="status_mock")
59-
def mock_status(requests_mock):
65+
def mock_status():
6066
"""Mock the status reply."""
61-
requests_mock.get(
62-
TEST_URL_STATUS,
63-
text=load_fixture("v4_json/status.json"),
64-
)
67+
with aioresponses() as client_mock:
68+
client_mock.get(
69+
TEST_URL_STATUS,
70+
status=200,
71+
body=load_fixture("v4_json/status.json"),
72+
)
73+
74+
yield client_mock
6575

6676

6777
@pytest.fixture(name="config_mock")
68-
def mock_config(requests_mock):
78+
def mock_config():
6979
"""Mock the config reply."""
70-
requests_mock.get(
71-
TEST_URL_CONFIG,
72-
text=load_fixture("v4_json/config.json"),
73-
)
80+
with aioresponses() as client_mock:
81+
client_mock.get(
82+
TEST_URL_CONFIG,
83+
status=200,
84+
body=load_fixture("v4_json/config.json"),
85+
)
86+
87+
yield client_mock
7488

7589

7690
@pytest.fixture(name="status_mock_v2")
77-
def mock_status_v2(requests_mock):
91+
def mock_status_v2():
7892
"""Mock the status reply."""
79-
requests_mock.get(
80-
TEST_URL_STATUS,
81-
text=load_fixture("v2_json/status.json"),
82-
)
93+
with aioresponses() as client_mock:
94+
client_mock.get(
95+
TEST_URL_STATUS,
96+
status=200,
97+
body=load_fixture("v2_json/status.json"),
98+
)
99+
yield client_mock
83100

84101

85102
@pytest.fixture(name="config_mock_v2")
86-
def mock_config_v2(requests_mock):
103+
def mock_config_v2():
87104
"""Mock the config reply."""
88-
requests_mock.get(
89-
TEST_URL_CONFIG,
90-
text=load_fixture("v2_json/config.json"),
91-
)
105+
with aioresponses() as client_mock:
106+
client_mock.get(
107+
TEST_URL_CONFIG,
108+
status=200,
109+
body=load_fixture("v2_json/config.json"),
110+
)
111+
yield client_mock
92112

93113

94114
@pytest.fixture(name="send_command_mock")
95-
def mock_send_command(requests_mock):
115+
def mock_send_command():
96116
"""Mock the command reply."""
97-
value = {"cmd": "OK", "ret": "$OK^20"}
98-
requests_mock.post(
99-
TEST_URL_RAPI,
100-
text=json.dumps(value),
101-
)
117+
with aioresponses() as client_mock:
118+
value = {"cmd": "OK", "ret": "$OK^20"}
119+
client_mock.post(
120+
TEST_URL_RAPI,
121+
status=200,
122+
body=json.dumps(value),
123+
)
124+
yield client_mock
102125

103126

104127
@pytest.fixture(name="send_command_parse_err")
105-
def mock_send_command_parse_err(requests_mock):
128+
def mock_send_command_parse_err():
106129
"""Mock the command reply parse err."""
107-
requests_mock.post(
108-
TEST_URL_RAPI,
109-
status_code=400,
110-
)
130+
with aioresponses() as client_mock:
131+
client_mock.post(
132+
TEST_URL_RAPI,
133+
status=400,
134+
)
135+
yield client_mock
111136

112137

113138
@pytest.fixture(name="send_command_auth_err")
114-
def mock_send_command_auth_err(requests_mock):
139+
def mock_send_command_auth_err():
115140
"""Mock the command reply auth err."""
116-
requests_mock.post(
117-
TEST_URL_RAPI,
118-
status_code=401,
119-
)
141+
with aioresponses() as client_mock:
142+
client_mock.post(
143+
TEST_URL_RAPI,
144+
status=401,
145+
)
146+
yield client_mock
120147

121148

122149
@pytest.fixture(name="send_command_mock_missing")
123-
def mock_send_command_missing(requests_mock):
150+
def mock_send_command_missing():
124151
"""Mock the command reply."""
125-
value = {"cmd": "OK", "what": "$NK^21"}
126-
requests_mock.post(
127-
TEST_URL_RAPI,
128-
text=json.dumps(value),
129-
)
152+
with aioresponses() as client_mock:
153+
value = {"cmd": "OK", "what": "$NK^21"}
154+
client_mock.post(
155+
TEST_URL_RAPI,
156+
status=200,
157+
body=json.dumps(value),
158+
)
159+
yield client_mock
130160

131161

132162
@pytest.fixture(name="send_command_mock_failed")
133-
def mock_send_command_failed(requests_mock):
163+
def mock_send_command_failed():
134164
"""Mock the command reply."""
135-
value = {"cmd": "OK", "ret": "$NK^21"}
136-
requests_mock.post(
137-
TEST_URL_RAPI,
138-
text=json.dumps(value),
139-
)
165+
with aioresponses() as client_mock:
166+
value = {"cmd": "OK", "ret": "$NK^21"}
167+
client_mock.post(
168+
TEST_URL_RAPI,
169+
status=200,
170+
body=json.dumps(value),
171+
)
172+
yield client_mock
140173

141174

142175
@pytest.fixture

tests/test_init.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,39 @@ async def test_get_status_auth_err(test_charger_auth_err, aioclient_mock):
2121

2222
async def test_send_command(test_charger, send_command_mock):
2323
"""Test v4 Status reply"""
24-
status = test_charger.send_command("test")
24+
status = await test_charger.send_command("test")
2525
assert status == ("OK", "$OK^20")
2626

2727

2828
async def test_send_command_failed(test_charger, send_command_mock_failed):
2929
"""Test v4 Status reply"""
30-
status = test_charger.send_command("test")
30+
status = await test_charger.send_command("test")
3131
assert status == ("OK", "$NK^21")
3232

3333

3434
async def test_send_command_missing(test_charger, send_command_mock_missing):
3535
"""Test v4 Status reply"""
36-
status = test_charger.send_command("test")
36+
status = await test_charger.send_command("test")
3737
assert status == (False, "")
3838

3939

4040
async def test_send_command_auth(test_charger_auth, send_command_mock):
4141
"""Test v4 Status reply"""
42-
status = test_charger_auth.send_command("test")
42+
status = await test_charger_auth.send_command("test")
4343
assert status == ("OK", "$OK^20")
4444

4545

4646
async def test_send_command_parse_err(test_charger_auth, send_command_parse_err):
4747
"""Test v4 Status reply"""
4848
with pytest.raises(openevsehttp.ParseJSONError):
49-
status = test_charger_auth.send_command("test")
49+
status = await test_charger_auth.send_command("test")
5050
assert status is None
5151

5252

5353
async def test_send_command_auth_err(test_charger_auth, send_command_auth_err):
5454
"""Test v4 Status reply"""
5555
with pytest.raises(openevsehttp.AuthenticationError):
56-
status = test_charger_auth.send_command("test")
56+
status = await test_charger_auth.send_command("test")
5757
assert status is None
5858

5959

0 commit comments

Comments
 (0)