Debian Codegraph @

tests/test_tutorial/test_query_params_str_validations/test_tutorial010.py

raw
1 import importlib
2
3 import pytest
4 from fastapi._compat import PYDANTIC_VERSION_MINOR_TUPLE
5 from fastapi.testclient import TestClient
6 from inline_snapshot import Is, snapshot
7
8 from ...utils import needs_py310
9
10
11 @pytest.fixture(
12 name="client",
13 params=[
14 pytest.param("tutorial010_py310", marks=needs_py310),
15 pytest.param("tutorial010_an_py310", marks=needs_py310),
16 ],
17 )
18 def get_client(request: pytest.FixtureRequest):
19 mod = importlib.import_module(
20 f"docs_src.query_params_str_validations.{request.param}"
21 )
22
23 client = TestClient(mod.app)
24 return client
25
26
27 def test_query_params_str_validations_no_query(client: TestClient):
28 response = client.get("/items/")
29 assert response.status_code == 200
30 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
31
32
33 def test_query_params_str_validations_item_query_fixedquery(client: TestClient):
34 response = client.get("/items/", params={"item-query": "fixedquery"})
35 assert response.status_code == 200
36 assert response.json() == {
37 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
38 "q": "fixedquery",
39 }
40
41
42 def test_query_params_str_validations_q_fixedquery(client: TestClient):
43 response = client.get("/items/", params={"q": "fixedquery"})
44 assert response.status_code == 200
45 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
46
47
48 def test_query_params_str_validations_item_query_nonregexquery(client: TestClient):
49 response = client.get("/items/", params={"item-query": "nonregexquery"})
50 assert response.status_code == 422
51 assert response.json() == {
52 "detail": [
53 {
54 "type": "string_pattern_mismatch",
55 "loc": ["query", "item-query"],
56 "msg": "String should match pattern '^fixedquery$'",
57 "input": "nonregexquery",
58 "ctx": {"pattern": "^fixedquery$"},
59 }
60 ]
61 }
62
63
64 def test_openapi_schema(client: TestClient):
65 response = client.get("/openapi.json")
66 assert response.status_code == 200, response.text
67
68 parameters_schema = {
69 "anyOf": [
70 {
71 "type": "string",
72 "minLength": 3,
73 "maxLength": 50,
74 "pattern": "^fixedquery$",
75 },
76 {"type": "null"},
77 ],
78 "title": "Query string",
79 "description": "Query string for the items to search in the database that have a good match",
80 # See https://github.com/pydantic/pydantic/blob/80353c29a824c55dea4667b328ba8f329879ac9f/tests/test_fastapi.sh#L25-L34.
81 **({"deprecated": True} if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 10) else {}),
82 }
83
84 assert response.json() == snapshot(
85 {
86 "openapi": "3.1.0",
87 "info": {"title": "FastAPI", "version": "0.1.0"},
88 "paths": {
89 "/items/": {
90 "get": {
91 "responses": {
92 "200": {
93 "description": "Successful Response",
94 "content": {"application/json": {"schema": {}}},
95 },
96 "422": {
97 "description": "Validation Error",
98 "content": {
99 "application/json": {
100 "schema": {
101 "$ref": "#/components/schemas/HTTPValidationError"
102 }
103 }
104 },
105 },
106 },
107 "summary": "Read Items",
108 "operationId": "read_items_items__get",
109 "parameters": [
110 {
111 "description": "Query string for the items to search in the database that have a good match",
112 "required": False,
113 "deprecated": True,
114 "schema": Is(parameters_schema),
115 "name": "item-query",
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 )