Coverage for tests/test_tutorial/test_response_model/test_tutorial005.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, IsOneOf 1abcdef
5from fastapi.testclient import TestClient 1abcdef
7from ...utils import needs_py310 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial005",
14 pytest.param("tutorial005_py310", marks=needs_py310),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abcdef
18 mod = importlib.import_module(f"docs_src.response_model.{request.param}") 1abcdef
20 client = TestClient(mod.app) 1abcdef
21 return client 1abcdef
24def test_read_item_name(client: TestClient): 1abcdef
25 response = client.get("/items/bar/name") 1ghijkl
26 assert response.status_code == 200, response.text 1ghijkl
27 assert response.json() == {"name": "Bar", "description": "The Bar fighters"} 1ghijkl
30def test_read_item_public_data(client: TestClient): 1abcdef
31 response = client.get("/items/bar/public") 1mnopqr
32 assert response.status_code == 200, response.text 1mnopqr
33 assert response.json() == { 1mnopqr
34 "name": "Bar",
35 "description": "The Bar fighters",
36 "price": 62,
37 }
40def test_openapi_schema(client: TestClient): 1abcdef
41 response = client.get("/openapi.json") 1stuvwx
42 assert response.status_code == 200, response.text 1stuvwx
43 assert response.json() == { 1stuvwx
44 "openapi": "3.1.0",
45 "info": {"title": "FastAPI", "version": "0.1.0"},
46 "paths": {
47 "/items/{item_id}/name": {
48 "get": {
49 "responses": {
50 "200": {
51 "description": "Successful Response",
52 "content": {
53 "application/json": {
54 "schema": {"$ref": "#/components/schemas/Item"}
55 }
56 },
57 },
58 "422": {
59 "description": "Validation Error",
60 "content": {
61 "application/json": {
62 "schema": {
63 "$ref": "#/components/schemas/HTTPValidationError"
64 }
65 }
66 },
67 },
68 },
69 "summary": "Read Item Name",
70 "operationId": "read_item_name_items__item_id__name_get",
71 "parameters": [
72 {
73 "required": True,
74 "schema": {"title": "Item Id", "type": "string"},
75 "name": "item_id",
76 "in": "path",
77 }
78 ],
79 }
80 },
81 "/items/{item_id}/public": {
82 "get": {
83 "responses": {
84 "200": {
85 "description": "Successful Response",
86 "content": {
87 "application/json": {
88 "schema": {"$ref": "#/components/schemas/Item"}
89 }
90 },
91 },
92 "422": {
93 "description": "Validation Error",
94 "content": {
95 "application/json": {
96 "schema": {
97 "$ref": "#/components/schemas/HTTPValidationError"
98 }
99 }
100 },
101 },
102 },
103 "summary": "Read Item Public Data",
104 "operationId": "read_item_public_data_items__item_id__public_get",
105 "parameters": [
106 {
107 "required": True,
108 "schema": {"title": "Item Id", "type": "string"},
109 "name": "item_id",
110 "in": "path",
111 }
112 ],
113 }
114 },
115 },
116 "components": {
117 "schemas": {
118 "Item": {
119 "title": "Item",
120 "required": IsOneOf(
121 ["name", "description", "price", "tax"],
122 # TODO: remove when deprecating Pydantic v1
123 ["name", "price"],
124 ),
125 "type": "object",
126 "properties": {
127 "name": {"title": "Name", "type": "string"},
128 "price": {"title": "Price", "type": "number"},
129 "description": IsDict(
130 {
131 "title": "Description",
132 "anyOf": [{"type": "string"}, {"type": "null"}],
133 }
134 )
135 | IsDict(
136 # TODO: remove when deprecating Pydantic v1
137 {"title": "Description", "type": "string"}
138 ),
139 "tax": {"title": "Tax", "type": "number", "default": 10.5},
140 },
141 },
142 "ValidationError": {
143 "title": "ValidationError",
144 "required": ["loc", "msg", "type"],
145 "type": "object",
146 "properties": {
147 "loc": {
148 "title": "Location",
149 "type": "array",
150 "items": {
151 "anyOf": [{"type": "string"}, {"type": "integer"}]
152 },
153 },
154 "msg": {"title": "Message", "type": "string"},
155 "type": {"title": "Error Type", "type": "string"},
156 },
157 },
158 "HTTPValidationError": {
159 "title": "HTTPValidationError",
160 "type": "object",
161 "properties": {
162 "detail": {
163 "title": "Detail",
164 "type": "array",
165 "items": {"$ref": "#/components/schemas/ValidationError"},
166 }
167 },
168 },
169 }
170 },
171 }