Debian Codegraph @

tests/test_tutorial/test_query_params_str_validations/test_tutorial003.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7 from ...utils import needs_py310
8
9
10 @pytest.fixture(
11 name="client",
12 params=[
13 pytest.param("tutorial003_py310", marks=needs_py310),
14 pytest.param("tutorial003_an_py310", marks=needs_py310),
15 ],
16 )
17 def get_client(request: pytest.FixtureRequest):
18 mod = importlib.import_module(
19 f"docs_src.query_params_str_validations.{request.param}"
20 )
21
22 client = TestClient(mod.app)
23 return client
24
25
26 def test_query_params_str_validations_no_query(client: TestClient):
27 response = client.get("/items/")
28 assert response.status_code == 200
29 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
30
31
32 def test_query_params_str_validations_q_query(client: TestClient):
33 response = client.get("/items/", params={"q": "query"})
34 assert response.status_code == 200
35 assert response.json() == {
36 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
37 "q": "query",
38 }
39
40
41 def test_query_params_str_validations_q_too_short(client: TestClient):
42 response = client.get("/items/", params={"q": "qu"})
43 assert response.status_code == 422
44 assert response.json() == {
45 "detail": [
46 {
47 "type": "string_too_short",
48 "loc": ["query", "q"],
49 "msg": "String should have at least 3 characters",
50 "input": "qu",
51 "ctx": {"min_length": 3},
52 }
53 ]
54 }
55
56
57 def test_query_params_str_validations_q_too_long(client: TestClient):
58 response = client.get("/items/", params={"q": "q" * 51})
59 assert response.status_code == 422
60 assert response.json() == {
61 "detail": [
62 {
63 "type": "string_too_long",
64 "loc": ["query", "q"],
65 "msg": "String should have at most 50 characters",
66 "input": "q" * 51,
67 "ctx": {"max_length": 50},
68 }
69 ]
70 }
71
72
73 def test_openapi_schema(client: TestClient):
74 response = client.get("/openapi.json")
75 assert response.status_code == 200, response.text
76 assert response.json() == snapshot(
77 {
78 "openapi": "3.1.0",
79 "info": {"title": "FastAPI", "version": "0.1.0"},
80 "paths": {
81 "/items/": {
82 "get": {
83 "responses": {
84 "200": {
85 "description": "Successful Response",
86 "content": {"application/json": {"schema": {}}},
87 },
88 "422": {
89 "description": "Validation Error",
90 "content": {
91 "application/json": {
92 "schema": {
93 "$ref": "#/components/schemas/HTTPValidationError"
94 }
95 }
96 },
97 },
98 },
99 "summary": "Read Items",
100 "operationId": "read_items_items__get",
101 "parameters": [
102 {
103 "required": False,
104 "schema": {
105 "anyOf": [
106 {
107 "type": "string",
108 "minLength": 3,
109 "maxLength": 50,
110 },
111 {"type": "null"},
112 ],
113 "title": "Q",
114 },
115 "name": "q",
116 "in": "query",
117 }
118 ],
119 }
120 }
121 },
122 "components": {
123 "schemas": {
124 "ValidationError": {
125 "title": "ValidationError",
126 "required": ["loc", "msg", "type"],
127 "type": "object",
128 "properties": {
129 "loc": {
130 "title": "Location",
131 "type": "array",
132 "items": {
133 "anyOf": [{"type": "string"}, {"type": "integer"}]
134 },
135 },
136 "msg": {"title": "Message", "type": "string"},
137 "type": {"title": "Error Type", "type": "string"},
138 "input": {"title": "Input"},
139 "ctx": {"title": "Context", "type": "object"},
140 },
141 },
142 "HTTPValidationError": {
143 "title": "HTTPValidationError",
144 "type": "object",
145 "properties": {
146 "detail": {
147 "title": "Detail",
148 "type": "array",
149 "items": {
150 "$ref": "#/components/schemas/ValidationError"
151 },
152 }
153 },
154 },
155 }
156 },
157 }
158 )