Debian Codegraph @

tests/test_tutorial/test_query_params_str_validations/test_tutorial006c.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("tutorial006c_py310", marks=needs_py310),
14 pytest.param("tutorial006c_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 client = TestClient(mod.app)
22 return client
23
24
25 @pytest.mark.xfail(
26 reason="Code example is not valid. See https://github.com/fastapi/fastapi/issues/12419"
27 )
28 def test_query_params_str_validations_no_query(client: TestClient):
29 response = client.get("/items/")
30 assert response.status_code == 200
31 assert response.json() == { # pragma: no cover
32 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
33 }
34
35
36 @pytest.mark.xfail(
37 reason="Code example is not valid. See https://github.com/fastapi/fastapi/issues/12419"
38 )
39 def test_query_params_str_validations_empty_str(client: TestClient):
40 response = client.get("/items/?q=")
41 assert response.status_code == 200
42 assert response.json() == { # pragma: no cover
43 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
44 }
45
46
47 def test_query_params_str_validations_q_query(client: TestClient):
48 response = client.get("/items/", params={"q": "query"})
49 assert response.status_code == 200
50 assert response.json() == {
51 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
52 "q": "query",
53 }
54
55
56 def test_query_params_str_validations_q_short(client: TestClient):
57 response = client.get("/items/", params={"q": "fa"})
58 assert response.status_code == 422
59 assert response.json() == {
60 "detail": [
61 {
62 "type": "string_too_short",
63 "loc": ["query", "q"],
64 "msg": "String should have at least 3 characters",
65 "input": "fa",
66 "ctx": {"min_length": 3},
67 }
68 ]
69 }
70
71
72 def test_openapi_schema(client: TestClient):
73 response = client.get("/openapi.json")
74 assert response.status_code == 200, response.text
75 assert response.json() == snapshot(
76 {
77 "openapi": "3.1.0",
78 "info": {"title": "FastAPI", "version": "0.1.0"},
79 "paths": {
80 "/items/": {
81 "get": {
82 "responses": {
83 "200": {
84 "description": "Successful Response",
85 "content": {"application/json": {"schema": {}}},
86 },
87 "422": {
88 "description": "Validation Error",
89 "content": {
90 "application/json": {
91 "schema": {
92 "$ref": "#/components/schemas/HTTPValidationError"
93 }
94 }
95 },
96 },
97 },
98 "summary": "Read Items",
99 "operationId": "read_items_items__get",
100 "parameters": [
101 {
102 "required": True,
103 "schema": {
104 "anyOf": [
105 {"type": "string", "minLength": 3},
106 {"type": "null"},
107 ],
108 "title": "Q",
109 },
110 "name": "q",
111 "in": "query",
112 }
113 ],
114 }
115 }
116 },
117 "components": {
118 "schemas": {
119 "ValidationError": {
120 "title": "ValidationError",
121 "required": ["loc", "msg", "type"],
122 "type": "object",
123 "properties": {
124 "loc": {
125 "title": "Location",
126 "type": "array",
127 "items": {
128 "anyOf": [{"type": "string"}, {"type": "integer"}]
129 },
130 },
131 "msg": {"title": "Message", "type": "string"},
132 "type": {"title": "Error Type", "type": "string"},
133 "input": {"title": "Input"},
134 "ctx": {"title": "Context", "type": "object"},
135 },
136 },
137 "HTTPValidationError": {
138 "title": "HTTPValidationError",
139 "type": "object",
140 "properties": {
141 "detail": {
142 "title": "Detail",
143 "type": "array",
144 "items": {
145 "$ref": "#/components/schemas/ValidationError"
146 },
147 }
148 },
149 },
150 }
151 },
152 }
153 )