diff --git a/sunweg/api.py b/sunweg/api.py index 7edc331..65f08dc 100644 --- a/sunweg/api.py +++ b/sunweg/api.py @@ -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.""" diff --git a/tests/test_api.py b/tests/test_api.py index 5561872..5658b8c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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("user@acme.com", "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( @@ -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("") i += 1