Coverage for tests / test_tutorial / test_query_params_str_validations / test_tutorial004.py: 100%
26 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-12 18:15 +0000
1import importlib 1abdc
3import pytest 1abdc
4from fastapi.testclient import TestClient 1abdc
5from inline_snapshot import snapshot 1abdc
7from ...utils import needs_py310 1abdc
10@pytest.fixture( 1abdc
11 name="client",
12 params=[
13 pytest.param("tutorial004_py310", marks=needs_py310),
14 pytest.param("tutorial004_an_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abdc
18 mod = importlib.import_module( 1abc
19 f"docs_src.query_params_str_validations.{request.param}"
20 )
22 client = TestClient(mod.app) 1abc
23 return client 1abc
26def test_query_params_str_validations_no_query(client: TestClient): 1abdc
27 response = client.get("/items/") 1efg
28 assert response.status_code == 200 1efg
29 assert response.json() == {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]} 1efg
32def test_query_params_str_validations_q_fixedquery(client: TestClient): 1abdc
33 response = client.get("/items/", params={"q": "fixedquery"}) 1hij
34 assert response.status_code == 200 1hij
35 assert response.json() == { 1hij
36 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
37 "q": "fixedquery",
38 }
41def test_query_params_str_validations_q_nonregexquery(client: TestClient): 1abdc
42 response = client.get("/items/", params={"q": "nonregexquery"}) 1klm
43 assert response.status_code == 422 1klm
44 assert response.json() == { 1klm
45 "detail": [
46 {
47 "type": "string_pattern_mismatch",
48 "loc": ["query", "q"],
49 "msg": "String should match pattern '^fixedquery$'",
50 "input": "nonregexquery",
51 "ctx": {"pattern": "^fixedquery$"},
52 }
53 ]
54 }
57def test_openapi_schema(client: TestClient): 1abdc
58 response = client.get("/openapi.json") 1nop
59 assert response.status_code == 200, response.text 1nop
60 assert response.json() == snapshot( 1nop
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 "maxLength": 50,
94 "pattern": "^fixedquery$",
95 },
96 {"type": "null"},
97 ],
98 "title": "Q",
99 },
100 "name": "q",
101 "in": "query",
102 }
103 ],
104 }
105 }
106 },
107 "components": {
108 "schemas": {
109 "ValidationError": {
110 "title": "ValidationError",
111 "required": ["loc", "msg", "type"],
112 "type": "object",
113 "properties": {
114 "loc": {
115 "title": "Location",
116 "type": "array",
117 "items": {
118 "anyOf": [{"type": "string"}, {"type": "integer"}]
119 },
120 },
121 "msg": {"title": "Message", "type": "string"},
122 "type": {"title": "Error Type", "type": "string"},
123 "input": {"title": "Input"},
124 "ctx": {"title": "Context", "type": "object"},
125 },
126 },
127 "HTTPValidationError": {
128 "title": "HTTPValidationError",
129 "type": "object",
130 "properties": {
131 "detail": {
132 "title": "Detail",
133 "type": "array",
134 "items": {
135 "$ref": "#/components/schemas/ValidationError"
136 },
137 }
138 },
139 },
140 }
141 },
142 }
143 )