address review: exclude bool from coercion, parameterize tests over all fields
Agent-Logs-Url: https://github.com/dylanlangston/nextcloud-mcp-server/sessions/0360ab28-8913-450e-8c61-697e71ba9742 Co-authored-by: dylanlangston <16236219+dylanlangston@users.noreply.github.com>
This commit is contained in:
co-authored by
dylanlangston
parent
cb88d2b062
commit
6ae30acc6f
@@ -63,6 +63,8 @@ class Nutrition(BaseModel):
|
|||||||
strings (e.g. '650 kcal') or numbers (e.g. 650). Nextcloud Cookbook
|
strings (e.g. '650 kcal') or numbers (e.g. 650). Nextcloud Cookbook
|
||||||
stores whatever the source provided.
|
stores whatever the source provided.
|
||||||
"""
|
"""
|
||||||
|
if isinstance(v, bool):
|
||||||
|
return v
|
||||||
if isinstance(v, (int, float)):
|
if isinstance(v, (int, float)):
|
||||||
return str(v)
|
return str(v)
|
||||||
return v
|
return v
|
||||||
|
|||||||
@@ -1,67 +1,64 @@
|
|||||||
"""Unit tests for Nutrition model numeric coercion (issue #708)."""
|
"""Unit tests for Nutrition model numeric coercion (issue #708)."""
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from pydantic import ValidationError
|
||||||
|
|
||||||
from nextcloud_mcp_server.models.cookbook import Nutrition
|
from nextcloud_mcp_server.models.cookbook import Nutrition
|
||||||
|
|
||||||
|
NUTRITION_FIELDS = [
|
||||||
@pytest.mark.unit
|
"calories",
|
||||||
def test_nutrition_calories_accepts_string():
|
"carbohydrateContent",
|
||||||
"""String calories values should be accepted as-is."""
|
"cholesterolContent",
|
||||||
n = Nutrition(calories="650 kcal")
|
"fatContent",
|
||||||
assert n.calories == "650 kcal"
|
"fiberContent",
|
||||||
|
"proteinContent",
|
||||||
|
"saturatedFatContent",
|
||||||
|
"servingSize",
|
||||||
|
"sodiumContent",
|
||||||
|
"sugarContent",
|
||||||
|
"transFatContent",
|
||||||
|
"unsaturatedFatContent",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
def test_nutrition_calories_coerces_int():
|
@pytest.mark.parametrize("field", NUTRITION_FIELDS)
|
||||||
"""Integer calories values should be coerced to strings."""
|
def test_nutrition_field_accepts_string(field: str):
|
||||||
n = Nutrition(calories=260)
|
"""String values should be accepted as-is for every nutrition field."""
|
||||||
assert n.calories == "260"
|
n = Nutrition(**{field: "650 kcal"})
|
||||||
|
assert getattr(n, field) == "650 kcal"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
def test_nutrition_calories_coerces_float():
|
@pytest.mark.parametrize("field", NUTRITION_FIELDS)
|
||||||
"""Float calories values should be coerced to strings."""
|
def test_nutrition_field_coerces_int(field: str):
|
||||||
n = Nutrition(calories=260.5)
|
"""Integer values should be coerced to strings for every nutrition field."""
|
||||||
assert n.calories == "260.5"
|
n = Nutrition(**{field: 260})
|
||||||
|
assert getattr(n, field) == "260"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
def test_nutrition_calories_accepts_none():
|
@pytest.mark.parametrize("field", NUTRITION_FIELDS)
|
||||||
"""None calories should remain None."""
|
def test_nutrition_field_coerces_float(field: str):
|
||||||
n = Nutrition(calories=None)
|
"""Float values should be coerced to strings for every nutrition field."""
|
||||||
assert n.calories is None
|
n = Nutrition(**{field: 260.5})
|
||||||
|
assert getattr(n, field) == "260.5"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
def test_nutrition_all_fields_coerce_int():
|
@pytest.mark.parametrize("field", NUTRITION_FIELDS)
|
||||||
"""All nutrition content fields should coerce integer values to strings."""
|
def test_nutrition_field_accepts_none(field: str):
|
||||||
n = Nutrition(
|
"""None should remain None for every nutrition field."""
|
||||||
calories=260,
|
n = Nutrition(**{field: None})
|
||||||
carbohydrateContent=30,
|
assert getattr(n, field) is None
|
||||||
cholesterolContent=10,
|
|
||||||
fatContent=15,
|
|
||||||
fiberContent=5,
|
@pytest.mark.unit
|
||||||
proteinContent=20,
|
@pytest.mark.parametrize("field", NUTRITION_FIELDS)
|
||||||
saturatedFatContent=3,
|
def test_nutrition_field_rejects_bool(field: str):
|
||||||
servingSize=1,
|
"""Boolean values should not be silently coerced to strings."""
|
||||||
sodiumContent=500,
|
with pytest.raises(ValidationError):
|
||||||
sugarContent=8,
|
Nutrition(**{field: True})
|
||||||
transFatContent=0,
|
|
||||||
unsaturatedFatContent=12,
|
|
||||||
)
|
|
||||||
assert n.calories == "260"
|
|
||||||
assert n.carbohydrateContent == "30"
|
|
||||||
assert n.cholesterolContent == "10"
|
|
||||||
assert n.fatContent == "15"
|
|
||||||
assert n.fiberContent == "5"
|
|
||||||
assert n.proteinContent == "20"
|
|
||||||
assert n.saturatedFatContent == "3"
|
|
||||||
assert n.servingSize == "1"
|
|
||||||
assert n.sodiumContent == "500"
|
|
||||||
assert n.sugarContent == "8"
|
|
||||||
assert n.transFatContent == "0"
|
|
||||||
assert n.unsaturatedFatContent == "12"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
|
|||||||
Reference in New Issue
Block a user