-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_fodantic.py
301 lines (202 loc) · 7.35 KB
/
test_fodantic.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
import pydantic
import pytest
from fodantic import formable
AGE_TEST_VALUE = 33
@formable
class UserModel(pydantic.BaseModel):
name: str
age: int = AGE_TEST_VALUE
tags: list[str] = pydantic.Field(default_factory=list)
def test_empty_form():
form = UserModel.as_form()
assert not form.is_valid
assert not form.errors
assert list(form.fields.keys()) == ["name", "age", "tags"]
assert form.fields["name"].name == "name"
assert form.fields["name"].value == ""
assert form.fields["name"].is_required is True
assert form.fields["name"].error is None
assert form.fields["age"].name == "age"
assert form.fields["age"].value == AGE_TEST_VALUE
assert form.fields["age"].is_required is False
assert form.fields["age"].error is None
assert form.fields["tags"].name == "tags"
assert form.fields["tags"].value == []
assert form.fields["tags"].is_required is False
assert form.fields["tags"].error is None
with pytest.raises(ValueError):
form.save()
assert repr(form) == "UserModel.as_form(<empty>)"
def test_invalid_form():
form = UserModel.as_form({"age": "nan"})
assert not form.is_valid
assert not form.errors
assert form.fields["name"].error
assert form.fields["name"].error["type"] == "missing"
assert form.fields["age"].error
assert form.fields["age"].error["type"] == "int_parsing"
assert form.fields["age"].value == "nan"
assert form.fields["tags"].error is None
with pytest.raises(ValueError):
form.save()
assert repr(form) == "UserModel.as_form(<invalid>)"
def test_valid_form():
form = UserModel.as_form(
{
"name": "joe",
"age": "20",
"tags": "a",
}
)
assert form.is_valid
assert not form.errors
assert form.fields["name"].value == "joe"
assert form.fields["name"].error is None
assert form.fields["age"].value == "20"
assert form.fields["age"].error is None
assert form.fields["tags"].value == ["a"]
assert form.fields["tags"].error is None
assert form.save() == {"name": "joe", "age": 20, "tags": ["a"]}
assert repr(form) == "UserModel.as_form(name='joe', age=20, tags=['a'])"
def test_missing_list_is_empty_list():
form = UserModel.as_form({"name": "joe"})
assert form.is_valid
assert not form.errors
assert form.fields["tags"].value == []
assert form.fields["tags"].error is None
assert form.save() == {"name": "joe", "age": AGE_TEST_VALUE, "tags": []}
assert repr(form) == f"UserModel.as_form(name='joe', age={AGE_TEST_VALUE}, tags=[])"
def test_default_value():
form = UserModel.as_form({"name": "joe", "tags": "a"})
assert form.is_valid
assert not form.errors
assert form.fields["age"].value == AGE_TEST_VALUE
def test_prefix():
form = UserModel.as_form(
{"u1.name": "joe", "u1.age": "20", "u1.tags": "a"},
prefix="u1",
)
assert form.is_valid
assert not form.errors
assert form.fields["name"].value == "joe"
assert form.fields["name"].error is None
assert form.fields["age"].value == "20"
assert form.fields["age"].error is None
assert form.fields["tags"].value == ["a"]
assert form.fields["tags"].error is None
assert form.save() == {"name": "joe", "age": 20, "tags": ["a"]}
assert repr(form) == "UserModel.as_form(name='joe', age=20, tags=['a'])"
def test_prefix_with_default():
form = UserModel.as_form({"u1.name": "joe", "u1.tags": "a"}, prefix="u1")
assert form.is_valid
assert not form.errors
assert AGE_TEST_VALUE == form.fields["age"].value
def test_object_data():
class User:
name = "jon doe"
age = 25
tags = ["meh", "whatever"]
user = User()
form = UserModel.as_form({}, object=user)
assert form.is_valid
assert not form.errors
assert form.fields["name"].value == "jon doe"
assert form.fields["name"].error is None
assert form.fields["age"].value == 25
assert form.fields["age"].error is None
assert form.fields["tags"].value == ["meh", "whatever"]
assert form.fields["tags"].error is None
assert form.save() == user
def test_only_validate_with_reqdata():
class User:
name = ""
age = 55
tags = []
user = User()
form = UserModel.as_form(object=user)
assert not form.is_valid
assert not form.errors
assert form.fields["name"].value == ""
assert form.fields["name"].error is None
assert form.fields["age"].value == 55
assert form.fields["age"].error is None
assert form.fields["tags"].value == []
assert form.fields["tags"].error is None
with pytest.raises(ValueError):
form.save()
def test_object_updated():
class User:
name = "original"
age = 25
reqdata = {"name": "updated"}
user = User()
form = UserModel.as_form(reqdata, object=user)
assert form.is_valid
assert not form.errors
assert form.fields["name"].value == "updated"
assert form.fields["name"].error is None
assert form.fields["age"].value == 25
assert form.fields["age"].error is None
user = form.save()
assert "updated" == user.name
def test_dict_object_data():
user = {
"name": "jon doe",
"age": 25,
"tags": ["meh", "whatever"],
}
form = UserModel.as_form({}, object=user)
assert form.is_valid
assert not form.errors
assert form.fields["name"].value == "jon doe"
assert form.fields["name"].error is None
assert form.fields["age"].value == 25
assert form.fields["age"].error is None
assert form.fields["tags"].value == ["meh", "whatever"]
assert form.fields["tags"].error is None
assert user == form.save()
def test_dict_object_updated():
user = {"name": "original", "age": 25}
reqdata = {"name": "updated"}
form = UserModel.as_form(reqdata, object=user)
assert form.is_valid
assert not form.errors
assert form.fields["name"].value == "updated"
assert form.fields["name"].error is None
assert form.fields["age"].value == 25
assert form.fields["age"].error is None
assert "updated" == form.save()["name"]
def test_orm():
class User:
def __init__(self, name: str):
self.name = name
@formable(orm=User)
class MyModel(pydantic.BaseModel):
name: str
form = MyModel.as_form({"name": "Bobby Tables"})
user = form.save()
assert isinstance(user, User)
assert user.name == "Bobby Tables"
def test_checkbox():
@formable
class MyModel(pydantic.BaseModel):
checkbox: bool
form = MyModel.as_form({})
assert form.fields["checkbox"].value is False
assert {"checkbox": False} == form.save()
form = MyModel.as_form({"checkbox": ""})
assert form.fields["checkbox"].value is True
assert {"checkbox": True} == form.save()
form = MyModel.as_form({"checkbox": "whatever"})
assert form.fields["checkbox"].value is True
assert {"checkbox": True} == form.save()
def test_checkbox_empty_override_default():
@formable
class MyModel(pydantic.BaseModel):
checkbox: bool = True
form = MyModel.as_form({})
assert form.fields["checkbox"].value is False
assert {"checkbox": False} == form.save()
form = MyModel.as_form({}, object={"checkbox": True})
assert form.fields["checkbox"].value is False
assert {"checkbox": False} == form.save()