Coverage for tests/test_query.py: 100%
119 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-01-13 13:38 +0000
1from dirty_equals import IsDict 1abcde
2from fastapi.testclient import TestClient 1abcde
4from .main import app 1abcde
6client = TestClient(app) 1abcde
9def test_query(): 1abcde
10 response = client.get("/query") 1fghij
11 assert response.status_code == 422 1fghij
12 assert response.json() == IsDict( 1fghij
13 {
14 "detail": [
15 {
16 "type": "missing",
17 "loc": ["query", "query"],
18 "msg": "Field required",
19 "input": None,
20 }
21 ]
22 }
23 ) | IsDict(
24 # TODO: remove when deprecating Pydantic v1
25 {
26 "detail": [
27 {
28 "loc": ["query", "query"],
29 "msg": "field required",
30 "type": "value_error.missing",
31 }
32 ]
33 }
34 )
37def test_query_query_baz(): 1abcde
38 response = client.get("/query?query=baz") 1klmno
39 assert response.status_code == 200 1klmno
40 assert response.json() == "foo bar baz" 1klmno
43def test_query_not_declared_baz(): 1abcde
44 response = client.get("/query?not_declared=baz") 1pqrst
45 assert response.status_code == 422 1pqrst
46 assert response.json() == IsDict( 1pqrst
47 {
48 "detail": [
49 {
50 "type": "missing",
51 "loc": ["query", "query"],
52 "msg": "Field required",
53 "input": None,
54 }
55 ]
56 }
57 ) | IsDict(
58 # TODO: remove when deprecating Pydantic v1
59 {
60 "detail": [
61 {
62 "loc": ["query", "query"],
63 "msg": "field required",
64 "type": "value_error.missing",
65 }
66 ]
67 }
68 )
71def test_query_optional(): 1abcde
72 response = client.get("/query/optional") 1uvwxy
73 assert response.status_code == 200 1uvwxy
74 assert response.json() == "foo bar" 1uvwxy
77def test_query_optional_query_baz(): 1abcde
78 response = client.get("/query/optional?query=baz") 1zABCD
79 assert response.status_code == 200 1zABCD
80 assert response.json() == "foo bar baz" 1zABCD
83def test_query_optional_not_declared_baz(): 1abcde
84 response = client.get("/query/optional?not_declared=baz") 1EFGHI
85 assert response.status_code == 200 1EFGHI
86 assert response.json() == "foo bar" 1EFGHI
89def test_query_int(): 1abcde
90 response = client.get("/query/int") 1JKLMN
91 assert response.status_code == 422 1JKLMN
92 assert response.json() == IsDict( 1JKLMN
93 {
94 "detail": [
95 {
96 "type": "missing",
97 "loc": ["query", "query"],
98 "msg": "Field required",
99 "input": None,
100 }
101 ]
102 }
103 ) | IsDict(
104 # TODO: remove when deprecating Pydantic v1
105 {
106 "detail": [
107 {
108 "loc": ["query", "query"],
109 "msg": "field required",
110 "type": "value_error.missing",
111 }
112 ]
113 }
114 )
117def test_query_int_query_42(): 1abcde
118 response = client.get("/query/int?query=42") 1OPQRS
119 assert response.status_code == 200 1OPQRS
120 assert response.json() == "foo bar 42" 1OPQRS
123def test_query_int_query_42_5(): 1abcde
124 response = client.get("/query/int?query=42.5") 1TUVWX
125 assert response.status_code == 422 1TUVWX
126 assert response.json() == IsDict( 1TUVWX
127 {
128 "detail": [
129 {
130 "type": "int_parsing",
131 "loc": ["query", "query"],
132 "msg": "Input should be a valid integer, unable to parse string as an integer",
133 "input": "42.5",
134 }
135 ]
136 }
137 ) | IsDict(
138 # TODO: remove when deprecating Pydantic v1
139 {
140 "detail": [
141 {
142 "loc": ["query", "query"],
143 "msg": "value is not a valid integer",
144 "type": "type_error.integer",
145 }
146 ]
147 }
148 )
151def test_query_int_query_baz(): 1abcde
152 response = client.get("/query/int?query=baz") 1YZ012
153 assert response.status_code == 422 1YZ012
154 assert response.json() == IsDict( 1YZ012
155 {
156 "detail": [
157 {
158 "type": "int_parsing",
159 "loc": ["query", "query"],
160 "msg": "Input should be a valid integer, unable to parse string as an integer",
161 "input": "baz",
162 }
163 ]
164 }
165 ) | IsDict(
166 # TODO: remove when deprecating Pydantic v1
167 {
168 "detail": [
169 {
170 "loc": ["query", "query"],
171 "msg": "value is not a valid integer",
172 "type": "type_error.integer",
173 }
174 ]
175 }
176 )
179def test_query_int_not_declared_baz(): 1abcde
180 response = client.get("/query/int?not_declared=baz") 134567
181 assert response.status_code == 422 134567
182 assert response.json() == IsDict( 134567
183 {
184 "detail": [
185 {
186 "type": "missing",
187 "loc": ["query", "query"],
188 "msg": "Field required",
189 "input": None,
190 }
191 ]
192 }
193 ) | IsDict(
194 # TODO: remove when deprecating Pydantic v1
195 {
196 "detail": [
197 {
198 "loc": ["query", "query"],
199 "msg": "field required",
200 "type": "value_error.missing",
201 }
202 ]
203 }
204 )
207def test_query_int_optional(): 1abcde
208 response = client.get("/query/int/optional") 189!#$
209 assert response.status_code == 200 189!#$
210 assert response.json() == "foo bar" 189!#$
213def test_query_int_optional_query_50(): 1abcde
214 response = client.get("/query/int/optional?query=50") 1%'()*
215 assert response.status_code == 200 1%'()*
216 assert response.json() == "foo bar 50" 1%'()*
219def test_query_int_optional_query_foo(): 1abcde
220 response = client.get("/query/int/optional?query=foo") 1+,-./
221 assert response.status_code == 422 1+,-./
222 assert response.json() == IsDict( 1+,-./
223 {
224 "detail": [
225 {
226 "type": "int_parsing",
227 "loc": ["query", "query"],
228 "msg": "Input should be a valid integer, unable to parse string as an integer",
229 "input": "foo",
230 }
231 ]
232 }
233 ) | IsDict(
234 # TODO: remove when deprecating Pydantic v1
235 {
236 "detail": [
237 {
238 "loc": ["query", "query"],
239 "msg": "value is not a valid integer",
240 "type": "type_error.integer",
241 }
242 ]
243 }
244 )
247def test_query_int_default(): 1abcde
248 response = client.get("/query/int/default") 1:;=?@
249 assert response.status_code == 200 1:;=?@
250 assert response.json() == "foo bar 10" 1:;=?@
253def test_query_int_default_query_50(): 1abcde
254 response = client.get("/query/int/default?query=50") 1[]^_`
255 assert response.status_code == 200 1[]^_`
256 assert response.json() == "foo bar 50" 1[]^_`
259def test_query_int_default_query_foo(): 1abcde
260 response = client.get("/query/int/default?query=foo") 2{ | } ~ ab
261 assert response.status_code == 422 2{ | } ~ ab
262 assert response.json() == IsDict( 2{ | } ~ ab
263 {
264 "detail": [
265 {
266 "type": "int_parsing",
267 "loc": ["query", "query"],
268 "msg": "Input should be a valid integer, unable to parse string as an integer",
269 "input": "foo",
270 }
271 ]
272 }
273 ) | IsDict(
274 # TODO: remove when deprecating Pydantic v1
275 {
276 "detail": [
277 {
278 "loc": ["query", "query"],
279 "msg": "value is not a valid integer",
280 "type": "type_error.integer",
281 }
282 ]
283 }
284 )
287def test_query_param(): 1abcde
288 response = client.get("/query/param") 2bbcbdbebfb
289 assert response.status_code == 200 2bbcbdbebfb
290 assert response.json() == "foo bar" 2bbcbdbebfb
293def test_query_param_query_50(): 1abcde
294 response = client.get("/query/param?query=50") 2gbhbibjbkb
295 assert response.status_code == 200 2gbhbibjbkb
296 assert response.json() == "foo bar 50" 2gbhbibjbkb
299def test_query_param_required(): 1abcde
300 response = client.get("/query/param-required") 2lbmbnbobpb
301 assert response.status_code == 422 2lbmbnbobpb
302 assert response.json() == IsDict( 2lbmbnbobpb
303 {
304 "detail": [
305 {
306 "type": "missing",
307 "loc": ["query", "query"],
308 "msg": "Field required",
309 "input": None,
310 }
311 ]
312 }
313 ) | IsDict(
314 # TODO: remove when deprecating Pydantic v1
315 {
316 "detail": [
317 {
318 "loc": ["query", "query"],
319 "msg": "field required",
320 "type": "value_error.missing",
321 }
322 ]
323 }
324 )
327def test_query_param_required_query_50(): 1abcde
328 response = client.get("/query/param-required?query=50") 2qbrbsbtbub
329 assert response.status_code == 200 2qbrbsbtbub
330 assert response.json() == "foo bar 50" 2qbrbsbtbub
333def test_query_param_required_int(): 1abcde
334 response = client.get("/query/param-required/int") 2vbwbxbybzb
335 assert response.status_code == 422 2vbwbxbybzb
336 assert response.json() == IsDict( 2vbwbxbybzb
337 {
338 "detail": [
339 {
340 "type": "missing",
341 "loc": ["query", "query"],
342 "msg": "Field required",
343 "input": None,
344 }
345 ]
346 }
347 ) | IsDict(
348 # TODO: remove when deprecating Pydantic v1
349 {
350 "detail": [
351 {
352 "loc": ["query", "query"],
353 "msg": "field required",
354 "type": "value_error.missing",
355 }
356 ]
357 }
358 )
361def test_query_param_required_int_query_50(): 1abcde
362 response = client.get("/query/param-required/int?query=50") 2AbBbCbDbEb
363 assert response.status_code == 200 2AbBbCbDbEb
364 assert response.json() == "foo bar 50" 2AbBbCbDbEb
367def test_query_param_required_int_query_foo(): 1abcde
368 response = client.get("/query/param-required/int?query=foo") 2FbGbHbIbJb
369 assert response.status_code == 422 2FbGbHbIbJb
370 assert response.json() == IsDict( 2FbGbHbIbJb
371 {
372 "detail": [
373 {
374 "type": "int_parsing",
375 "loc": ["query", "query"],
376 "msg": "Input should be a valid integer, unable to parse string as an integer",
377 "input": "foo",
378 }
379 ]
380 }
381 ) | IsDict(
382 # TODO: remove when deprecating Pydantic v1
383 {
384 "detail": [
385 {
386 "loc": ["query", "query"],
387 "msg": "value is not a valid integer",
388 "type": "type_error.integer",
389 }
390 ]
391 }
392 )
395def test_query_frozenset_query_1_query_1_query_2(): 1abcde
396 response = client.get("/query/frozenset/?query=1&query=1&query=2") 2KbLbMbNbOb
397 assert response.status_code == 200 2KbLbMbNbOb
398 assert response.json() == "1,2" 2KbLbMbNbOb
401def test_query_list(): 1abcde
402 response = client.get("/query/list/?device_ids=1&device_ids=2") 2PbQbRbSbTb
403 assert response.status_code == 200 2PbQbRbSbTb
404 assert response.json() == [1, 2] 2PbQbRbSbTb
407def test_query_list_empty(): 1abcde
408 response = client.get("/query/list/") 24b5b6b7b8b
409 assert response.status_code == 422 24b5b6b7b8b
412def test_query_list_default(): 1abcde
413 response = client.get("/query/list-default/?device_ids=1&device_ids=2") 2UbVbWbXbYb
414 assert response.status_code == 200 2UbVbWbXbYb
415 assert response.json() == [1, 2] 2UbVbWbXbYb
418def test_query_list_default_empty(): 1abcde
419 response = client.get("/query/list-default/") 2Zb0b1b2b3b
420 assert response.status_code == 200 2Zb0b1b2b3b
421 assert response.json() == [] 2Zb0b1b2b3b