diff --git a/myfitnesspal/client.py b/myfitnesspal/client.py index b46c72b..dd93ca7 100644 --- a/myfitnesspal/client.py +++ b/myfitnesspal/client.py @@ -704,6 +704,9 @@ def get_report( """ Returns report data of a given name and category between two dates. """ + if (datetime.date.today()-lower_bound).days > 80: + logger.warning(f"Report API may not be able to look back this far. Some results may be incorrect.") + upper_bound, lower_bound = self._ensure_upper_lower_bound( lower_bound, upper_bound ) @@ -748,13 +751,13 @@ def _get_report_data(self, json_data: dict) -> Dict[datetime.date, float]: if not data: return report_data - for index, entry in enumerate(json_data["data"]): + for index, entry in enumerate(data): # Dates are returned without year. # As the returned dates will always begin from the current day, the - # correct date can be determined using the entries index + # correct date can be determined using the entry's index date = ( - datetime.datetime.today() - - datetime.timedelta(days=len(json_data["data"])) + datetime.date.today() + - datetime.timedelta(days=len(data)) + datetime.timedelta(days=index + 1) ) diff --git a/tests/test_client.py b/tests/test_client.py index 83630d7..50c28f1 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -438,31 +438,31 @@ def test_get_completed_day(self): True, ) - def test_get_report(self): - with patch.object(self.client, "_get_json_for_url") as get_doc: - get_doc.return_value = self.get_json_data( - "report_nutrition_net_calories.json" - ) - actual_measurements = self.client.get_report( - report_name="Net Calories", - report_category="Nutrition", - lower_bound=datetime.date.today() - datetime.timedelta(days=4), - ) - - # Dates are determined based on the assumption that the results will - # always start from the current day. Dates held in sample data file are - # therefore irrelevant for this test. - - expected_measurements = OrderedDict( - sorted( - [ - (datetime.date.today(), 425.0), - (datetime.date.today() - datetime.timedelta(days=1), 1454.0), - (datetime.date.today() - datetime.timedelta(days=2), 1451.0), - (datetime.date.today() - datetime.timedelta(days=3), 1489.0), - (datetime.date.today() - datetime.timedelta(days=4), 1390.0), - ] - ) + def test_get_report(self): + with patch.object(self.client, "_get_json_for_url") as get_doc: + get_doc.return_value = self.get_json_data( + "report_nutrition_net_calories.json" ) + actual_measurements = self.client.get_report( + report_name="Net Calories", + report_category="Nutrition", + lower_bound=datetime.date.today() - datetime.timedelta(days=4), + ) + + # Dates are determined based on the assumption that the results will + # always start from the current day. Dates held in sample data file are + # therefore irrelevant for this test. + + expected_measurements = OrderedDict( + sorted( + [ + (datetime.date.today(), 425.0), + (datetime.date.today() - datetime.timedelta(days=1), 1454.0), + (datetime.date.today() - datetime.timedelta(days=2), 1451.0), + (datetime.date.today() - datetime.timedelta(days=3), 1489.0), + (datetime.date.today() - datetime.timedelta(days=4), 1390.0), + ] + ) + ) - self.assertEqual(expected_measurements, actual_measurements) + self.assertEqual(expected_measurements, actual_measurements)