Coverage for tests/test_tutorial/test_cookie_param_models/test_tutorial002.py: 100%
40 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-03-10 12:31 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-03-10 12:31 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsDict 1abcdef
5from fastapi.testclient import TestClient 1abcdef
6from inline_snapshot import snapshot 1abcdef
8from tests.utils import needs_py39, needs_py310, needs_pydanticv1, needs_pydanticv2 1abcdef
11@pytest.fixture( 1abcdef
12 name="client",
13 params=[
14 pytest.param("tutorial002", marks=needs_pydanticv2),
15 pytest.param("tutorial002_py310", marks=[needs_py310, needs_pydanticv2]),
16 pytest.param("tutorial002_an", marks=needs_pydanticv2),
17 pytest.param("tutorial002_an_py39", marks=[needs_py39, needs_pydanticv2]),
18 pytest.param("tutorial002_an_py310", marks=[needs_py310, needs_pydanticv2]),
19 pytest.param("tutorial002_pv1", marks=[needs_pydanticv1, needs_pydanticv1]),
20 pytest.param("tutorial002_pv1_py310", marks=[needs_py310, needs_pydanticv1]),
21 pytest.param("tutorial002_pv1_an", marks=[needs_pydanticv1]),
22 pytest.param("tutorial002_pv1_an_py39", marks=[needs_py39, needs_pydanticv1]),
23 pytest.param("tutorial002_pv1_an_py310", marks=[needs_py310, needs_pydanticv1]),
24 ],
25)
26def get_client(request: pytest.FixtureRequest): 1abcdef
27 mod = importlib.import_module(f"docs_src.cookie_param_models.{request.param}") 1abcdef
29 client = TestClient(mod.app) 1abcdef
30 return client 1abcdef
33def test_cookie_param_model(client: TestClient): 1abcdef
34 with client as c: 1ghijkl
35 c.cookies.set("session_id", "123") 1ghijkl
36 c.cookies.set("fatebook_tracker", "456") 1ghijkl
37 c.cookies.set("googall_tracker", "789") 1ghijkl
38 response = c.get("/items/") 1ghijkl
39 assert response.status_code == 200 1ghijkl
40 assert response.json() == { 1ghijkl
41 "session_id": "123",
42 "fatebook_tracker": "456",
43 "googall_tracker": "789",
44 }
47def test_cookie_param_model_defaults(client: TestClient): 1abcdef
48 with client as c: 1stuvwx
49 c.cookies.set("session_id", "123") 1stuvwx
50 response = c.get("/items/") 1stuvwx
51 assert response.status_code == 200 1stuvwx
52 assert response.json() == { 1stuvwx
53 "session_id": "123",
54 "fatebook_tracker": None,
55 "googall_tracker": None,
56 }
59def test_cookie_param_model_invalid(client: TestClient): 1abcdef
60 response = client.get("/items/") 1yzABCD
61 assert response.status_code == 422 1yzABCD
62 assert response.json() == snapshot( 1yzABCD
63 IsDict(
64 {
65 "detail": [
66 {
67 "type": "missing",
68 "loc": ["cookie", "session_id"],
69 "msg": "Field required",
70 "input": {},
71 }
72 ]
73 }
74 )
75 | IsDict(
76 # TODO: remove when deprecating Pydantic v1
77 {
78 "detail": [
79 {
80 "type": "value_error.missing",
81 "loc": ["cookie", "session_id"],
82 "msg": "field required",
83 }
84 ]
85 }
86 )
87 )
90def test_cookie_param_model_extra(client: TestClient): 1abcdef
91 with client as c: 1mnopqr
92 c.cookies.set("session_id", "123") 1mnopqr
93 c.cookies.set("extra", "track-me-here-too") 1mnopqr
94 response = c.get("/items/") 1mnopqr
95 assert response.status_code == 422 1mnopqr
96 assert response.json() == snapshot( 1mnopqr
97 IsDict(
98 {
99 "detail": [
100 {
101 "type": "extra_forbidden",
102 "loc": ["cookie", "extra"],
103 "msg": "Extra inputs are not permitted",
104 "input": "track-me-here-too",
105 }
106 ]
107 }
108 )
109 | IsDict(
110 # TODO: remove when deprecating Pydantic v1
111 {
112 "detail": [
113 {
114 "type": "value_error.extra",
115 "loc": ["cookie", "extra"],
116 "msg": "extra fields not permitted",
117 }
118 ]
119 }
120 )
121 )
124def test_openapi_schema(client: TestClient): 1abcdef
125 response = client.get("/openapi.json") 1EFGHIJ
126 assert response.status_code == 200, response.text 1EFGHIJ
127 assert response.json() == snapshot( 1EFGHIJ
128 {
129 "openapi": "3.1.0",
130 "info": {"title": "FastAPI", "version": "0.1.0"},
131 "paths": {
132 "/items/": {
133 "get": {
134 "summary": "Read Items",
135 "operationId": "read_items_items__get",
136 "parameters": [
137 {
138 "name": "session_id",
139 "in": "cookie",
140 "required": True,
141 "schema": {"type": "string", "title": "Session Id"},
142 },
143 {
144 "name": "fatebook_tracker",
145 "in": "cookie",
146 "required": False,
147 "schema": IsDict(
148 {
149 "anyOf": [{"type": "string"}, {"type": "null"}],
150 "title": "Fatebook Tracker",
151 }
152 )
153 | IsDict(
154 # TODO: remove when deprecating Pydantic v1
155 {
156 "type": "string",
157 "title": "Fatebook Tracker",
158 }
159 ),
160 },
161 {
162 "name": "googall_tracker",
163 "in": "cookie",
164 "required": False,
165 "schema": IsDict(
166 {
167 "anyOf": [{"type": "string"}, {"type": "null"}],
168 "title": "Googall Tracker",
169 }
170 )
171 | IsDict(
172 # TODO: remove when deprecating Pydantic v1
173 {
174 "type": "string",
175 "title": "Googall Tracker",
176 }
177 ),
178 },
179 ],
180 "responses": {
181 "200": {
182 "description": "Successful Response",
183 "content": {"application/json": {"schema": {}}},
184 },
185 "422": {
186 "description": "Validation Error",
187 "content": {
188 "application/json": {
189 "schema": {
190 "$ref": "#/components/schemas/HTTPValidationError"
191 }
192 }
193 },
194 },
195 },
196 }
197 }
198 },
199 "components": {
200 "schemas": {
201 "HTTPValidationError": {
202 "properties": {
203 "detail": {
204 "items": {
205 "$ref": "#/components/schemas/ValidationError"
206 },
207 "type": "array",
208 "title": "Detail",
209 }
210 },
211 "type": "object",
212 "title": "HTTPValidationError",
213 },
214 "ValidationError": {
215 "properties": {
216 "loc": {
217 "items": {
218 "anyOf": [{"type": "string"}, {"type": "integer"}]
219 },
220 "type": "array",
221 "title": "Location",
222 },
223 "msg": {"type": "string", "title": "Message"},
224 "type": {"type": "string", "title": "Error Type"},
225 },
226 "type": "object",
227 "required": ["loc", "msg", "type"],
228 "title": "ValidationError",
229 },
230 }
231 },
232 }
233 )