Debian Codegraph @

tests/test_tutorial/test_query_params_str_validations/test_tutorial011.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("tutorial011_py310", marks=needs_py310),
14 pytest.param("tutorial011_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_multi_query_values(client: TestClient):
27 url = "/items/?q=foo&q=bar"
28 response = client.get(url)
29 assert response.status_code == 200, response.text
30 assert response.json() == {"q": ["foo", "bar"]}
31
32
33 def test_query_no_values(client: TestClient):
34 url = "/items/"
35 response = client.get(url)
36 assert response.status_code == 200, response.text
37 assert response.json() == {"q": None}
38
39
40 def test_openapi_schema(client: TestClient):
41 response = client.get("/openapi.json")
42 assert response.status_code == 200, response.text
43 assert response.json() == snapshot(
44 {
45 "openapi": "3.1.0",
46 "info": {"title": "FastAPI", "version": "0.1.0"},
47 "paths": {
48 "/items/": {
49 "get": {
50 "responses": {
51 "200": {
52 "description": "Successful Response",
53 "content": {"application/json": {"schema": {}}},
54 },
55 "422": {
56 "description": "Validation Error",
57 "content": {
58 "application/json": {
59 "schema": {
60 "$ref": "#/components/schemas/HTTPValidationError"
61 }
62 }
63 },
64 },
65 },
66 "summary": "Read Items",
67 "operationId": "read_items_items__get",
68 "parameters": [
69 {
70 "required": False,
71 "schema": {
72 "anyOf": [
73 {"type": "array", "items": {"type": "string"}},
74 {"type": "null"},
75 ],
76 "title": "Q",
77 },
78 "name": "q",
79 "in": "query",
80 }
81 ],
82 }
83 }
84 },
85 "components": {
86 "schemas": {
87 "ValidationError": {
88 "title": "ValidationError",
89 "required": ["loc", "msg", "type"],
90 "type": "object",
91 "properties": {
92 "loc": {
93 "title": "Location",
94 "type": "array",
95 "items": {
96 "anyOf": [{"type": "string"}, {"type": "integer"}]
97 },
98 },
99 "msg": {"title": "Message", "type": "string"},
100 "type": {"title": "Error Type", "type": "string"},
101 "input": {"title": "Input"},
102 "ctx": {"title": "Context", "type": "object"},
103 },
104 },
105 "HTTPValidationError": {
106 "title": "HTTPValidationError",
107 "type": "object",
108 "properties": {
109 "detail": {
110 "title": "Detail",
111 "type": "array",
112 "items": {
113 "$ref": "#/components/schemas/ValidationError"
114 },
115 }
116 },
117 },
118 }
119 },
120 }
121 )