forked from pydantic/pydantic
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_orm_mode.py
360 lines (269 loc) · 8.42 KB
/
test_orm_mode.py
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
from types import SimpleNamespace
from typing import Any, Dict, List
import pytest
from pydantic import BaseModel, ConfigError, ValidationError, root_validator
from pydantic.utils import GetterDict
def test_getdict():
class TestCls:
a = 1
b: int
def __init__(self):
self.c = 3
@property
def d(self):
return 4
def __getattr__(self, key):
if key == 'e':
return 5
else:
raise AttributeError()
t = TestCls()
gd = GetterDict(t)
assert gd.keys() == ['a', 'c', 'd']
assert gd.get('a') == 1
assert gd['a'] == 1
with pytest.raises(KeyError):
assert gd['foobar']
assert gd.get('b', None) is None
assert gd.get('b', 1234) == 1234
assert gd.get('c', None) == 3
assert gd.get('d', None) == 4
assert gd.get('e', None) == 5
assert gd.get('f', 'missing') == 'missing'
assert list(gd.values()) == [1, 3, 4]
assert list(gd.items()) == [('a', 1), ('c', 3), ('d', 4)]
assert list(gd) == ['a', 'c', 'd']
assert gd == {'a': 1, 'c': 3, 'd': 4}
assert 'a' in gd
assert len(gd) == 3
assert str(gd) == "{'a': 1, 'c': 3, 'd': 4}"
assert repr(gd) == "GetterDict[TestCls]({'a': 1, 'c': 3, 'd': 4})"
def test_orm_mode_root():
class PokemonCls:
def __init__(self, *, en_name: str, jp_name: str):
self.en_name = en_name
self.jp_name = jp_name
class Pokemon(BaseModel):
en_name: str
jp_name: str
class Config:
orm_mode = True
class PokemonList(BaseModel):
__root__: List[Pokemon]
class Config:
orm_mode = True
pika = PokemonCls(en_name='Pikachu', jp_name='ピカチュウ')
bulbi = PokemonCls(en_name='Bulbasaur', jp_name='フシギダネ')
pokemons = PokemonList.from_orm([pika, bulbi])
assert pokemons.__root__ == [
Pokemon(en_name='Pikachu', jp_name='ピカチュウ'),
Pokemon(en_name='Bulbasaur', jp_name='フシギダネ'),
]
class PokemonDict(BaseModel):
__root__: Dict[str, Pokemon]
class Config:
orm_mode = True
pokemons = PokemonDict.from_orm({'pika': pika, 'bulbi': bulbi})
assert pokemons.__root__ == {
'pika': Pokemon(en_name='Pikachu', jp_name='ピカチュウ'),
'bulbi': Pokemon(en_name='Bulbasaur', jp_name='フシギダネ'),
}
def test_orm_mode():
class PetCls:
def __init__(self, *, name: str, species: str):
self.name = name
self.species = species
class PersonCls:
def __init__(self, *, name: str, age: float = None, pets: List[PetCls]):
self.name = name
self.age = age
self.pets = pets
class Pet(BaseModel):
name: str
species: str
class Config:
orm_mode = True
class Person(BaseModel):
name: str
age: float = None
pets: List[Pet]
class Config:
orm_mode = True
bones = PetCls(name='Bones', species='dog')
orion = PetCls(name='Orion', species='cat')
anna = PersonCls(name='Anna', age=20, pets=[bones, orion])
anna_model = Person.from_orm(anna)
assert anna_model.dict() == {
'name': 'Anna',
'pets': [{'name': 'Bones', 'species': 'dog'}, {'name': 'Orion', 'species': 'cat'}],
'age': 20.0,
}
def test_not_orm_mode():
class Pet(BaseModel):
name: str
species: str
with pytest.raises(ConfigError):
Pet.from_orm(None)
def test_object_with_getattr():
class FooGetAttr:
def __getattr__(self, key: str):
if key == 'foo':
return 'Foo'
else:
raise AttributeError
class Model(BaseModel):
foo: str
bar: int = 1
class Config:
orm_mode = True
class ModelInvalid(BaseModel):
foo: str
bar: int
class Config:
orm_mode = True
foo = FooGetAttr()
model = Model.from_orm(foo)
assert model.foo == 'Foo'
assert model.bar == 1
assert model.dict(exclude_unset=True) == {'foo': 'Foo'}
with pytest.raises(ValidationError):
ModelInvalid.from_orm(foo)
def test_properties():
class XyProperty:
x = 4
@property
def y(self):
return '5'
class Model(BaseModel):
x: int
y: int
class Config:
orm_mode = True
model = Model.from_orm(XyProperty())
assert model.x == 4
assert model.y == 5
def test_extra_allow():
class TestCls:
x = 1
y = 2
class Model(BaseModel):
x: int
class Config:
orm_mode = True
extra = 'allow'
model = Model.from_orm(TestCls())
assert model.dict() == {'x': 1}
def test_extra_forbid():
class TestCls:
x = 1
y = 2
class Model(BaseModel):
x: int
class Config:
orm_mode = True
extra = 'forbid'
model = Model.from_orm(TestCls())
assert model.dict() == {'x': 1}
def test_root_validator():
validator_value = None
class TestCls:
x = 1
y = 2
class Model(BaseModel):
x: int
y: int
z: int
@root_validator(pre=True)
def change_input_data(cls, value):
nonlocal validator_value
validator_value = value
return {**value, 'z': value['x'] + value['y']}
class Config:
orm_mode = True
model = Model.from_orm(TestCls())
assert model.dict() == {'x': 1, 'y': 2, 'z': 3}
assert isinstance(validator_value, GetterDict)
assert validator_value == {'x': 1, 'y': 2}
def test_custom_getter_dict():
class TestCls:
x = 1
y = 2
def custom_getter_dict(obj):
assert isinstance(obj, TestCls)
return {'x': 42, 'y': 24}
class Model(BaseModel):
x: int
y: int
class Config:
orm_mode = True
getter_dict = custom_getter_dict
model = Model.from_orm(TestCls())
assert model.dict() == {'x': 42, 'y': 24}
def test_custom_getter_dict_derived_model_class():
class CustomCollection:
__custom__ = True
def __iter__(self):
for elem in range(5):
yield elem
class Example:
def __init__(self, *args, **kwargs):
self.col = CustomCollection()
self.id = 1
self.name = 'name'
class MyGetterDict(GetterDict):
def get(self, key: Any, default: Any = None) -> Any:
res = getattr(self._obj, key, default)
if hasattr(res, '__custom__'):
return list(res)
return res
class ExampleBase(BaseModel):
name: str
col: List[int]
class ExampleOrm(ExampleBase):
id: int
class Config:
orm_mode = True
getter_dict = MyGetterDict
model = ExampleOrm.from_orm(Example())
assert model.dict() == {'name': 'name', 'col': [0, 1, 2, 3, 4], 'id': 1}
def test_recursive_parsing():
class Getter(GetterDict):
# try to read the modified property name
# either as an attribute or as a key
def get(self, key, default):
key = key + key
try:
v = self._obj[key]
return Getter(v) if isinstance(v, dict) else v
except TypeError:
return getattr(self._obj, key, default)
except KeyError:
return default
class Model(BaseModel):
class Config:
orm_mode = True
getter_dict = Getter
class ModelA(Model):
a: int
class ModelB(Model):
b: ModelA
# test recursive parsing with object attributes
dct = dict(bb=SimpleNamespace(aa=1))
assert ModelB.from_orm(dct) == ModelB(b=ModelA(a=1))
# test recursive parsing with dict keys
obj = dict(bb=dict(aa=1))
assert ModelB.from_orm(obj) == ModelB(b=ModelA(a=1))
def test_nested_orm():
class User(BaseModel):
first_name: str
last_name: str
class Config:
orm_mode = True
class State(BaseModel):
user: User
class Config:
orm_mode = True
# Pass an "orm instance"
State.from_orm(SimpleNamespace(user=SimpleNamespace(first_name='John', last_name='Appleseed')))
# Pass dictionary data directly
State(**{'user': {'first_name': 'John', 'last_name': 'Appleseed'}})