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