Skip to content

Commit 1d9cf70

Browse files
committed
Add tests for send_command
1 parent a277cc6 commit 1d9cf70

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

openevsehttp/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ def __init__(self, host: str, user: str = None, pwd: str = None) -> None:
4747
self._status = None
4848
self._config = None
4949

50-
async def send_command(self, command: str) -> tuple | None:
50+
def send_command(self, command: str) -> tuple | None:
5151
"""Send a command via HTTP to the charger and prases the response."""
5252
url = f"{self._url}/r?json=1"
5353
data = {"rapi": command}
5454

5555
_LOGGER.debug("Posting data: %s to %s", command, url)
5656
if self._user is not None:
57-
value = requests.get(url, data=data, auth=(self._user, self._pwd))
57+
value = requests.post(url, data=data, auth=(self._user, self._pwd))
5858
else:
59-
value = requests.get(url, data=data)
59+
value = requests.post(url, data=data)
6060

6161
if value.status_code == 400:
6262
raise ParseJSONError
@@ -66,7 +66,7 @@ async def send_command(self, command: str) -> tuple | None:
6666
if "ret" not in value.json():
6767
return False, ""
6868
resp = value.json()
69-
return resp[0] == "OK", resp[1:]
69+
return resp["cmd"] == "OK", resp["ret"]
7070

7171
def update(self) -> None:
7272
"""Update the values."""

tests/conftest.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Provide common pytest fixtures."""
22
import pytest
3+
import json
34

45
import openevsehttp
56
from tests.common import load_fixture
@@ -85,3 +86,41 @@ def mock_config_v2(requests_mock):
8586
"http://openevse.test.tld/config",
8687
text=load_fixture("v2_json/config.json"),
8788
)
89+
90+
91+
@pytest.fixture(name="send_command_mock")
92+
def mock_send_command(requests_mock):
93+
"""Mock the command reply."""
94+
value = {"cmd": "OK", "ret": "test"}
95+
requests_mock.post(
96+
"http://openevse.test.tld/r?json=1",
97+
text=json.dumps(value),
98+
)
99+
100+
101+
@pytest.fixture(name="send_command_parse_err")
102+
def mock_send_command_parse_err(requests_mock):
103+
"""Mock the command reply parse err."""
104+
requests_mock.post(
105+
"http://openevse.test.tld/r?json=1",
106+
status_code=400,
107+
)
108+
109+
110+
@pytest.fixture(name="send_command_auth_err")
111+
def mock_send_command_auth_err(requests_mock):
112+
"""Mock the command reply auth err."""
113+
requests_mock.post(
114+
"http://openevse.test.tld/r?json=1",
115+
status_code=401,
116+
)
117+
118+
119+
@pytest.fixture(name="send_command_mock_missing")
120+
def mock_send_command_missing(requests_mock):
121+
"""Mock the command reply."""
122+
value = {"cmd": "OK", "what": "test"}
123+
requests_mock.post(
124+
"http://openevse.test.tld/r?json=1",
125+
text=json.dumps(value),
126+
)

tests/test_init.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,38 @@ def test_get_status_auth_err(test_charger_auth_err):
1616
assert test_charger_auth_err is None
1717

1818

19+
def test_send_command(test_charger, send_command_mock):
20+
"""Test v4 Status reply"""
21+
status = test_charger.send_command("test")
22+
assert status == (True, "test")
23+
24+
25+
def test_send_command_missing(test_charger, send_command_mock_missing):
26+
"""Test v4 Status reply"""
27+
status = test_charger.send_command("test")
28+
assert status == (False, "")
29+
30+
31+
def test_send_command_auth(test_charger_auth, send_command_mock):
32+
"""Test v4 Status reply"""
33+
status = test_charger_auth.send_command("test")
34+
assert status == (True, "test")
35+
36+
37+
def test_send_command_parse_err(test_charger_auth, send_command_parse_err):
38+
"""Test v4 Status reply"""
39+
with pytest.raises(openevsehttp.ParseJSONError):
40+
status = test_charger_auth.send_command("test")
41+
assert status is None
42+
43+
44+
def test_send_command_auth_err(test_charger_auth, send_command_auth_err):
45+
"""Test v4 Status reply"""
46+
with pytest.raises(openevsehttp.AuthenticationError):
47+
status = test_charger_auth.send_command("test")
48+
assert status is None
49+
50+
1951
@pytest.mark.parametrize(
2052
"fixture, expected",
2153
[("test_charger", "sleeping"), ("test_charger_v2", "not connected")],

0 commit comments

Comments
 (0)