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-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("tutorial002_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 {"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): 1abdc
37 response = client.get(path) 1efg
38 assert response.status_code == 200 1efg
39 assert response.json() == expected_json 1efg
42def test_openapi_schema(client: TestClient): 1abdc
43 response = client.get("/openapi.json") 1hij
44 assert response.status_code == 200 1hij
45 assert response.json() == snapshot( 1hij
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 )