-
-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathtest_msgspec.py
More file actions
284 lines (235 loc) · 8.96 KB
/
test_msgspec.py
File metadata and controls
284 lines (235 loc) · 8.96 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from __future__ import annotations
import itertools
from dataclasses import replace
from typing import TYPE_CHECKING, Annotated, ClassVar
from unittest.mock import ANY
import pytest
from msgspec import Meta, Struct, field
from litestar import Litestar, get, post
from litestar.dto import DTOConfig, DTOField, Mark, MsgspecDTO, dto_field
from litestar.dto.data_structures import DTOFieldDefinition
from litestar.testing import create_test_client
from litestar.typing import FieldDefinition
if TYPE_CHECKING:
from collections.abc 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",
}
def test_msgspec_dto_with_classvar() -> None:
class ModelWithClassVar(Struct):
regular_field: str
class_field: ClassVar[str] = "a string in the class"
field_defs = list(MsgspecDTO.generate_field_definitions(ModelWithClassVar))
# Only the regular field should be included, not the ClassVar
assert len(field_defs) == 1
assert field_defs[0].name == "regular_field"
@pytest.mark.parametrize("use_experimental_dto_backend", [False, True])
def test_msgspec_dto_tagged_union_tag_field_serialized(use_experimental_dto_backend: bool) -> None:
"""Tag field must be present in DTO-serialized output for tagged Struct types.
Regression: MsgspecDTO.generate_field_definitions iterates over
msgspec.inspect.type_info(model).fields which does NOT include the synthetic
tag field, so the tag is silently dropped when the DTO builds its transfer model.
"""
class Cat(Struct, tag=True):
name: str
class Dog(Struct, tag=True):
name: str
class CatDTO(MsgspecDTO[Cat]):
config = DTOConfig(experimental_codegen_backend=use_experimental_dto_backend)
@get("/cat", return_dto=CatDTO, signature_types=[Cat])
def handler() -> Cat:
return Cat(name="Whiskers")
with create_test_client([handler]) as client:
response = client.get("/cat")
assert response.status_code == 200
data = response.json()
# The tag field ("type") must be present and equal to the class name
assert data.get("type") == "Cat", f"Expected tag field 'type' = 'Cat' in response, got: {data!r}"
assert data.get("name") == "Whiskers"
@pytest.mark.parametrize("use_experimental_dto_backend", [False, True])
def test_msgspec_dto_tagged_union_custom_tag_field_serialized(use_experimental_dto_backend: bool) -> None:
"""Custom tag_field and tag value must be present in DTO-serialized output."""
class Widget(Struct, tag_field="kind", tag="widget"):
value: int
class WidgetDTO(MsgspecDTO[Widget]):
config = DTOConfig(experimental_codegen_backend=use_experimental_dto_backend)
@get("/widget", return_dto=WidgetDTO, signature_types=[Widget])
def handler() -> Widget:
return Widget(value=42)
with create_test_client([handler]) as client:
response = client.get("/widget")
assert response.status_code == 200
data = response.json()
assert data.get("kind") == "widget", f"Expected tag field 'kind' = 'widget' in response, got: {data!r}"
assert data.get("value") == 42