cronometer-api-mcp 0.2.4__tar.gz → 0.2.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/PKG-INFO +1 -1
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/pyproject.toml +1 -1
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/server.json +2 -2
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/src/cronometer_api_mcp/client.py +61 -10
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/src/cronometer_api_mcp/server.py +28 -6
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/tests/test_client.py +50 -7
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/tests/test_custom_food_edit.py +174 -1
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/uv.lock +24 -24
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/.dockerignore +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/.env.example +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/.github/dependabot.yml +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/.github/workflows/ci.yml +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/.github/workflows/docker.yml +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/.github/workflows/publish.yml +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/.gitignore +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/.python-version +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/Dockerfile +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/LICENSE +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/README.md +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/src/cronometer_api_mcp/__init__.py +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/tests/test_relative_dates.py +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/tests/test_server_import.py +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/tests/test_timezone.py +0 -0
- {cronometer_api_mcp-0.2.4 → cronometer_api_mcp-0.2.5}/tests/test_totp.py +0 -0
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
"url": "https://github.com/rwestergren/cronometer-api-mcp",
|
|
7
7
|
"source": "github"
|
|
8
8
|
},
|
|
9
|
-
"version": "0.2.
|
|
9
|
+
"version": "0.2.5",
|
|
10
10
|
"packages": [
|
|
11
11
|
{
|
|
12
12
|
"registryType": "pypi",
|
|
13
13
|
"identifier": "cronometer-api-mcp",
|
|
14
|
-
"version": "0.2.
|
|
14
|
+
"version": "0.2.5",
|
|
15
15
|
"transport": {
|
|
16
16
|
"type": "stdio"
|
|
17
17
|
},
|
|
@@ -15,7 +15,9 @@ import hashlib
|
|
|
15
15
|
import hmac
|
|
16
16
|
import json
|
|
17
17
|
import logging
|
|
18
|
+
import math
|
|
18
19
|
import os
|
|
20
|
+
import re
|
|
19
21
|
import struct
|
|
20
22
|
import threading
|
|
21
23
|
import time
|
|
@@ -82,6 +84,9 @@ _OZ_GRAMS = 28.3495231
|
|
|
82
84
|
# Nutrient IDs create_custom_food() already writes via its named macro args
|
|
83
85
|
# (including the derived/negative-ID duplicates it sends alongside them).
|
|
84
86
|
# extra_nutrients must not reuse one of these -- see create_custom_food().
|
|
87
|
+
# A serving name that leads with a plain number, e.g. "2 cookies".
|
|
88
|
+
_QUANTITY_LED_NAME = re.compile(r"^\s*(\d+(?:\.\d+)?)\s+(\S.*)$")
|
|
89
|
+
|
|
85
90
|
_RESERVED_CUSTOM_FOOD_NUTRIENT_IDS = frozenset(
|
|
86
91
|
{
|
|
87
92
|
NUTRIENT_IDS["energy"],
|
|
@@ -825,6 +830,7 @@ class CronometerClient:
|
|
|
825
830
|
extra_nutrients: dict[int, float] | None = None,
|
|
826
831
|
serving_name: str | None = None,
|
|
827
832
|
serving_grams: float | None = None,
|
|
833
|
+
serving_quantity: float | None = None,
|
|
828
834
|
) -> dict:
|
|
829
835
|
"""Edit a user-created custom food in place.
|
|
830
836
|
|
|
@@ -835,6 +841,16 @@ class CronometerClient:
|
|
|
835
841
|
alone leaves the stored per-100g values as they are, so the food's
|
|
836
842
|
per-serving numbers shift with the new weight.
|
|
837
843
|
|
|
844
|
+
A measure is `amount` units of `name` weighing `value` grams in total,
|
|
845
|
+
and the app renders the serving as "<amount> <name>". Setting the
|
|
846
|
+
weight therefore also resets the quantity (to serving_quantity, or 1)
|
|
847
|
+
and makes the measure a named ("Atomic") serving. Otherwise a food
|
|
848
|
+
first saved as "400 g" keeps amount=400 and type "Weight" and shows up
|
|
849
|
+
as "400 <serving_name>" with no weight of its own. A quantity leading
|
|
850
|
+
serving_name ("2 cookies") is moved into the quantity for the same
|
|
851
|
+
reason. A quantity set without serving_grams keeps the total weight,
|
|
852
|
+
so the per-unit weight changes.
|
|
853
|
+
|
|
838
854
|
Returns {"food_id": int, "name": str}.
|
|
839
855
|
"""
|
|
840
856
|
if extra_nutrients:
|
|
@@ -846,6 +862,26 @@ class CronometerClient:
|
|
|
846
862
|
f"those instead."
|
|
847
863
|
)
|
|
848
864
|
|
|
865
|
+
# The server splits a leading quantity off a new measure's name
|
|
866
|
+
# ("10 pieces" is stored as amount=10, name="pieces") but not when
|
|
867
|
+
# an existing measure is re-sent, so do the same here. Done before
|
|
868
|
+
# validation so an inferred quantity is checked too.
|
|
869
|
+
quantity_label = "serving_quantity"
|
|
870
|
+
if serving_name is not None:
|
|
871
|
+
led = _QUANTITY_LED_NAME.match(serving_name)
|
|
872
|
+
if led and float(led.group(1)) > 0:
|
|
873
|
+
serving_name = led.group(2)
|
|
874
|
+
if serving_quantity is None:
|
|
875
|
+
serving_quantity = float(led.group(1))
|
|
876
|
+
quantity_label = "serving_name quantity"
|
|
877
|
+
|
|
878
|
+
for label, v in (
|
|
879
|
+
("serving_grams", serving_grams),
|
|
880
|
+
(quantity_label, serving_quantity),
|
|
881
|
+
):
|
|
882
|
+
if v is not None and not (math.isfinite(v) and v > 0):
|
|
883
|
+
raise ValueError(f"{label} must be a positive number, got {v!r}")
|
|
884
|
+
|
|
849
885
|
with self._food_lock(food_id):
|
|
850
886
|
food = self._get_custom_food(food_id)
|
|
851
887
|
|
|
@@ -862,6 +898,11 @@ class CronometerClient:
|
|
|
862
898
|
measure["name"] = serving_name
|
|
863
899
|
if serving_grams is not None:
|
|
864
900
|
measure["value"] = serving_grams
|
|
901
|
+
if serving_grams is not None or serving_quantity is not None:
|
|
902
|
+
measure["amount"] = (
|
|
903
|
+
serving_quantity if serving_quantity is not None else 1.0
|
|
904
|
+
)
|
|
905
|
+
measure["type"] = "Atomic"
|
|
865
906
|
grams = (measure or {}).get("value") or 100.0
|
|
866
907
|
scale = 100.0 / grams if grams > 0 else 1.0
|
|
867
908
|
|
|
@@ -1642,9 +1683,11 @@ class CronometerClient:
|
|
|
1642
1683
|
merges per-entry:
|
|
1643
1684
|
|
|
1644
1685
|
- name, source, category: from the food object
|
|
1645
|
-
-
|
|
1646
|
-
measureId (falls back to
|
|
1647
|
-
|
|
1686
|
+
- serving_size: {measure_id, quantity, unit, grams} defining one
|
|
1687
|
+
portion in the entry's measureId (falls back to defaultMeasureId).
|
|
1688
|
+
Recipe measures have null grams: their value is a reference-serving
|
|
1689
|
+
count, not a gram weight.
|
|
1690
|
+
- servings: number of those portions consumed, when derivable
|
|
1648
1691
|
- nutrients: the food's nutrient profile scaled to the entry's amount
|
|
1649
1692
|
(per-100g for Weight/Atomic measures, per-serving for Recipe
|
|
1650
1693
|
measures), labeled with name/unit/category via the nutrient
|
|
@@ -1705,18 +1748,26 @@ class CronometerClient:
|
|
|
1705
1748
|
)
|
|
1706
1749
|
grams = entry.get("grams")
|
|
1707
1750
|
if measure:
|
|
1708
|
-
|
|
1709
|
-
|
|
1751
|
+
portion_value = measure.get("value")
|
|
1752
|
+
quantity = measure.get("amount", 1)
|
|
1753
|
+
if not (
|
|
1754
|
+
isinstance(quantity, (int, float))
|
|
1755
|
+
and math.isfinite(quantity)
|
|
1756
|
+
and quantity > 0
|
|
1757
|
+
):
|
|
1758
|
+
quantity = None
|
|
1759
|
+
entry["serving_size"] = {
|
|
1710
1760
|
"measure_id": measure.get("id"),
|
|
1711
|
-
"
|
|
1712
|
-
"
|
|
1761
|
+
"quantity": quantity,
|
|
1762
|
+
"unit": measure.get("name"),
|
|
1763
|
+
"grams": None if measure.get("type") == "Recipe" else portion_value,
|
|
1713
1764
|
}
|
|
1714
1765
|
if (
|
|
1715
1766
|
isinstance(grams, (int, float))
|
|
1716
|
-
and isinstance(
|
|
1717
|
-
and
|
|
1767
|
+
and isinstance(portion_value, (int, float))
|
|
1768
|
+
and portion_value
|
|
1718
1769
|
):
|
|
1719
|
-
entry["servings"] = round(grams /
|
|
1770
|
+
entry["servings"] = round(grams / portion_value, 4)
|
|
1720
1771
|
|
|
1721
1772
|
# Nutrient scaling depends on the measure type:
|
|
1722
1773
|
# - Recipe measures: nutrients are stored per one reference
|
|
@@ -175,10 +175,18 @@ def get_food_log(
|
|
|
175
175
|
entries, prefer get_daily_nutrition(days=N).
|
|
176
176
|
|
|
177
177
|
Returns every food entry logged for the day. Each "Serving" entry is
|
|
178
|
-
enriched (best-effort) with the food's name, source,
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
178
|
+
enriched (best-effort) with the food's name, source, serving_size, servings,
|
|
179
|
+
and that food's own nutrient profile scaled to the amount eaten. Non-food
|
|
180
|
+
entries (exercise, biometrics) carry their own name.
|
|
181
|
+
|
|
182
|
+
serving_size defines one portion: quantity units weighing grams in total.
|
|
183
|
+
servings is how many of those portions were consumed. For example,
|
|
184
|
+
serving_size={quantity: 2, unit: "cookies", grams: 30} with servings=1
|
|
185
|
+
means two cookies eaten; servings=0.5 means one cookie eaten. The size
|
|
186
|
+
uses the entry's selected measure, or the food's default as a fallback.
|
|
187
|
+
For serving-based recipes, serving_size.grams is null because the API
|
|
188
|
+
uses reference-serving counts instead of gram weights. An invalid
|
|
189
|
+
serving_size.quantity is reported as null.
|
|
182
190
|
|
|
183
191
|
Note: the per-entry "nutrients" are each food's individual contribution,
|
|
184
192
|
which is distinct from the day-level nutrition_summary aggregate below.
|
|
@@ -544,6 +552,8 @@ def get_food_details(food_id: int) -> str:
|
|
|
544
552
|
"measure_id": m.get("id"),
|
|
545
553
|
"name": m.get("name"),
|
|
546
554
|
"grams": m.get("value"),
|
|
555
|
+
"quantity": m.get("amount"),
|
|
556
|
+
"type": m.get("type"),
|
|
547
557
|
}
|
|
548
558
|
)
|
|
549
559
|
|
|
@@ -652,6 +662,7 @@ def update_custom_food(
|
|
|
652
662
|
extra_nutrients: dict[int, float] | None = None,
|
|
653
663
|
serving_name: str | None = None,
|
|
654
664
|
serving_grams: float | None = None,
|
|
665
|
+
serving_quantity: float | None = None,
|
|
655
666
|
) -> str:
|
|
656
667
|
"""Edit an existing custom food (one you created) in place.
|
|
657
668
|
|
|
@@ -680,8 +691,18 @@ def update_custom_food(
|
|
|
680
691
|
extra_nutrients: Additional nutrients keyed by Cronometer nutrient ID
|
|
681
692
|
(from get_daily_nutrition) and valued per serving; must not reuse
|
|
682
693
|
an ID the named args already cover.
|
|
683
|
-
serving_name: New name for the default serving.
|
|
684
|
-
|
|
694
|
+
serving_name: New name for the default serving. The app shows the
|
|
695
|
+
serving as "<quantity> <name>"; a leading number ("2 cookies")
|
|
696
|
+
is taken as the quantity (see serving_quantity).
|
|
697
|
+
serving_grams: New total weight of the default serving in grams.
|
|
698
|
+
Also resets the quantity to serving_quantity (or 1).
|
|
699
|
+
serving_quantity: How many units make up the default serving
|
|
700
|
+
(default 1 whenever serving_grams is passed). Without
|
|
701
|
+
serving_grams the serving's total weight stays the same, so each
|
|
702
|
+
unit gets lighter or heavier: changing "2 cookies = 30 g" to 3
|
|
703
|
+
gives "3 cookies = 30 g". To keep the per-unit weight, also pass
|
|
704
|
+
serving_grams (45 here). Check the result with get_food_details,
|
|
705
|
+
which lists each measure's quantity.
|
|
685
706
|
"""
|
|
686
707
|
try:
|
|
687
708
|
client = _get_client()
|
|
@@ -699,6 +720,7 @@ def update_custom_food(
|
|
|
699
720
|
extra_nutrients=extra_nutrients,
|
|
700
721
|
serving_name=serving_name,
|
|
701
722
|
serving_grams=serving_grams,
|
|
723
|
+
serving_quantity=serving_quantity,
|
|
702
724
|
)
|
|
703
725
|
return _ok({"food_id": result["food_id"], "name": result["name"]})
|
|
704
726
|
except Exception as e:
|
|
@@ -270,10 +270,11 @@ def test_enrich_diary_merges_names_measures_and_scaled_nutrients(tmp_path):
|
|
|
270
270
|
assert oats["name"] == "Oats"
|
|
271
271
|
assert oats["source"] == "USDA"
|
|
272
272
|
assert oats["category"] == "Grains"
|
|
273
|
-
assert oats["
|
|
273
|
+
assert oats["serving_size"] == {
|
|
274
274
|
"measure_id": 10,
|
|
275
|
-
"
|
|
276
|
-
"
|
|
275
|
+
"quantity": 1,
|
|
276
|
+
"unit": "cup",
|
|
277
|
+
"grams": 100.0,
|
|
277
278
|
}
|
|
278
279
|
assert oats["servings"] == 2.0 # 200g / 100g per cup
|
|
279
280
|
# per-100g energy 389 scaled to 200g -> 778
|
|
@@ -284,15 +285,20 @@ def test_enrich_diary_merges_names_measures_and_scaled_nutrients(tmp_path):
|
|
|
284
285
|
|
|
285
286
|
milk = entries[1]
|
|
286
287
|
# unknown measureId 999 -> fell back to defaultMeasureId 20
|
|
287
|
-
assert milk["
|
|
288
|
-
assert milk["
|
|
288
|
+
assert milk["serving_size"]["measure_id"] == 20
|
|
289
|
+
assert milk["serving_size"]["unit"] == "glass"
|
|
289
290
|
# 42 kcal/100g scaled to 50g -> 21
|
|
290
291
|
assert next(n for n in milk["nutrients"] if n["id"] == 208)["amount"] == 21.0
|
|
291
292
|
|
|
292
293
|
recipe = entries[2]
|
|
293
294
|
assert recipe["name"] == "Recipe Food"
|
|
294
|
-
assert recipe["
|
|
295
|
-
|
|
295
|
+
assert recipe["serving_size"] == {
|
|
296
|
+
"measure_id": 30,
|
|
297
|
+
"quantity": 1,
|
|
298
|
+
"unit": "serving",
|
|
299
|
+
"grams": None,
|
|
300
|
+
}
|
|
301
|
+
assert recipe["servings"] == 1.1
|
|
296
302
|
# Recipe measure: nutrients are per-serving and "grams" is a serving count,
|
|
297
303
|
# so 708.538 kcal/serving * 1.1 servings -> 779.39 (not grams/100)
|
|
298
304
|
energy = next(n for n in recipe["nutrients"] if n["id"] == 208)
|
|
@@ -304,6 +310,43 @@ def test_enrich_diary_merges_names_measures_and_scaled_nutrients(tmp_path):
|
|
|
304
310
|
assert entries[3] == {"type": "Exercise", "name": "Running", "order": 1}
|
|
305
311
|
|
|
306
312
|
|
|
313
|
+
@pytest.mark.parametrize(
|
|
314
|
+
("measure_type", "quantity", "value", "expected_quantity", "servings"),
|
|
315
|
+
[
|
|
316
|
+
("Weight", 100, 100, 100, 0.6),
|
|
317
|
+
("Atomic", 0.5, 120, 0.5, 0.5),
|
|
318
|
+
("Atomic", None, 120, None, 0.5),
|
|
319
|
+
("Atomic", 0, 120, None, 0.5),
|
|
320
|
+
("Atomic", -1, 120, None, 0.5),
|
|
321
|
+
("Atomic", "bad", 120, None, 0.5),
|
|
322
|
+
("Atomic", float("nan"), 120, None, 0.5),
|
|
323
|
+
("Atomic", float("inf"), 120, None, 0.5),
|
|
324
|
+
],
|
|
325
|
+
)
|
|
326
|
+
def test_enrich_diary_measure_quantity(
|
|
327
|
+
tmp_path, monkeypatch, measure_type, quantity, value, expected_quantity, servings
|
|
328
|
+
):
|
|
329
|
+
client = _enrich_client(tmp_path)
|
|
330
|
+
food = {
|
|
331
|
+
"id": 100,
|
|
332
|
+
"defaultMeasureId": 10,
|
|
333
|
+
"measures": [
|
|
334
|
+
{"id": 10, "type": measure_type, "amount": quantity, "value": value}
|
|
335
|
+
],
|
|
336
|
+
}
|
|
337
|
+
monkeypatch.setattr(client, "get_foods", lambda ids: [food])
|
|
338
|
+
monkeypatch.setattr(client, "get_nutrient_definitions", dict)
|
|
339
|
+
|
|
340
|
+
out = client.enrich_diary_servings(
|
|
341
|
+
{"diary": [{"type": "Serving", "foodId": 100, "measureId": 999, "grams": 60}]}
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
entry = out["diary"][0]
|
|
345
|
+
assert entry["serving_size"]["quantity"] == expected_quantity
|
|
346
|
+
assert entry["serving_size"]["grams"] == value
|
|
347
|
+
assert entry["servings"] == servings
|
|
348
|
+
|
|
349
|
+
|
|
307
350
|
def test_enrich_diary_is_best_effort_when_get_foods_fails(tmp_path):
|
|
308
351
|
client = _enrich_client(tmp_path)
|
|
309
352
|
|
|
@@ -162,11 +162,184 @@ def test_update_custom_food_serving_overrides_measure_and_scale(tmp_path):
|
|
|
162
162
|
data = state["payloads"][1]["data"]
|
|
163
163
|
(measure,) = data["measures"]
|
|
164
164
|
assert measure["id"] == 900
|
|
165
|
-
assert measure["name"] == "1
|
|
165
|
+
assert measure["name"] == "cup" # the leading 1 is the quantity
|
|
166
|
+
assert measure["amount"] == 1.0
|
|
166
167
|
assert measure["value"] == 200
|
|
167
168
|
assert nutrients_by_id(state["payloads"][1])[208] == 200.0 # 400 per 200 g
|
|
168
169
|
|
|
169
170
|
|
|
171
|
+
def weight_default_food() -> dict:
|
|
172
|
+
"""A food first saved as "400 g": its default measure is 400 units of a
|
|
173
|
+
Weight measure, which the app renders as "400 <name>"."""
|
|
174
|
+
food = custom_food()
|
|
175
|
+
food["measures"][0].update(name="1 serving", amount=400, value=250, type="Weight")
|
|
176
|
+
return food
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def test_update_custom_food_serving_grams_resets_quantity_and_type(tmp_path):
|
|
180
|
+
"""Setting the weight makes the default measure one named serving, so a
|
|
181
|
+
food first saved as "400 g" stops rendering as "400 <name>"."""
|
|
182
|
+
client, state = make_cold_client(tmp_path, [weight_default_food(), OK])
|
|
183
|
+
|
|
184
|
+
client.update_custom_food(FOOD_ID, serving_name="serving", serving_grams=250)
|
|
185
|
+
|
|
186
|
+
(measure,) = state["payloads"][1]["data"]["measures"]
|
|
187
|
+
assert measure["id"] == 900
|
|
188
|
+
assert measure["name"] == "serving"
|
|
189
|
+
assert measure["value"] == 250
|
|
190
|
+
assert measure["amount"] == 1.0
|
|
191
|
+
assert measure["type"] == "Atomic"
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def test_update_custom_food_serving_quantity(tmp_path):
|
|
195
|
+
"""serving_quantity sets how many units the default serving is, alone or
|
|
196
|
+
together with the weight."""
|
|
197
|
+
client, state = make_cold_client(
|
|
198
|
+
tmp_path, [weight_default_food(), OK, custom_food(), OK]
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
client.update_custom_food(FOOD_ID, serving_quantity=1)
|
|
202
|
+
(measure,) = state["payloads"][1]["data"]["measures"]
|
|
203
|
+
assert (measure["amount"], measure["value"], measure["type"]) == (1, 250, "Atomic")
|
|
204
|
+
|
|
205
|
+
client.update_custom_food(FOOD_ID, serving_grams=30, serving_quantity=2)
|
|
206
|
+
(measure,) = state["payloads"][3]["data"]["measures"]
|
|
207
|
+
assert (measure["amount"], measure["value"]) == (2, 30)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
@pytest.mark.parametrize(
|
|
211
|
+
"kwargs",
|
|
212
|
+
[
|
|
213
|
+
{"serving_name": "2 cookies"},
|
|
214
|
+
{"serving_name": "cookies", "serving_quantity": 2},
|
|
215
|
+
],
|
|
216
|
+
)
|
|
217
|
+
@pytest.mark.parametrize(
|
|
218
|
+
("measure_id", "quantity", "unit", "grams", "counts"),
|
|
219
|
+
[
|
|
220
|
+
(900, 2, "cookies", 30, (0.5, 1, 1.5)),
|
|
221
|
+
(901, 1, "cookie", 15, (1, 2, 3)),
|
|
222
|
+
],
|
|
223
|
+
)
|
|
224
|
+
def test_update_custom_food_serving_size_in_diary(
|
|
225
|
+
tmp_path, monkeypatch, kwargs, measure_id, quantity, unit, grams, counts
|
|
226
|
+
):
|
|
227
|
+
food = custom_food()
|
|
228
|
+
food["measures"].append(
|
|
229
|
+
{"id": 901, "name": "cookie", "amount": 1, "value": 15, "type": "Atomic"}
|
|
230
|
+
)
|
|
231
|
+
client, state = make_cold_client(tmp_path, [food, OK])
|
|
232
|
+
client.update_custom_food(FOOD_ID, serving_grams=30, **kwargs)
|
|
233
|
+
|
|
234
|
+
saved = state["payloads"][1]["data"]
|
|
235
|
+
monkeypatch.setattr(client, "get_foods", lambda ids: [saved])
|
|
236
|
+
monkeypatch.setattr(client, "get_nutrient_definitions", dict)
|
|
237
|
+
diary = client.enrich_diary_servings(
|
|
238
|
+
{
|
|
239
|
+
"diary": [
|
|
240
|
+
{
|
|
241
|
+
"type": "Serving",
|
|
242
|
+
"foodId": FOOD_ID,
|
|
243
|
+
"measureId": measure_id,
|
|
244
|
+
"grams": consumed_grams,
|
|
245
|
+
}
|
|
246
|
+
for consumed_grams in (15, 30, 45)
|
|
247
|
+
]
|
|
248
|
+
}
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
for entry, count, calories in zip(diary["diary"], counts, (30, 60, 90)):
|
|
252
|
+
assert entry["serving_size"] == {
|
|
253
|
+
"measure_id": measure_id,
|
|
254
|
+
"quantity": quantity,
|
|
255
|
+
"unit": unit,
|
|
256
|
+
"grams": grams,
|
|
257
|
+
}
|
|
258
|
+
assert entry["servings"] == count
|
|
259
|
+
assert "measure" not in entry
|
|
260
|
+
assert "quantity" not in entry
|
|
261
|
+
energy = next(n for n in entry["nutrients"] if n["id"] == 208)
|
|
262
|
+
assert energy["amount"] == calories
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@pytest.mark.parametrize(
|
|
266
|
+
("given", "kwargs", "name", "amount"),
|
|
267
|
+
[
|
|
268
|
+
("1 serving", {}, "serving", 1.0),
|
|
269
|
+
("10 pieces", {}, "pieces", 10.0),
|
|
270
|
+
("1.5 cups", {}, "cups", 1.5),
|
|
271
|
+
("2 cookies", {"serving_quantity": 3}, "cookies", 3),
|
|
272
|
+
("1/2 cup", {"serving_grams": 120}, "1/2 cup", 1.0),
|
|
273
|
+
("0 calorie syrup", {"serving_grams": 15}, "0 calorie syrup", 1.0),
|
|
274
|
+
],
|
|
275
|
+
)
|
|
276
|
+
def test_update_custom_food_quantity_led_serving_name(
|
|
277
|
+
tmp_path, given, kwargs, name, amount
|
|
278
|
+
):
|
|
279
|
+
"""A plain number leading serving_name becomes the quantity, as the server
|
|
280
|
+
does for a new measure; an explicit serving_quantity wins."""
|
|
281
|
+
client, state = make_cold_client(tmp_path, [weight_default_food(), OK])
|
|
282
|
+
|
|
283
|
+
client.update_custom_food(FOOD_ID, serving_name=given, **kwargs)
|
|
284
|
+
|
|
285
|
+
(measure,) = state["payloads"][1]["data"]["measures"]
|
|
286
|
+
assert (measure["name"], measure["amount"], measure["type"]) == (
|
|
287
|
+
name,
|
|
288
|
+
amount,
|
|
289
|
+
"Atomic",
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def test_update_custom_food_serving_name_alone_keeps_quantity(tmp_path):
|
|
294
|
+
client, state = make_cold_client(tmp_path, [weight_default_food(), OK])
|
|
295
|
+
|
|
296
|
+
client.update_custom_food(FOOD_ID, serving_name="portion")
|
|
297
|
+
|
|
298
|
+
(measure,) = state["payloads"][1]["data"]["measures"]
|
|
299
|
+
assert (measure["name"], measure["amount"], measure["type"]) == (
|
|
300
|
+
"portion",
|
|
301
|
+
400,
|
|
302
|
+
"Weight",
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
@pytest.mark.parametrize("field", ["serving_grams", "serving_quantity"])
|
|
307
|
+
@pytest.mark.parametrize("bad", [0, -1, float("nan"), float("inf")])
|
|
308
|
+
def test_update_custom_food_rejects_bad_serving_numbers(tmp_path, field, bad):
|
|
309
|
+
client, state = make_cold_client(tmp_path, [])
|
|
310
|
+
|
|
311
|
+
with pytest.raises(ValueError, match=field):
|
|
312
|
+
client.update_custom_food(FOOD_ID, **{field: bad})
|
|
313
|
+
assert state["payloads"] == []
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def test_update_custom_food_rejects_overflowing_name_quantity(tmp_path):
|
|
317
|
+
"""A leading number too large for a float would become inf; it is
|
|
318
|
+
rejected before anything is fetched or saved."""
|
|
319
|
+
client, state = make_cold_client(tmp_path, [])
|
|
320
|
+
|
|
321
|
+
with pytest.raises(ValueError, match="serving_name quantity"):
|
|
322
|
+
client.update_custom_food(FOOD_ID, serving_name="9" * 400 + " cookies")
|
|
323
|
+
assert state["payloads"] == []
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
def test_update_custom_food_quantity_alone_keeps_total_weight(tmp_path):
|
|
327
|
+
"""Without serving_grams, a new quantity keeps the serving's total weight:
|
|
328
|
+
"2 cookies = 30 g" becomes "3 cookies = 30 g"."""
|
|
329
|
+
food = custom_food()
|
|
330
|
+
food["measures"][0].update(name="cookies", amount=2, value=30)
|
|
331
|
+
client, state = make_cold_client(tmp_path, [food, OK])
|
|
332
|
+
|
|
333
|
+
client.update_custom_food(FOOD_ID, serving_quantity=3)
|
|
334
|
+
|
|
335
|
+
(measure,) = state["payloads"][1]["data"]["measures"]
|
|
336
|
+
assert (measure["name"], measure["amount"], measure["value"]) == (
|
|
337
|
+
"cookies",
|
|
338
|
+
3,
|
|
339
|
+
30,
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
|
|
170
343
|
def test_update_custom_food_extra_nutrients(tmp_path):
|
|
171
344
|
"""extra_nutrients are set (scaled) by id, replacing an existing entry or
|
|
172
345
|
appending a new one, and may not overlap the named macro args."""
|
|
@@ -476,11 +476,11 @@ wheels = [
|
|
|
476
476
|
|
|
477
477
|
[[package]]
|
|
478
478
|
name = "python-dotenv"
|
|
479
|
-
version = "1.2.
|
|
479
|
+
version = "1.2.4"
|
|
480
480
|
source = { registry = "https://pypi.org/simple" }
|
|
481
|
-
sdist = { url = "https://files.pythonhosted.org/packages/
|
|
481
|
+
sdist = { url = "https://files.pythonhosted.org/packages/74/26/2fbeedb218a787a5eea551c7532cac4e009f83d689dd2faa0d0353473f86/python_dotenv-1.2.4.tar.gz", hash = "sha256:f0d53e69935a851c0dcc78f3ab7aaccd8cabef0b92382b576b824212902873c0", size = 60824, upload-time = "2026-10-01T05:36:10Z" }
|
|
482
482
|
wheels = [
|
|
483
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
483
|
+
{ url = "https://files.pythonhosted.org/packages/60/d1/38f3a3405989a89ac18390803e70c6ad7c7760da4f9b83cbeca0c44a0c72/python_dotenv-1.2.4-py3-none-any.whl", hash = "sha256:42269a8a5b3fd54ffa6f3d84b18abed50064717576b4ecf03dc4a55d8aa04fdc", size = 23266, upload-time = "2026-10-01T05:36:08.633Z" },
|
|
484
484
|
]
|
|
485
485
|
|
|
486
486
|
[[package]]
|
|
@@ -554,27 +554,27 @@ wheels = [
|
|
|
554
554
|
|
|
555
555
|
[[package]]
|
|
556
556
|
name = "ruff"
|
|
557
|
-
version = "0.16.
|
|
558
|
-
source = { registry = "https://pypi.org/simple" }
|
|
559
|
-
sdist = { url = "https://files.pythonhosted.org/packages/
|
|
560
|
-
wheels = [
|
|
561
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
562
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
563
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
564
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
565
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
566
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
567
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
568
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
569
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
570
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
571
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
572
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
573
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
574
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
575
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
576
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
577
|
-
{ url = "https://files.pythonhosted.org/packages/
|
|
557
|
+
version = "0.16.10"
|
|
558
|
+
source = { registry = "https://pypi.org/simple" }
|
|
559
|
+
sdist = { url = "https://files.pythonhosted.org/packages/c4/49/23802c45f093eb14bde54b141d2b2f058edfa63a06db7beed047308cc08f/ruff-0.16.10.tar.gz", hash = "sha256:eff4728c4eaae93f0955cd264d24b2ab348e74bf59986ccf282ba6dc16b3b017", size = 4958724, upload-time = "2026-10-01T18:03:21.697Z" }
|
|
560
|
+
wheels = [
|
|
561
|
+
{ url = "https://files.pythonhosted.org/packages/2f/21/ebce22e1d90cdb2cd691026b9c6e9bec6499f0b396089481755b5d49efff/ruff-0.16.10-py3-none-linux_armv6l.whl", hash = "sha256:488b0fe3f3574210e5cf80d9f59b9e3ab17a127a8155de3f307b392589cfb511", size = 10095557, upload-time = "2026-10-01T18:02:36.072Z" },
|
|
562
|
+
{ url = "https://files.pythonhosted.org/packages/cb/98/a54de85876a8b2612bfa0d84c7b9abfb39c6a3354aee7800b09c1649b9e3/ruff-0.16.10-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:e748ff95c934c4e978783b8e687bc174e7bd84e8ad24e3243e1ecfcda5e0282d", size = 10340577, upload-time = "2026-10-01T18:02:39.258Z" },
|
|
563
|
+
{ url = "https://files.pythonhosted.org/packages/9f/16/1a5a4a2657effe4806110f2b907313802f1367fcdbb29e8122766e407fab/ruff-0.16.10-py3-none-macosx_11_0_arm64.whl", hash = "sha256:3031a4a2e8e7b8a46f70be45f198c35a11ece509a94b80334d8d397a33c67550", size = 9774282, upload-time = "2026-10-01T18:02:41.79Z" },
|
|
564
|
+
{ url = "https://files.pythonhosted.org/packages/6e/fb/470085af734da396e68cd80fb0a3e7c109a459716ae59588c0f6fab8a17d/ruff-0.16.10-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:494401c86df4c4c25f69b9419605d944467ee98c42fb6ad405ef4fa40b8fb67d", size = 9920895, upload-time = "2026-10-01T18:02:44.458Z" },
|
|
565
|
+
{ url = "https://files.pythonhosted.org/packages/57/de/f10cffe4f88a37ea6615ec460f01bf76f0bc1477737a35d9ee61a630a78b/ruff-0.16.10-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:d203abc0ff2b773ee33d00ab8df0bb67046fbc7c07b119332f08b9b345cf8221", size = 9902707, upload-time = "2026-10-01T18:02:47.041Z" },
|
|
566
|
+
{ url = "https://files.pythonhosted.org/packages/ff/44/3fdcedf83ae60ef837dd239170e480dce606a9142cfa2db911afdf855fd9/ruff-0.16.10-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:bd83d1235a5258d318477bc5b576303974cbdef5df0c01a1bff14efcc23bd12a", size = 10619287, upload-time = "2026-10-01T18:02:49.485Z" },
|
|
567
|
+
{ url = "https://files.pythonhosted.org/packages/c1/62/02e76a5574002153618eafbb70e159468addc72a5d2c488e65cbb4639d2d/ruff-0.16.10-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:bc2610fb269fa56dd8a68669ae470fa6272902668c0fc2ebc3aa112b2633d5b8", size = 11339942, upload-time = "2026-10-01T18:02:52.008Z" },
|
|
568
|
+
{ url = "https://files.pythonhosted.org/packages/6b/c4/cde27d47ad8d4126c587e608c67c46e7feac11763f606d261a1bc8a489e6/ruff-0.16.10-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:3e70175e29cc94c26ea296c80e470180b744b7419026898e58f520c6ab32578e", size = 10934316, upload-time = "2026-10-01T18:02:54.811Z" },
|
|
569
|
+
{ url = "https://files.pythonhosted.org/packages/e4/03/17234145f302a645a123e8c3bb2411ecf4669fbc350de3b0d803230a1729/ruff-0.16.10-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f33f43a864a8483eebd160e713336c8bab02c934feaff0a33cf5ccb41546d09a", size = 10387968, upload-time = "2026-10-01T18:02:57.497Z" },
|
|
570
|
+
{ url = "https://files.pythonhosted.org/packages/71/29/2493af60240ee7644b38a4b821f5f9c3b5a4fa3178770fe1d0c685217395/ruff-0.16.10-py3-none-manylinux_2_31_riscv64.whl", hash = "sha256:1dfc6f0088149fb6a362c1c446bcbb3fd2157b3852fe2fa68409276eab9ad9b3", size = 10537906, upload-time = "2026-10-01T18:03:00.006Z" },
|
|
571
|
+
{ url = "https://files.pythonhosted.org/packages/2e/9a/f56b28f3b143bb9e518c34fb89ab191b626bca8b70dbf8af0d2e2d473572/ruff-0.16.10-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:6553498afc35f580f036030795810b9e6bcea31604b0fd9e8d352795473042e3", size = 10012938, upload-time = "2026-10-01T18:03:03.006Z" },
|
|
572
|
+
{ url = "https://files.pythonhosted.org/packages/c2/c4/fca37362848ea4d4d80712df13632e645e7c7cfb1bdedf140699ac7a090b/ruff-0.16.10-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:a3b8471dea115d37f123882be852bed13403746d3a76c11de4a19ec5f9ff5a03", size = 9897945, upload-time = "2026-10-01T18:03:05.818Z" },
|
|
573
|
+
{ url = "https://files.pythonhosted.org/packages/7d/c7/e0bc57664d6e0c61fd22f620af260fe9662d7e1ddb320ea7f160e76ef165/ruff-0.16.10-py3-none-musllinux_1_2_i686.whl", hash = "sha256:92e59a70bcbd9d3a5483656da906ec28edfdacfce00afd99edb8b4e9d15644be", size = 10333134, upload-time = "2026-10-01T18:03:08.25Z" },
|
|
574
|
+
{ url = "https://files.pythonhosted.org/packages/21/aa/5c9f3b68737c0e4a33a1db5dfab44f7b786d96ca91a7233112ddab1e6dc2/ruff-0.16.10-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:7ae7375f803b5520dc9f546bed7e3a0acb70b91812e9bb4b19927de22f25b77d", size = 10741702, upload-time = "2026-10-01T18:03:10.775Z" },
|
|
575
|
+
{ url = "https://files.pythonhosted.org/packages/78/fa/0f9c2020dc316c53d983be011720b8157cc052b97e3f4dfa7db6d0f880a6/ruff-0.16.10-py3-none-win32.whl", hash = "sha256:2a12e01cb9156c10c466f63b46eaae5ecea28dfbd21b5836353ae498e7d1349a", size = 10139176, upload-time = "2026-10-01T18:03:13.267Z" },
|
|
576
|
+
{ url = "https://files.pythonhosted.org/packages/99/29/cfb0df9448d4d4ad48c2de029ada9ebd71baa6da983a6c77ee6c6cd0fe82/ruff-0.16.10-py3-none-win_amd64.whl", hash = "sha256:97f2015c92aa97105b0eab19eb5d224884399281cfc5da86a92db4ab5e7fb2ca", size = 10584734, upload-time = "2026-10-01T18:03:16.006Z" },
|
|
577
|
+
{ url = "https://files.pythonhosted.org/packages/fc/05/c16957eb287c3fc062e032619a25868d93d408a844b725bcf514f7a378ff/ruff-0.16.10-py3-none-win_arm64.whl", hash = "sha256:25a65fe998c4e6861ec079ada5826a2fc605e6cbccbe9dcd7fac1f54e791621b", size = 10366440, upload-time = "2026-10-01T18:03:19.04Z" },
|
|
578
578
|
]
|
|
579
579
|
|
|
580
580
|
[[package]]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|