BL / tests /test_models.py
youssefboutaleb's picture
Implement OCR pipeline enhancements: Pydantic schemas, validation gate, retry, and evaluation harness (excluding images)
497c3ef
Raw History Blame Contribute Delete
1.19 kB
from __future__ import annotations
import json
from pathlib import Path
import pytest
from pydantic import ValidationError
from bl_ocr.domain.models import DeliveryNote
PROJECT_ROOT = Path(__file__).resolve().parents[1]
def _example() -> dict:
return json.loads((PROJECT_ROOT / "example.json").read_text(encoding="utf-8"))
def test_models_parse_example():
note = DeliveryNote.model_validate(_example())
assert note.products[0].pharmacist_price_ttc.value == 5.35
assert note.totals.line_count.value == 3
def test_models_forbid_extra_keys():
annotation = _example()
annotation["products"][0]["_repaired"] = "leak"
with pytest.raises(ValidationError):
DeliveryNote.model_validate(annotation)
def test_models_enforce_confidence_bounds():
annotation = _example()
annotation["products"][0]["quantity"]["confidence"] = 1.5
with pytest.raises(ValidationError):
DeliveryNote.model_validate(annotation)
def test_models_allow_null_values():
annotation = _example()
annotation["products"][0]["expiry_date"]["value"] = None
note = DeliveryNote.model_validate(annotation)
assert note.products[0].expiry_date.value is None