|
| 1 | +"""Unit tests for search-oriented schemas (search.py).""" |
| 2 | + |
| 3 | +from datetime import datetime, timezone |
| 4 | + |
| 5 | +import pytest |
| 6 | +from holiday_peak_lib.schemas.search import IntentClassification, SearchEnrichedProduct |
| 7 | + |
| 8 | + |
| 9 | +class TestSearchEnrichedProduct: |
| 10 | + """Tests for SearchEnrichedProduct model.""" |
| 11 | + |
| 12 | + @pytest.mark.parametrize( |
| 13 | + "payload", |
| 14 | + [ |
| 15 | + { |
| 16 | + "id": "SEP-1", |
| 17 | + "entityId": "STYLE-1", |
| 18 | + "sku": "SKU-1", |
| 19 | + "name": "Trail Shoe", |
| 20 | + "brand": "Contoso", |
| 21 | + "category": "footwear", |
| 22 | + "sourceApprovalVersion": 3, |
| 23 | + }, |
| 24 | + { |
| 25 | + "id": "SEP-2", |
| 26 | + "entityId": "STYLE-2", |
| 27 | + "sku": "SKU-2", |
| 28 | + "name": "Running Shoe", |
| 29 | + "brand": "Fabrikam", |
| 30 | + "category": "footwear", |
| 31 | + "description": "Breathable upper", |
| 32 | + "price": 129.99, |
| 33 | + "useCases": ["running"], |
| 34 | + "complementaryProducts": ["SKU-SOCK-1"], |
| 35 | + "substituteProducts": ["SKU-ALT-1"], |
| 36 | + "searchKeywords": ["shoe", "breathable"], |
| 37 | + "enrichedDescription": "Lightweight breathable trail shoe.", |
| 38 | + "enrichedAt": datetime(2026, 3, 1, tzinfo=timezone.utc), |
| 39 | + "enrichmentModel": "gpt-4o-mini", |
| 40 | + "sourceApprovalVersion": 5, |
| 41 | + }, |
| 42 | + ], |
| 43 | + ) |
| 44 | + def test_required_and_optional_fields(self, payload: dict): |
| 45 | + model = SearchEnrichedProduct(**payload) |
| 46 | + assert model.id.startswith("SEP-") |
| 47 | + assert model.entity_id.startswith("STYLE-") |
| 48 | + assert model.source_approval_version > 0 |
| 49 | + |
| 50 | + def test_json_roundtrip(self): |
| 51 | + model = SearchEnrichedProduct( |
| 52 | + id="SEP-3", |
| 53 | + entityId="STYLE-3", |
| 54 | + sku="SKU-3", |
| 55 | + name="Hiking Boot", |
| 56 | + brand="Contoso", |
| 57 | + category="boots", |
| 58 | + useCases=["hiking"], |
| 59 | + sourceApprovalVersion=7, |
| 60 | + ) |
| 61 | + payload = model.model_dump_json(by_alias=True) |
| 62 | + restored = SearchEnrichedProduct.model_validate_json(payload) |
| 63 | + assert restored.entity_id == "STYLE-3" |
| 64 | + assert restored.use_cases == ["hiking"] |
| 65 | + |
| 66 | + |
| 67 | +class TestIntentClassification: |
| 68 | + """Tests for IntentClassification model.""" |
| 69 | + |
| 70 | + @pytest.mark.parametrize("query_type", ["simple", "complex"]) |
| 71 | + def test_required_and_optional_fields(self, query_type: str): |
| 72 | + model = IntentClassification(queryType=query_type, confidence=0.84) |
| 73 | + assert model.query_type == query_type |
| 74 | + assert model.attributes == [] |
| 75 | + assert model.price_range == (None, None) |
| 76 | + |
| 77 | + @pytest.mark.parametrize("confidence", [-0.01, 1.01]) |
| 78 | + def test_confidence_bounds(self, confidence: float): |
| 79 | + with pytest.raises(Exception): |
| 80 | + IntentClassification(queryType="simple", confidence=confidence) |
| 81 | + |
| 82 | + def test_json_roundtrip(self): |
| 83 | + model = IntentClassification( |
| 84 | + queryType="complex", |
| 85 | + category="footwear", |
| 86 | + attributes=["waterproof", "lightweight"], |
| 87 | + useCase="hiking", |
| 88 | + brand="Contoso", |
| 89 | + priceRange=(100.0, 200.0), |
| 90 | + filters={"size": "10"}, |
| 91 | + subQueries=["waterproof hiking shoe", "lightweight hiking boot"], |
| 92 | + confidence=0.91, |
| 93 | + ) |
| 94 | + payload = model.model_dump_json(by_alias=True) |
| 95 | + restored = IntentClassification.model_validate_json(payload) |
| 96 | + assert restored.query_type == "complex" |
| 97 | + assert restored.sub_queries[0] == "waterproof hiking shoe" |
0 commit comments