Skip to content

Commit

Permalink
146 fix get report datetime issue (coddingtonbear#147)
Browse files Browse the repository at this point in the history
* return dict with keys of type date instead of datetime (as per type hint); fix indentation of test

* MFP get_report API (URL) seems to return incorrect numbers or zeroes for dates over 80 days ago. Log a warning in this case.
  • Loading branch information
hannahburkhardt committed Oct 26, 2022
1 parent 989137d commit 934960e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
11 changes: 7 additions & 4 deletions myfitnesspal/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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)
)

Expand Down
52 changes: 26 additions & 26 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 934960e

Please sign in to comment.