Skip to content

Commit

Permalink
Improve unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Moustachauve committed Jan 28, 2024
1 parent c268523 commit f1a5bff
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/technove/technove.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ async def set_auto_charge(self, *, enabled: bool) -> None:
"/station/set/automatic", method="POST", data={"activated": enabled}
)

async def set_charging_enabled(self, *, can_charge: bool) -> None:
async def set_charging_enabled(self, *, enabled: bool) -> None:
"""Set whether the charging station is allowed to provide power or not.
This can only be set if the auto_charge feature is not enabled.
Args:
----
can_charge: True to allow a plugged-in vehicle to charge, otherwise false.
enabled: True to allow a plugged-in vehicle to charge, otherwise false.
Raises:
------
Expand All @@ -177,7 +177,7 @@ async def set_charging_enabled(self, *, can_charge: bool) -> None:
if self.station and self.station.info.auto_charge:
msg = "Cannot start or stop charging when auto-charge is enabled."
raise TechnoVEError(msg)
action = "start" if can_charge else "stop"
action = "start" if enabled else "stop"
await self.request(f"/station/control/{action}")

async def close(self) -> None:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Tests for `technove.TechnoVE`."""


import pytest

from technove import Status, TechnoVEError


def test_status_build() -> None:
"""Test status build with a known status code."""
assert Status.build(67) == Status.PLUGGED_CHARGING


def test_status_build_unknown() -> None:
"""Test status build with an unknown status code."""
with pytest.raises(TechnoVEError):
Status.build(42)
92 changes: 85 additions & 7 deletions tests/test_technove.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def test_json_request(aresponses: ResponsesMockServer) -> None:


@pytest.mark.asyncio
async def test_internal_session(aresponses: ResponsesMockServer) -> None:
async def test_json_request_internal_session(aresponses: ResponsesMockServer) -> None:
"""Test JSON response is handled correctly."""
aresponses.add(
"example.com",
Expand All @@ -47,6 +47,25 @@ async def test_internal_session(aresponses: ResponsesMockServer) -> None:
assert response["status"] == "ok"


@pytest.mark.asyncio
async def test_text_request(aresponses: ResponsesMockServer) -> None:
"""Test plain text response is handled correctly."""
aresponses.add(
"example.com",
"/",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "text/plain"},
text="ok",
),
)
async with aiohttp.ClientSession() as session:
technove = TechnoVE("example.com", session=session)
response = await technove.request("/")
assert response == "ok"


@pytest.mark.asyncio
async def test_post_request(aresponses: ResponsesMockServer) -> None:
"""Test POST requests are handled correctly."""
Expand Down Expand Up @@ -159,8 +178,8 @@ async def test_http_error500(aresponses: ResponsesMockServer) -> None:


@pytest.mark.asyncio
async def test_empty_full_responses(aresponses: ResponsesMockServer) -> None:
"""Test failure handling of full data request TechnoVE device state."""
async def test_update_empty_responses(aresponses: ResponsesMockServer) -> None:
"""Test failure handling of data request TechnoVE device state."""
aresponses.add(
"example.com",
"/station/get/info",
Expand All @@ -171,20 +190,79 @@ async def test_empty_full_responses(aresponses: ResponsesMockServer) -> None:
text="{}",
),
)
async with aiohttp.ClientSession() as session:
technove = TechnoVE("example.com", session=session)
with pytest.raises(TechnoVEError):
await technove.update()


@pytest.mark.asyncio
async def test_update_partial_responses(aresponses: ResponsesMockServer) -> None:
"""Test handling of data request TechnoVE device state."""
aresponses.add(
"example.com",
"/station/get/info",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text="{}",
text='{"name":"testing"}',
),
)
async with aiohttp.ClientSession() as session:
technove = TechnoVE("example.com", session=session)
with pytest.raises(TechnoVEError):
await technove.update()
station = await technove.update()
assert station.info.name == "testing"


@pytest.mark.asyncio
async def test_set_auto_charge(aresponses: ResponsesMockServer) -> None:
"""Test that enabling auto_charge calls the right API."""
aresponses.add(
"example.com",
"/station/set/automatic",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "plain/text"},
text="ok",
),
)
async with aiohttp.ClientSession() as session:
technove = TechnoVE("example.com", session=session)
await technove.set_auto_charge(enabled=True)
aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_set_charging_enabled(aresponses: ResponsesMockServer) -> None:
"""Test that changing charging_enabled calls the right API."""
aresponses.add(
"example.com",
"/station/control/start",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "plain/text"},
text="ok",
),
)
aresponses.add(
"example.com",
"/station/control/stop",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "plain/text"},
text="ok",
),
)
async with aiohttp.ClientSession() as session:
technove = TechnoVE("example.com", session=session)
technove.station = Station({"auto_charge": False})
await technove.set_charging_enabled(enabled=True)
await technove.set_charging_enabled(enabled=False)
aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
Expand All @@ -193,4 +271,4 @@ async def test_set_charging_enabled_auto_charge() -> None:
technove = TechnoVE("example.com")
technove.station = Station({"auto_charge": True})
with pytest.raises(TechnoVEError):
await technove.set_charging_enabled(can_charge=True)
await technove.set_charging_enabled(enabled=True)

0 comments on commit f1a5bff

Please sign in to comment.