Coverage for tests / test_tutorial / test_query_params / test_tutorial003.py: 100%
19 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("tutorial003_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest): 1abdc
17 mod = importlib.import_module(f"docs_src.query_params.{request.param}") 1abc
19 client = TestClient(mod.app) 1abc
20 return client 1abc
23@pytest.mark.parametrize( 1abdc
24 ("path", "expected_json"),
25 [
26 (
27 "/items/foo",
28 {
29 "item_id": "foo",
30 "description": "This is an amazing item that has a long description",
31 },
32 ),
33 (
34 "/items/bar?q=somequery",
35 {
36 "item_id": "bar",
37 "q": "somequery",
38 "description": "This is an amazing item that has a long description",
39 },
40 ),
41 (
42 "/items/baz?short=true",
43 {"item_id": "baz"},
44 ),
45 ],
46)
47def test_read_user_item(client: TestClient, path, expected_json): 1abdc
48 response = client.get(path) 1efg
49 assert response.status_code == 200 1efg
50 assert response.json() == expected_json 1efg
53def test_openapi_schema(client: TestClient): 1abdc
54 response = client.get("/openapi.json") 1hij
55 assert response.status_code == 200 1hij
56 assert response.json() == snapshot( 1hij
57 {
58 "openapi": "3.1.0",
59 "info": {"title": "FastAPI", "version": "0.1.0"},
60 "paths": {
61 "/items/{item_id}": {
62 "get": {
63 "summary": "Read Item",
64 "operationId": "read_item_items__item_id__get",
65 "parameters": [
66 {
67 "required": True,
68 "schema": {"title": "Item Id", "type": "string"},
69 "name": "item_id",
70 "in": "path",
71 },
72 {
73 "required": False,
74 "schema": {
75 "title": "Q",
76 "anyOf": [
77 {
78 "type": "string",
79 },
80 {
81 "type": "null",
82 },
83 ],
84 },
85 "name": "q",
86 "in": "query",
87 },
88 {
89 "required": False,
90 "schema": {
91 "title": "Short",
92 "type": "boolean",
93 "default": False,
94 },
95 "name": "short",
96 "in": "query",
97 },
98 ],
99 "responses": {
100 "200": {
101 "description": "Successful Response",
102 "content": {"application/json": {"schema": {}}},
103 },
104 "422": {
105 "content": {
106 "application/json": {
107 "schema": {
108 "$ref": "#/components/schemas/HTTPValidationError",
109 },
110 },
111 },
112 "description": "Validation Error",
113 },
114 },
115 }
116 }
117 },
118 "components": {
119 "schemas": {
120 "ValidationError": {
121 "title": "ValidationError",
122 "required": ["loc", "msg", "type"],
123 "type": "object",
124 "properties": {
125 "loc": {
126 "title": "Location",
127 "type": "array",
128 "items": {
129 "anyOf": [{"type": "string"}, {"type": "integer"}]
130 },
131 },
132 "msg": {"title": "Message", "type": "string"},
133 "type": {"title": "Error Type", "type": "string"},
134 "input": {"title": "Input"},
135 "ctx": {"title": "Context", "type": "object"},
136 },
137 },
138 "HTTPValidationError": {
139 "title": "HTTPValidationError",
140 "type": "object",
141 "properties": {
142 "detail": {
143 "title": "Detail",
144 "type": "array",
145 "items": {
146 "$ref": "#/components/schemas/ValidationError"
147 },
148 }
149 },
150 },
151 }
152 },
153 }
154 )