Skip to content

Commit

Permalink
test(stats): improved coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rokam committed Dec 26, 2023
1 parent 5dd93bd commit 321e3b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sunweg/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ def month_stats_production(self, year: int, month: int, plant_id: int, inverter_
except LoginError:
if retry:
self.authenticate()
self.month_stats_production(year, month, plant_id, inverter_id, False)
return self.month_stats_production(year, month, plant_id, inverter_id, False)
return []

def _populate_MPPT(self, result: dict, inverter: Inverter) -> None:
"""Populate MPPT information inside a inverter."""
Expand Down
17 changes: 17 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,22 @@ def test_month_stats_fail(self) -> None:
api.month_stats_production(2013, 12, plant)
assert e_info.value.__str__() == "Error message"

def test_month_stats_401(self) -> None:
"""Test month stats with data from server with expired token."""
with patch(
"requests.Session.post",
return_value=self.responses["auth_success_response.json"],
), patch(
"requests.Session.get",
return_value=self.responses["error_401_response.txt"],
):
api = APIHelper("[email protected]", "password")
plant = MagicMock()
plant.id = 1
stats = api.month_stats_production(2023, 12, plant)
assert isinstance(stats, list)
assert len(stats) == 0

def test_month_stats_success(self) -> None:
"""Test month stats with data from server."""
with patch(
Expand All @@ -368,4 +384,5 @@ def test_month_stats_success(self) -> None:
assert stat.date == date(2023, 12, i)
assert isinstance(stat.production, float)
assert stat.prognostic == 98.774193548387
assert stat.__str__().startswith("<class 'sunweg.util.ProductionStats'>")
i += 1

0 comments on commit 321e3b4

Please sign in to comment.