test(api): add tests for ai suggestion endpoints and helpers
This commit is contained in:
parent
5ad9b66a21
commit
88f3642387
4 changed files with 387 additions and 1 deletions
93
backend/tests/test_products_helpers.py
Normal file
93
backend/tests/test_products_helpers.py
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import uuid
|
||||
from datetime import date
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlmodel import Session
|
||||
|
||||
from innercontext.api.products import _build_shopping_context
|
||||
from innercontext.models import Product, SkinConditionSnapshot, ProductInventory
|
||||
|
||||
def test_build_shopping_context(session: Session):
|
||||
# Empty context
|
||||
ctx = _build_shopping_context(session)
|
||||
assert "(brak danych)" in ctx
|
||||
assert "POSIADANE PRODUKTY" in ctx
|
||||
|
||||
# Add snapshot
|
||||
snap = SkinConditionSnapshot(
|
||||
id=uuid.uuid4(),
|
||||
snapshot_date=date.today(),
|
||||
overall_state="fair",
|
||||
skin_type="combination",
|
||||
hydration_level=3,
|
||||
sensitivity_level=4,
|
||||
barrier_state="mildly_compromised",
|
||||
active_concerns=["redness"],
|
||||
priorities=["soothing"]
|
||||
)
|
||||
session.add(snap)
|
||||
|
||||
# Add product
|
||||
p = Product(
|
||||
id=uuid.uuid4(),
|
||||
name="Soothing Serum",
|
||||
brand="BrandX",
|
||||
category="serum",
|
||||
recommended_time="both",
|
||||
leave_on=True,
|
||||
targets=["redness"],
|
||||
product_effect_profile={"soothing_strength": 4, "hydration_immediate": 1},
|
||||
actives=[{"name": "Centella"}]
|
||||
)
|
||||
session.add(p)
|
||||
session.commit()
|
||||
|
||||
# Add inventory
|
||||
inv = ProductInventory(id=uuid.uuid4(), product_id=p.id, is_opened=True)
|
||||
session.add(inv)
|
||||
session.commit()
|
||||
|
||||
ctx = _build_shopping_context(session)
|
||||
assert "Typ skóry: combination" in ctx
|
||||
assert "Nawilżenie: 3/5" in ctx
|
||||
assert "Wrażliwość: 4/5" in ctx
|
||||
assert "Aktywne problemy: redness" in ctx
|
||||
assert "Priorytety: soothing" in ctx
|
||||
|
||||
# Check product
|
||||
assert "[✓] Soothing Serum" in ctx
|
||||
assert "BrandX" in ctx
|
||||
assert "targets: ['redness']" in ctx
|
||||
assert "actives: ['Centella']" in ctx
|
||||
assert "effects: {'soothing': 4}" in ctx
|
||||
|
||||
|
||||
def test_suggest_shopping(client, session):
|
||||
with patch("innercontext.api.products.call_gemini") as mock_gemini:
|
||||
mock_response = type("Response", (), {"text": '{"suggestions": [{"category": "cleanser", "product_type": "cleanser", "priority": "high", "key_ingredients": [], "target_concerns": [], "why_needed": "reason", "recommended_time": "am", "frequency": "daily"}], "reasoning": "Test shopping"}'})
|
||||
mock_gemini.return_value = mock_response
|
||||
|
||||
r = client.post("/products/suggest")
|
||||
assert r.status_code == 200
|
||||
data = r.json()
|
||||
assert len(data["suggestions"]) == 1
|
||||
assert data["suggestions"][0]["product_type"] == "cleanser"
|
||||
assert data["reasoning"] == "Test shopping"
|
||||
|
||||
def test_shopping_context_medication_skip(session: Session):
|
||||
p = Product(
|
||||
id=uuid.uuid4(),
|
||||
name="Epiduo",
|
||||
brand="Galderma",
|
||||
category="serum",
|
||||
recommended_time="pm",
|
||||
leave_on=True,
|
||||
is_medication=True,
|
||||
product_effect_profile={}
|
||||
)
|
||||
session.add(p)
|
||||
session.commit()
|
||||
|
||||
ctx = _build_shopping_context(session)
|
||||
assert "Epiduo" not in ctx
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue