Coverage for tests / test_tutorial / test_response_model / test_tutorial004.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("tutorial004_py310", marks=needs_py310),
14 ],
15)
16def get_client(request: pytest.FixtureRequest): 1abdc
17 mod = importlib.import_module(f"docs_src.response_model.{request.param}") 1abc
19 client = TestClient(mod.app) 1abc
20 return client 1abc
23@pytest.mark.parametrize( 1abdc
24 "url,data",
25 [
26 ("/items/foo", {"name": "Foo", "price": 50.2}),
27 (
28 "/items/bar",
29 {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
30 ),
31 (
32 "/items/baz",
33 {
34 "name": "Baz",
35 "description": None,
36 "price": 50.2,
37 "tax": 10.5,
38 "tags": [],
39 },
40 ),
41 ],
42)
43def test_get(url, data, client: TestClient): 1abdc
44 response = client.get(url) 1efg
45 assert response.status_code == 200, response.text 1efg
46 assert response.json() == data 1efg
49def test_openapi_schema(client: TestClient): 1abdc
50 response = client.get("/openapi.json") 1hij
51 assert response.status_code == 200, response.text 1hij
52 assert response.json() == snapshot( 1hij
53 {
54 "openapi": "3.1.0",
55 "info": {"title": "FastAPI", "version": "0.1.0"},
56 "paths": {
57 "/items/{item_id}": {
58 "get": {
59 "responses": {
60 "200": {
61 "description": "Successful Response",
62 "content": {
63 "application/json": {
64 "schema": {"$ref": "#/components/schemas/Item"}
65 }
66 },
67 },
68 "422": {
69 "description": "Validation Error",
70 "content": {
71 "application/json": {
72 "schema": {
73 "$ref": "#/components/schemas/HTTPValidationError"
74 }
75 }
76 },
77 },
78 },
79 "summary": "Read Item",
80 "operationId": "read_item_items__item_id__get",
81 "parameters": [
82 {
83 "required": True,
84 "schema": {"title": "Item Id", "type": "string"},
85 "name": "item_id",
86 "in": "path",
87 }
88 ],
89 }
90 }
91 },
92 "components": {
93 "schemas": {
94 "Item": {
95 "title": "Item",
96 "required": ["name", "price"],
97 "type": "object",
98 "properties": {
99 "name": {"title": "Name", "type": "string"},
100 "price": {"title": "Price", "type": "number"},
101 "description": {
102 "title": "Description",
103 "anyOf": [{"type": "string"}, {"type": "null"}],
104 },
105 "tax": {"title": "Tax", "type": "number", "default": 10.5},
106 "tags": {
107 "title": "Tags",
108 "type": "array",
109 "items": {"type": "string"},
110 "default": [],
111 },
112 },
113 },
114 "ValidationError": {
115 "title": "ValidationError",
116 "required": ["loc", "msg", "type"],
117 "type": "object",
118 "properties": {
119 "ctx": {"title": "Context", "type": "object"},
120 "input": {"title": "Input"},
121 "loc": {
122 "title": "Location",
123 "type": "array",
124 "items": {
125 "anyOf": [{"type": "string"}, {"type": "integer"}]
126 },
127 },
128 "msg": {"title": "Message", "type": "string"},
129 "type": {"title": "Error Type", "type": "string"},
130 },
131 },
132 "HTTPValidationError": {
133 "title": "HTTPValidationError",
134 "type": "object",
135 "properties": {
136 "detail": {
137 "title": "Detail",
138 "type": "array",
139 "items": {
140 "$ref": "#/components/schemas/ValidationError"
141 },
142 }
143 },
144 },
145 }
146 },
147 }
148 )