Files
estimator/tests/unit/llm/test_schema.py
T

70 lines
1.9 KiB
Python

from pydantic import BaseModel, Field
from estimator.domain.enums import Role
from estimator.infra.llm.schema import to_strict_schema
class Inner(BaseModel):
title: str # a property named like a JSON Schema keyword must survive
role: Role
hours: float = Field(ge=0, le=100, default=1.0)
class Outer(BaseModel):
items: list[Inner]
note: str | None = None
def _objects(node):
if isinstance(node, dict):
if node.get("type") == "object":
yield node
for v in node.values():
yield from _objects(v)
elif isinstance(node, list):
for v in node:
yield from _objects(v)
def test_refs_are_inlined():
schema = to_strict_schema(Outer)
assert "$ref" not in str(schema) and "$defs" not in schema
def test_every_object_is_closed_and_fully_required():
schema = to_strict_schema(Outer)
objs = list(_objects(schema))
assert len(objs) == 2
for o in objs:
assert o["additionalProperties"] is False
assert o["required"] == list(o["properties"].keys())
def test_unsupported_keywords_stripped_but_property_names_kept():
schema = to_strict_schema(Outer)
inner = schema["properties"]["items"]["items"]
assert "title" in inner["properties"] # the field, not the keyword
hours = inner["properties"]["hours"]
assert "minimum" not in hours and "maximum" not in hours and "default" not in hours
def test_enum_is_inlined():
inner = to_strict_schema(Outer)["properties"]["items"]["items"]
assert "analyst" in inner["properties"]["role"]["enum"]
def test_nullable_field_kept_as_anyof():
note = to_strict_schema(Outer)["properties"]["note"]
assert {"type": "null"} in note["anyOf"]
def test_recursive_schema_rejected():
import pytest
class Node(BaseModel):
children: list["Node"]
Node.model_rebuild()
with pytest.raises(ValueError):
to_strict_schema(Node)