Coverage for tests/test_tutorial/test_query_params/test_tutorial006.py: 100%
22 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsDict 1abcdef
5from fastapi.testclient import TestClient 1abcdef
7from ...utils import needs_py310 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial006",
14 pytest.param("tutorial006_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abcdef
18 mod = importlib.import_module(f"docs_src.query_params.{request.param}") 1abcdef
20 c = TestClient(mod.app) 1abcdef
21 return c 1abcdef
24def test_foo_needy_very(client: TestClient): 1abcdef
25 response = client.get("/items/foo?needy=very") 1ghijkl
26 assert response.status_code == 200 1ghijkl
27 assert response.json() == { 1ghijkl
28 "item_id": "foo",
29 "needy": "very",
30 "skip": 0,
31 "limit": None,
32 }
35def test_foo_no_needy(client: TestClient): 1abcdef
36 response = client.get("/items/foo?skip=a&limit=b") 1mnopqr
37 assert response.status_code == 422 1mnopqr
38 assert response.json() == IsDict( 1mnopqr
39 {
40 "detail": [
41 {
42 "type": "missing",
43 "loc": ["query", "needy"],
44 "msg": "Field required",
45 "input": None,
46 },
47 {
48 "type": "int_parsing",
49 "loc": ["query", "skip"],
50 "msg": "Input should be a valid integer, unable to parse string as an integer",
51 "input": "a",
52 },
53 {
54 "type": "int_parsing",
55 "loc": ["query", "limit"],
56 "msg": "Input should be a valid integer, unable to parse string as an integer",
57 "input": "b",
58 },
59 ]
60 }
61 ) | IsDict(
62 # TODO: remove when deprecating Pydantic v1
63 {
64 "detail": [
65 {
66 "loc": ["query", "needy"],
67 "msg": "field required",
68 "type": "value_error.missing",
69 },
70 {
71 "loc": ["query", "skip"],
72 "msg": "value is not a valid integer",
73 "type": "type_error.integer",
74 },
75 {
76 "loc": ["query", "limit"],
77 "msg": "value is not a valid integer",
78 "type": "type_error.integer",
79 },
80 ]
81 }
82 )
85def test_openapi_schema(client: TestClient): 1abcdef
86 response = client.get("/openapi.json") 1stuvwx
87 assert response.status_code == 200 1stuvwx
88 assert response.json() == { 1stuvwx
89 "openapi": "3.1.0",
90 "info": {"title": "FastAPI", "version": "0.1.0"},
91 "paths": {
92 "/items/{item_id}": {
93 "get": {
94 "responses": {
95 "200": {
96 "description": "Successful Response",
97 "content": {"application/json": {"schema": {}}},
98 },
99 "422": {
100 "description": "Validation Error",
101 "content": {
102 "application/json": {
103 "schema": {
104 "$ref": "#/components/schemas/HTTPValidationError"
105 }
106 }
107 },
108 },
109 },
110 "summary": "Read User Item",
111 "operationId": "read_user_item_items__item_id__get",
112 "parameters": [
113 {
114 "required": True,
115 "schema": {"title": "Item Id", "type": "string"},
116 "name": "item_id",
117 "in": "path",
118 },
119 {
120 "required": True,
121 "schema": {"title": "Needy", "type": "string"},
122 "name": "needy",
123 "in": "query",
124 },
125 {
126 "required": False,
127 "schema": {
128 "title": "Skip",
129 "type": "integer",
130 "default": 0,
131 },
132 "name": "skip",
133 "in": "query",
134 },
135 {
136 "required": False,
137 "schema": IsDict(
138 {
139 "anyOf": [{"type": "integer"}, {"type": "null"}],
140 "title": "Limit",
141 }
142 )
143 | IsDict(
144 # TODO: remove when deprecating Pydantic v1
145 {"title": "Limit", "type": "integer"}
146 ),
147 "name": "limit",
148 "in": "query",
149 },
150 ],
151 }
152 }
153 },
154 "components": {
155 "schemas": {
156 "ValidationError": {
157 "title": "ValidationError",
158 "required": ["loc", "msg", "type"],
159 "type": "object",
160 "properties": {
161 "loc": {
162 "title": "Location",
163 "type": "array",
164 "items": {
165 "anyOf": [{"type": "string"}, {"type": "integer"}]
166 },
167 },
168 "msg": {"title": "Message", "type": "string"},
169 "type": {"title": "Error Type", "type": "string"},
170 },
171 },
172 "HTTPValidationError": {
173 "title": "HTTPValidationError",
174 "type": "object",
175 "properties": {
176 "detail": {
177 "title": "Detail",
178 "type": "array",
179 "items": {"$ref": "#/components/schemas/ValidationError"},
180 }
181 },
182 },
183 }
184 },
185 }