Debian Codegraph @

tests/test_tutorial/test_query_params_str_validations/test_tutorial012.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7
8 @pytest.fixture(
9 name="client",
10 params=[
11 pytest.param("tutorial012_py310"),
12 pytest.param("tutorial012_an_py310"),
13 ],
14 )
15 def get_client(request: pytest.FixtureRequest):
16 mod = importlib.import_module(
17 f"docs_src.query_params_str_validations.{request.param}"
18 )
19
20 client = TestClient(mod.app)
21 return client
22
23
24 def test_default_query_values(client: TestClient):
25 url = "/items/"
26 response = client.get(url)
27 assert response.status_code == 200, response.text
28 assert response.json() == {"q": ["foo", "bar"]}
29
30
31 def test_multi_query_values(client: TestClient):
32 url = "/items/?q=baz&q=foobar"
33 response = client.get(url)
34 assert response.status_code == 200, response.text
35 assert response.json() == {"q": ["baz", "foobar"]}
36
37
38 def test_openapi_schema(client: TestClient):
39 response = client.get("/openapi.json")
40 assert response.status_code == 200, response.text
41 assert response.json() == snapshot(
42 {
43 "openapi": "3.1.0",
44 "info": {"title": "FastAPI", "version": "0.1.0"},
45 "paths": {
46 "/items/": {
47 "get": {
48 "responses": {
49 "200": {
50 "description": "Successful Response",
51 "content": {"application/json": {"schema": {}}},
52 },
53 "422": {
54 "description": "Validation Error",
55 "content": {
56 "application/json": {
57 "schema": {
58 "$ref": "#/components/schemas/HTTPValidationError"
59 }
60 }
61 },
62 },
63 },
64 "summary": "Read Items",
65 "operationId": "read_items_items__get",
66 "parameters": [
67 {
68 "required": False,
69 "schema": {
70 "title": "Q",
71 "type": "array",
72 "items": {"type": "string"},
73 "default": ["foo", "bar"],
74 },
75 "name": "q",
76 "in": "query",
77 }
78 ],
79 }
80 }
81 },
82 "components": {
83 "schemas": {
84 "ValidationError": {
85 "title": "ValidationError",
86 "required": ["loc", "msg", "type"],
87 "type": "object",
88 "properties": {
89 "loc": {
90 "title": "Location",
91 "type": "array",
92 "items": {
93 "anyOf": [{"type": "string"}, {"type": "integer"}]
94 },
95 },
96 "msg": {"title": "Message", "type": "string"},
97 "type": {"title": "Error Type", "type": "string"},
98 "input": {"title": "Input"},
99 "ctx": {"title": "Context", "type": "object"},
100 },
101 },
102 "HTTPValidationError": {
103 "title": "HTTPValidationError",
104 "type": "object",
105 "properties": {
106 "detail": {
107 "title": "Detail",
108 "type": "array",
109 "items": {
110 "$ref": "#/components/schemas/ValidationError"
111 },
112 }
113 },
114 },
115 }
116 },
117 }
118 )