Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

146 fix get report datetime issue #147

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)