Coverage for tests / test_tutorial / test_query_params / test_tutorial002.py: 100%
19 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1abcd
3import pytest 1abcd
4from fastapi.testclient import TestClient 1abcd
5from inline_snapshot import snapshot 1abcd
7from ...utils import needs_py310 1abcd
10@pytest.fixture( 1abcd
11 name="client",
12 params=[
13 pytest.param("tutorial002_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest): 1abcd
17 mod = importlib.import_module(f"docs_src.query_params.{request.param}") 1efghijklmno
19 client = TestClient(mod.app) 1efghijklmno
20 return client 1efghijklmno
23@pytest.mark.parametrize( 1abcd
24 ("path", "expected_json"),
25 [
26 (
27 "/items/foo",
28 {"item_id": "foo"},
29 ),
30 (
31 "/items/bar?q=somequery",
32 {"item_id": "bar", "q": "somequery"},
33 ),
34 ],
35)
36def test_read_user_item(client: TestClient, path, expected_json): 1abcd
37 response = client.get(path) 1pqrstuvw
38 assert response.status_code == 200 1pqrstuvw
39 assert response.json() == expected_json 1pqrstuvw
42def test_openapi_schema(client: TestClient): 1abcd
43 response = client.get("/openapi.json") 1xyzA
44 assert response.status_code == 200 1xyzA
45 assert response.json() == snapshot( 1xyzA
46 {
47 "openapi": "3.1.0",
48 "info": {"title": "FastAPI", "version": "0.1.0"},
49 "paths": {
50 "/items/{item_id}": {
51 "get": {
52 "summary": "Read Item",
53 "operationId": "read_item_items__item_id__get",
54 "parameters": [
55 {
56 "required": True,
57 "schema": {"title": "Item Id", "type": "string"},
58 "name": "item_id",
59 "in": "path",
60 },
61 {
62 "required": False,
63 "schema": {
64 "title": "Q",
65 "anyOf": [
66 {
67 "type": "string",
68 },
69 {
70 "type": "null",
71 },
72 ],
73 },
74 "name": "q",
75 "in": "query",
76 },
77 ],
78 "responses": {
79 "200": {
80 "description": "Successful Response",
81 "content": {"application/json": {"schema": {}}},
82 },
83 "422": {
84 "content": {
85 "application/json": {
86 "schema": {
87 "$ref": "#/components/schemas/HTTPValidationError",
88 },
89 },
90 },
91 "description": "Validation Error",
92 },
93 },
94 }
95 }
96 },
97 "components": {
98 "schemas": {
99 "ValidationError": {
100 "title": "ValidationError",
101 "required": ["loc", "msg", "type"],
102 "type": "object",
103 "properties": {
104 "loc": {
105 "title": "Location",
106 "type": "array",
107 "items": {
108 "anyOf": [{"type": "string"}, {"type": "integer"}]
109 },
110 },
111 "msg": {"title": "Message", "type": "string"},
112 "type": {"title": "Error Type", "type": "string"},
113 "input": {"title": "Input"},
114 "ctx": {"title": "Context", "type": "object"},
115 },
116 },
117 "HTTPValidationError": {
118 "title": "HTTPValidationError",
119 "type": "object",
120 "properties": {
121 "detail": {
122 "title": "Detail",
123 "type": "array",
124 "items": {
125 "$ref": "#/components/schemas/ValidationError"
126 },
127 }
128 },
129 },
130 }
131 },
132 }
133 )