Debian Codegraph @

tests/test_tutorial/test_query_params_str_validations/test_tutorial007.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("tutorial007_py310", marks=needs_py310),
14 pytest.param("tutorial007_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_fixedquery(client: TestClient):
33 response = client.get("/items/", params={"q": "fixedquery"})
34 assert response.status_code == 200
35 assert response.json() == {
36 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
37 "q": "fixedquery",
38 }
39
40
41 def test_query_params_str_validations_q_fixedquery_too_short(client: TestClient):
42 response = client.get("/items/", params={"q": "fa"})
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": "fa",
51 "ctx": {"min_length": 3},
52 }
53 ]
54 }
55
56
57 def test_openapi_schema(client: TestClient):
58 response = client.get("/openapi.json")
59 assert response.status_code == 200, response.text
60 assert response.json() == snapshot(
61 {
62 "openapi": "3.1.0",
63 "info": {"title": "FastAPI", "version": "0.1.0"},
64 "paths": {
65 "/items/": {
66 "get": {
67 "responses": {
68 "200": {
69 "description": "Successful Response",
70 "content": {"application/json": {"schema": {}}},
71 },
72 "422": {
73 "description": "Validation Error",
74 "content": {
75 "application/json": {
76 "schema": {
77 "$ref": "#/components/schemas/HTTPValidationError"
78 }
79 }
80 },
81 },
82 },
83 "summary": "Read Items",
84 "operationId": "read_items_items__get",
85 "parameters": [
86 {
87 "required": False,
88 "schema": {
89 "anyOf": [
90 {
91 "type": "string",
92 "minLength": 3,
93 },
94 {"type": "null"},
95 ],
96 "title": "Query string",
97 },
98 "name": "q",
99 "in": "query",
100 }
101 ],
102 }
103 }
104 },
105 "components": {
106 "schemas": {
107 "ValidationError": {
108 "title": "ValidationError",
109 "required": ["loc", "msg", "type"],
110 "type": "object",
111 "properties": {
112 "loc": {
113 "title": "Location",
114 "type": "array",
115 "items": {
116 "anyOf": [{"type": "string"}, {"type": "integer"}]
117 },
118 },
119 "msg": {"title": "Message", "type": "string"},
120 "type": {"title": "Error Type", "type": "string"},
121 "input": {"title": "Input"},
122 "ctx": {"title": "Context", "type": "object"},
123 },
124 },
125 "HTTPValidationError": {
126 "title": "HTTPValidationError",
127 "type": "object",
128 "properties": {
129 "detail": {
130 "title": "Detail",
131 "type": "array",
132 "items": {
133 "$ref": "#/components/schemas/ValidationError"
134 },
135 }
136 },
137 },
138 }
139 },
140 }
141 )