1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
from __future__ import annotations
import itertools
from dataclasses import replace
from typing import TYPE_CHECKING
from unittest.mock import ANY
import pytest
from msgspec import Meta, Struct, field
from typing_extensions import Annotated
from litestar import Litestar, post
from litestar.dto import DTOField, Mark, MsgspecDTO, dto_field
from litestar.dto.data_structures import DTOFieldDefinition
from litestar.typing import FieldDefinition
if TYPE_CHECKING:
from typing import Callable
@pytest.fixture
def expected_field_defs(int_factory: Callable[[], int]) -> list[DTOFieldDefinition]:
return [
DTOFieldDefinition.from_field_definition(
field_definition=FieldDefinition.from_kwarg(
annotation=int,
name="a",
),
model_name=ANY,
default_factory=None,
dto_field=DTOField(),
),
replace(
DTOFieldDefinition.from_field_definition(
field_definition=FieldDefinition.from_kwarg(
annotation=int,
name="b",
),
model_name=ANY,
default_factory=None,
dto_field=DTOField(mark=Mark.READ_ONLY),
),
metadata=ANY,
type_wrappers=ANY,
raw=ANY,
kwarg_definition=ANY,
),
replace(
DTOFieldDefinition.from_field_definition(
field_definition=FieldDefinition.from_kwarg(
annotation=int,
name="c",
),
model_name=ANY,
default_factory=None,
dto_field=DTOField(),
),
metadata=ANY,
type_wrappers=ANY,
raw=ANY,
kwarg_definition=ANY,
),
replace(
DTOFieldDefinition.from_field_definition(
field_definition=FieldDefinition.from_kwarg(
annotation=int,
name="d",
default=1,
),
model_name=ANY,
default_factory=None,
dto_field=DTOField(),
),
metadata=ANY,
type_wrappers=ANY,
raw=ANY,
kwarg_definition=ANY,
),
replace(
DTOFieldDefinition.from_field_definition(
field_definition=FieldDefinition.from_kwarg(
annotation=int,
name="e",
),
model_name=ANY,
default_factory=int_factory,
dto_field=DTOField(),
),
metadata=ANY,
type_wrappers=ANY,
raw=ANY,
kwarg_definition=ANY,
),
replace(
DTOFieldDefinition.from_field_definition(
field_definition=FieldDefinition.from_kwarg(
annotation=str,
name="computed",
),
model_name=ANY,
default_factory=None,
dto_field=DTOField(mark="read-only"),
),
metadata=ANY,
type_wrappers=ANY,
raw=ANY,
kwarg_definition=ANY,
),
]
def test_field_definition_generation(
int_factory: Callable[[], int], expected_field_defs: list[DTOFieldDefinition]
) -> None:
class TestStruct(Struct):
a: int
b: Annotated[int, Meta(extra=dto_field("read-only"))]
c: Annotated[int, Meta(gt=1)]
d: int = field(default=1)
e: int = field(default_factory=int_factory)
@property
def computed(self) -> str:
return "i am computed"
@property
def _private_computed(self) -> str:
return ""
field_defs = list(MsgspecDTO.generate_field_definitions(TestStruct))
assert field_defs[0].model_name == "TestStruct"
for field_def, exp in itertools.zip_longest(expected_field_defs, field_defs, fillvalue=None):
assert field_def == exp
def test_detect_nested_field() -> None:
class TestStruct(Struct):
a: int
class NotStruct:
pass
assert MsgspecDTO.detect_nested_field(FieldDefinition.from_annotation(TestStruct)) is True
assert MsgspecDTO.detect_nested_field(FieldDefinition.from_annotation(NotStruct)) is False
ReadOnlyInt = Annotated[int, DTOField("read-only")]
def test_msgspec_dto_annotated_dto_field() -> None:
class Model(Struct):
a: Annotated[int, DTOField("read-only")]
b: ReadOnlyInt
dto_type = MsgspecDTO[Model]
fields = list(dto_type.generate_field_definitions(Model))
assert fields[0].dto_field == DTOField("read-only")
assert fields[1].dto_field == DTOField("read-only")
def test_tag_field_included_in_schema() -> None:
# default tag field, default tag value
class Model(Struct, tag=True):
regular_field: str
# default tag field, custom tag value
class Model2(Struct, tag=2):
regular_field: str
# custom tag field, custom tag value
class Model3(Struct, tag_field="foo", tag="bar"):
regular_field: str
@post("/1")
def handler(data: Model) -> None:
return None
@post("/2")
def handler_2(data: Model2) -> None:
return None
@post("/3")
def handler_3(data: Model3) -> None:
return None
components = Litestar(
[handler, handler_2, handler_3],
signature_types=[Model, Model2, Model3],
).openapi_schema.components.to_schema()["schemas"]
assert components["test_tag_field_included_in_schema.Model"] == {
"properties": {
"regular_field": {"type": "string"},
"type": {"type": "string", "const": "Model"},
},
"type": "object",
"required": ["regular_field", "type"],
"title": "Model",
}
assert components["test_tag_field_included_in_schema.Model2"] == {
"properties": {
"regular_field": {"type": "string"},
"type": {"type": "integer", "const": 2},
},
"type": "object",
"required": ["regular_field", "type"],
"title": "Model2",
}
assert components["test_tag_field_included_in_schema.Model3"] == {
"properties": {
"regular_field": {"type": "string"},
"foo": {"type": "string", "const": "bar"},
},
"type": "object",
"required": ["foo", "regular_field"],
"title": "Model3",
}
|