Coverage for tests / test_tutorial / test_query_params_str_validations / test_tutorial005.py: 100%
25 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
8@pytest.fixture( 1abdc
9 name="client",
10 params=[
11 pytest.param("tutorial005_py310"),
12 pytest.param("tutorial005_an_py310"),
13 ],
14)
15def get_client(request: pytest.FixtureRequest): 1abdc
16 mod = importlib.import_module( 1abc
17 f"docs_src.query_params_str_validations.{request.param}"
18 )
20 client = TestClient(mod.app) 1abc
21 return client 1abc
24def test_query_params_str_validations_no_query(client: TestClient): 1abdc
25 response = client.get("/items/") 1efg
26 assert response.status_code == 200 1efg
27 assert response.json() == { 1efg
28 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
29 "q": "fixedquery",
30 }
33def test_query_params_str_validations_q_query(client: TestClient): 1abdc
34 response = client.get("/items/", params={"q": "query"}) 1hij
35 assert response.status_code == 200 1hij
36 assert response.json() == { 1hij
37 "items": [{"item_id": "Foo"}, {"item_id": "Bar"}],
38 "q": "query",
39 }
42def test_query_params_str_validations_q_short(client: TestClient): 1abdc
43 response = client.get("/items/", params={"q": "fa"}) 1klm
44 assert response.status_code == 422 1klm
45 assert response.json() == { 1klm
46 "detail": [
47 {
48 "type": "string_too_short",
49 "loc": ["query", "q"],
50 "msg": "String should have at least 3 characters",
51 "input": "fa",
52 "ctx": {"min_length": 3},
53 }
54 ]
55 }
58def test_openapi_schema(client: TestClient): 1abdc
59 response = client.get("/openapi.json") 1nop
60 assert response.status_code == 200, response.text 1nop
61 assert response.json() == snapshot( 1nop
62 {
63 "openapi": "3.1.0",
64 "info": {"title": "FastAPI", "version": "0.1.0"},
65 "paths": {
66 "/items/": {
67 "get": {
68 "responses": {
69 "200": {
70 "description": "Successful Response",
71 "content": {"application/json": {"schema": {}}},
72 },
73 "422": {
74 "description": "Validation Error",
75 "content": {
76 "application/json": {
77 "schema": {
78 "$ref": "#/components/schemas/HTTPValidationError"
79 }
80 }
81 },
82 },
83 },
84 "summary": "Read Items",
85 "operationId": "read_items_items__get",
86 "parameters": [
87 {
88 "required": False,
89 "schema": {
90 "type": "string",
91 "default": "fixedquery",
92 "minLength": 3,
93 "title": "Q",
94 },
95 "name": "q",
96 "in": "query",
97 }
98 ],
99 }
100 }
101 },
102 "components": {
103 "schemas": {
104 "ValidationError": {
105 "title": "ValidationError",
106 "required": ["loc", "msg", "type"],
107 "type": "object",
108 "properties": {
109 "loc": {
110 "title": "Location",
111 "type": "array",
112 "items": {
113 "anyOf": [{"type": "string"}, {"type": "integer"}]
114 },
115 },
116 "msg": {"title": "Message", "type": "string"},
117 "type": {"title": "Error Type", "type": "string"},
118 "input": {"title": "Input"},
119 "ctx": {"title": "Context", "type": "object"},
120 },
121 },
122 "HTTPValidationError": {
123 "title": "HTTPValidationError",
124 "type": "object",
125 "properties": {
126 "detail": {
127 "title": "Detail",
128 "type": "array",
129 "items": {
130 "$ref": "#/components/schemas/ValidationError"
131 },
132 }
133 },
134 },
135 }
136 },
137 }
138 )