53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
"""Типы PATCH проверяются до домена, значения и частичная шапка — в домене."""
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from src.schemas.documents import OsvHeaderPatch
|
|
|
|
|
|
@pytest.mark.parametrize("year", ["abc", "2025", 2025.5, 2025.0, True, [], {}])
|
|
def test_year_type_is_strict(year: object) -> None:
|
|
with pytest.raises(ValidationError) as error:
|
|
OsvHeaderPatch.model_validate({"period_year": year})
|
|
assert error.value.errors()[0]["loc"] == ("period_year",)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"field,value",
|
|
[
|
|
("period_quarter", 3),
|
|
("period_quarter", []),
|
|
("osv_account_code", 60),
|
|
("osv_account_code", {}),
|
|
("is_cumulative", "true"),
|
|
("is_cumulative", 1),
|
|
("is_cumulative", 0),
|
|
],
|
|
)
|
|
def test_other_types_are_strict(field: str, value: object) -> None:
|
|
with pytest.raises(ValidationError):
|
|
OsvHeaderPatch.model_validate({field: value})
|
|
|
|
|
|
def test_domain_values_are_not_rejected_by_schema() -> None:
|
|
patch = {"period_year": 2100, "period_quarter": "Q0", "is_cumulative": False, "osv_account_code": ""}
|
|
assert OsvHeaderPatch.model_validate(patch).model_dump(exclude_unset=True) == patch
|
|
|
|
|
|
def test_null_and_absent_fields_remain_distinct() -> None:
|
|
assert OsvHeaderPatch().model_dump(exclude_unset=True) == {}
|
|
assert OsvHeaderPatch(period_year=None).model_dump(exclude_unset=True) == {"period_year": None}
|
|
|
|
|
|
def test_openapi_schema_describes_field_types() -> None:
|
|
properties = OsvHeaderPatch.model_json_schema()["properties"]
|
|
expected = {
|
|
"period_year": "integer",
|
|
"period_quarter": "string",
|
|
"is_cumulative": "boolean",
|
|
"osv_account_code": "string",
|
|
}
|
|
for name, kind in expected.items():
|
|
assert properties[name]["anyOf"] == [{"type": kind}, {"type": "null"}]
|