Repository navigation
Add update_food_entry tool to edit or move a logged entry #76
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
Closed
paultristanwagner
wants to merge
1
commit into
rwestergren:main
from
paultristanwagner:edit-diary-entry
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| """update_serving: change a logged serving's amount, measure or meal group. | ||
|
|
||
| The serving is read back from get_diary and re-sent whole to | ||
| /api/v2/edit_serving. The meal group is the high 16 bits of `order`. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from datetime import date | ||
|
|
||
| import pytest | ||
| from test_client import make_cold_client | ||
|
|
||
| from cronometer_api_mcp.client import CronometerError | ||
|
|
||
| DAY = date(1900, 1, 2) | ||
| SERVING_ID = 5312202908 | ||
|
|
||
|
|
||
| def serving(**overrides) -> dict: | ||
| """A get_diary entry shaped like the real one.""" | ||
| entry = { | ||
| "servingId": SERVING_ID, | ||
| "type": "Serving", | ||
| "foodId": 5442, | ||
| "measureId": 14990, | ||
| "translationId": 0, | ||
| "grams": 100, | ||
| "day": "1900-01-02", | ||
| "time": "11:23:50", | ||
| "offset": 0, | ||
| "userId": 42, | ||
| "order": (1 << 16) | 1, # breakfast, first | ||
| } | ||
| entry.update(overrides) | ||
| return entry | ||
|
|
||
|
|
||
| def diary(*entries) -> dict: | ||
| return {"diary": list(entries), "summary": {}, "messages": []} | ||
|
|
||
|
|
||
| EDITED = {"messages": [], "id": SERVING_ID} | ||
|
|
||
|
|
||
| def test_update_serving_changes_grams_and_keeps_the_rest(tmp_path): | ||
| client, state = make_cold_client(tmp_path, [diary(serving()), EDITED]) | ||
|
|
||
| result = client.update_serving(SERVING_ID, DAY, grams=150) | ||
|
|
||
| sent = state["payloads"][1] | ||
| assert sent["config"] == {"call_version": 2} | ||
| assert sent["serving"] == serving(grams=150) | ||
| assert result == serving(grams=150) | ||
|
|
||
|
|
||
| def test_update_serving_moves_to_end_of_new_group(tmp_path): | ||
| lunch = [ | ||
| serving(servingId=1, order=(2 << 16) | 1), | ||
| serving(servingId=2, order=(2 << 16) | 4), | ||
| ] | ||
| client, state = make_cold_client(tmp_path, [diary(serving(), *lunch), EDITED]) | ||
|
|
||
| client.update_serving(str(SERVING_ID), DAY, diary_group=2) | ||
|
|
||
| assert state["payloads"][1]["serving"]["order"] == (2 << 16) | 5 | ||
|
|
||
|
|
||
| def test_update_serving_same_group_keeps_position(tmp_path): | ||
| entry = serving(order=(1 << 16) | 3) | ||
| client, state = make_cold_client(tmp_path, [diary(entry), EDITED]) | ||
|
|
||
| client.update_serving(SERVING_ID, DAY, diary_group=1, measure_id=7) | ||
|
|
||
| sent = state["payloads"][1]["serving"] | ||
| assert sent["order"] == (1 << 16) | 3 | ||
| assert sent["measureId"] == 7 | ||
|
|
||
|
|
||
| def test_update_serving_unknown_id_raises_without_editing(tmp_path): | ||
| client, state = make_cold_client(tmp_path, [diary(serving()), EDITED]) | ||
|
|
||
| with pytest.raises(CronometerError, match="No serving 999"): | ||
| client.update_serving(999, DAY, grams=10) | ||
|
|
||
| assert len(state["payloads"]) == 1 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("kwargs", "message"), | ||
| [ | ||
| ({}, "needs grams"), | ||
| ({"grams": 0}, "positive"), | ||
| ({"diary_group": 5}, "1-4"), | ||
| ], | ||
| ) | ||
| def test_update_serving_validates_before_any_request(tmp_path, kwargs, message): | ||
| client, state = make_cold_client(tmp_path, [diary(serving()), EDITED]) | ||
|
|
||
| with pytest.raises(ValueError, match=message): | ||
| client.update_serving(SERVING_ID, DAY, **kwargs) | ||
|
|
||
| assert state["payloads"] == [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| "remove_food_entry", | ||
| "search_foods", | ||
| "update_custom_food", | ||
| "update_food_entry", | ||
| } | ||
|
|
||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cronometer's API uses
gramsmisleadingly - for regular foods and weight-based recipes, it holds actual grams.For serving-based recipes, it holds a reference-serving count instead. Editing such an entry to grams=100 therefore sends 100 reference servings.
Take a look at
enrich_diary_servingswhich already handles this. It may be useful to extract that shared measure logic to a small helper.