Debian Codegraph @

tests/test_tutorial/test_query_params_str_validations/test_tutorial001.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("tutorial001_py310", marks=needs_py310),
14 ],
15 )
16 def get_client(request: pytest.FixtureRequest):
17 mod = importlib.import_module(
18 f"docs_src.query_params_str_validations.{request.param}"
19 )
20
21 client = TestClient(mod.app)
22 return client
23
24
25 def test_query_params_str_validations_no_query(client: TestClient):
26 response = client.get("/items/")
27 assert response.status_code == 200
28 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
29
30
31 def test_query_params_str_validations_q_empty_str(client: TestClient):
32 response = client.get("/items/", params={"q": ""})
33 assert response.status_code == 200
34 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
35
36
37 def test_query_params_str_validations_q_query(client: TestClient):
38 response = client.get("/items/", params={"q": "query"})
39 assert response.status_code == 200
40 assert response.json() == {
41 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
42 "q": "query",
43 }
44
45
46 def test_openapi_schema(client: TestClient):
47 response = client.get("/openapi.json")
48 assert response.status_code == 200, response.text
49 assert response.json() == snapshot(
50 {
51 "openapi": "3.1.0",
52 "info": {"title": "FastAPI", "version": "0.1.0"},
53 "paths": {
54 "/items/": {
55 "get": {
56 "responses": {
57 "200": {
58 "description": "Successful Response",
59 "content": {"application/json": {"schema": {}}},
60 },
61 "422": {
62 "description": "Validation Error",
63 "content": {
64 "application/json": {
65 "schema": {
66 "$ref": "#/components/schemas/HTTPValidationError"
67 }
68 }
69 },
70 },
71 },
72 "summary": "Read Items",
73 "operationId": "read_items_items__get",
74 "parameters": [
75 {
76 "required": False,
77 "schema": {
78 "anyOf": [
79 {"type": "string"},
80 {"type": "null"},
81 ],
82 "title": "Q",
83 },
84 "name": "q",
85 "in": "query",
86 }
87 ],
88 }
89 }
90 },
91 "components": {
92 "schemas": {
93 "ValidationError": {
94 "title": "ValidationError",
95 "required": ["loc", "msg", "type"],
96 "type": "object",
97 "properties": {
98 "loc": {
99 "title": "Location",
100 "type": "array",
101 "items": {
102 "anyOf": [{"type": "string"}, {"type": "integer"}]
103 },
104 },
105 "msg": {"title": "Message", "type": "string"},
106 "type": {"title": "Error Type", "type": "string"},
107 "input": {"title": "Input"},
108 "ctx": {"title": "Context", "type": "object"},
109 },
110 },
111 "HTTPValidationError": {
112 "title": "HTTPValidationError",
113 "type": "object",
114 "properties": {
115 "detail": {
116 "title": "Detail",
117 "type": "array",
118 "items": {
119 "$ref": "#/components/schemas/ValidationError"
120 },
121 }
122 },
123 },
124 }
125 },
126 }
127 )