Debian Codegraph @

tests/test_lists.py

raw symbols SCIP
1 from typing import List
2
3 import pytest
4 from django.http import QueryDict # noqa
5 from pydantic import BaseModel, Field, conlist
6
7 from ninja import Body, Form, Query, Router, Schema
8 from ninja.testing import TestClient
9
10 router = Router()
11
12
13 @router.post("/list1")
14 def listview1(
15 request,
16 query: List[int] = Query(...),
17 form: List[int] = Form(...),
18 ):
19 return {
20 "query": query,
21 "form": form,
22 }
23
24
25 @router.post("/list2")
26 def listview2(
27 request,
28 body: List[int],
29 query: List[int] = Query(...),
30 ):
31 return {
32 "query": query,
33 "body": body,
34 }
35
36
37 class BodyModel(BaseModel):
38 x: int
39 y: int
40
41
42 @router.post("/list3")
43 def listview3(request, body: List[BodyModel]):
44 return {
45 "body": body,
46 }
47
48
49 @router.post("/list-default")
50 def listviewdefault(request, body: List[int] = [1]): # noqa: B006
51 # By default List[anything] is treated for body
52 return {
53 "body": body,
54 }
55
56
57 class Filters(Schema):
58 tags: List[str] = []
59 other_tags: List[str] = Field([], alias="other_tags_alias")
60
61
62 @router.post("/list4")
63 def listview4(
64 request,
65 filters: Filters = Query(...),
66 ):
67 return {
68 "filters": filters,
69 }
70
71
72 class ConListSchema(Schema):
73 query: conlist(int, min_length=1)
74
75
76 class Data(Schema):
77 data: ConListSchema
78
79
80 @router.post("/list5")
81 def listview5(
82 request,
83 body: conlist(int, min_length=1) = Body(...),
84 a_query: Data = Query(...),
85 ):
86 return {
87 "query": a_query.data.query,
88 "body": body,
89 }
90
91
92 @router.post("/list6")
93 def listview6(
94 request,
95 object_id: List[int] = Query(None, alias="id"),
96 ):
97 return {"query": object_id}
98
99
100 client = TestClient(router)
101
102
103 @pytest.mark.parametrize(
104 # fmt: off
105 "path,kwargs,expected_response",
106 [
107 (
108 "/list1?query=1&query=2",
109 dict(data=QueryDict("form=3&form=4")),
110 {"query": [1, 2], "form": [3, 4]},
111 ),
112 (
113 "/list2?query=1&query=2",
114 dict(json=[5, 6]),
115 {"query": [1, 2], "body": [5, 6]},
116 ),
117 (
118 "/list3",
119 dict(json=[{"x": 1, "y": 1}]),
120 {"body": [{"x": 1, "y": 1}]},
121 ),
122 (
123 "/list-default",
124 {},
125 {"body": [1]},
126 ),
127 (
128 "/list-default",
129 dict(json=[1, 2]),
130 {"body": [1, 2]},
131 ),
132 (
133 "/list4?tags=a&tags=b&other_tags_alias=a&other_tags_alias=b",
134 {},
135 {"filters": {"tags": ["a", "b"], "other_tags": ["a", "b"]}},
136 ),
137 (
138 "/list4?tags=abc&other_tags_alias=abc",
139 {},
140 {"filters": {"tags": ["abc"], "other_tags": ["abc"]}},
141 ),
142 (
143 "/list4",
144 {},
145 {"filters": {"tags": [], "other_tags": []}},
146 ),
147 (
148 "/list5?query=1&query=2",
149 dict(json=[5, 6]),
150 {"query": [1, 2], "body": [5, 6]},
151 ),
152 (
153 "/list6?id=1&id=2",
154 {},
155 {"query": [1, 2]},
156 ),
157 ],
158 # fmt: on
159 )
160 def test_list(path, kwargs, expected_response):
161 response = client.post(path, **kwargs)
162 assert response.status_code == 200, response.content
163 assert response.json() == expected_response