Coverage for tests/test_tutorial/test_response_model/test_tutorial004_py310.py: 100%
20 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1import pytest 1deabc
2from dirty_equals import IsDict, IsOneOf 1deabc
3from fastapi.testclient import TestClient 1deabc
5from ...utils import needs_py310 1deabc
8@pytest.fixture(name="client") 1deabc
9def get_client(): 1deabc
10 from docs_src.response_model.tutorial004_py310 import app 1abc
12 client = TestClient(app) 1abc
13 return client 1abc
16@needs_py310 1deabc
17@pytest.mark.parametrize( 1deabc
18 "url,data",
19 [
20 ("/items/foo", {"name": "Foo", "price": 50.2}),
21 (
22 "/items/bar",
23 {"name": "Bar", "description": "The bartenders", "price": 62, "tax": 20.2},
24 ),
25 (
26 "/items/baz",
27 {
28 "name": "Baz",
29 "description": None,
30 "price": 50.2,
31 "tax": 10.5,
32 "tags": [],
33 },
34 ),
35 ],
36)
37def test_get(url, data, client: TestClient): 1deabc
38 response = client.get(url) 1abc
39 assert response.status_code == 200, response.text 1abc
40 assert response.json() == data 1abc
43@needs_py310 1deabc
44def test_openapi_schema(client: TestClient): 1deabc
45 response = client.get("/openapi.json") 1abc
46 assert response.status_code == 200, response.text 1abc
47 assert response.json() == { 1abc
48 "openapi": "3.1.0",
49 "info": {"title": "FastAPI", "version": "0.1.0"},
50 "paths": {
51 "/items/{item_id}": {
52 "get": {
53 "responses": {
54 "200": {
55 "description": "Successful Response",
56 "content": {
57 "application/json": {
58 "schema": {"$ref": "#/components/schemas/Item"}
59 }
60 },
61 },
62 "422": {
63 "description": "Validation Error",
64 "content": {
65 "application/json": {
66 "schema": {
67 "$ref": "#/components/schemas/HTTPValidationError"
68 }
69 }
70 },
71 },
72 },
73 "summary": "Read Item",
74 "operationId": "read_item_items__item_id__get",
75 "parameters": [
76 {
77 "required": True,
78 "schema": {"title": "Item Id", "type": "string"},
79 "name": "item_id",
80 "in": "path",
81 }
82 ],
83 }
84 }
85 },
86 "components": {
87 "schemas": {
88 "Item": {
89 "title": "Item",
90 "required": IsOneOf(
91 ["name", "description", "price", "tax", "tags"],
92 # TODO: remove when deprecating Pydantic v1
93 ["name", "price"],
94 ),
95 "type": "object",
96 "properties": {
97 "name": {"title": "Name", "type": "string"},
98 "price": {"title": "Price", "type": "number"},
99 "description": IsDict(
100 {
101 "title": "Description",
102 "anyOf": [{"type": "string"}, {"type": "null"}],
103 }
104 )
105 | IsDict(
106 # TODO: remove when deprecating Pydantic v1
107 {"title": "Description", "type": "string"}
108 ),
109 "tax": {"title": "Tax", "type": "number", "default": 10.5},
110 "tags": {
111 "title": "Tags",
112 "type": "array",
113 "items": {"type": "string"},
114 "default": [],
115 },
116 },
117 },
118 "ValidationError": {
119 "title": "ValidationError",
120 "required": ["loc", "msg", "type"],
121 "type": "object",
122 "properties": {
123 "loc": {
124 "title": "Location",
125 "type": "array",
126 "items": {
127 "anyOf": [{"type": "string"}, {"type": "integer"}]
128 },
129 },
130 "msg": {"title": "Message", "type": "string"},
131 "type": {"title": "Error Type", "type": "string"},
132 },
133 },
134 "HTTPValidationError": {
135 "title": "HTTPValidationError",
136 "type": "object",
137 "properties": {
138 "detail": {
139 "title": "Detail",
140 "type": "array",
141 "items": {"$ref": "#/components/schemas/ValidationError"},
142 }
143 },
144 },
145 }
146 },
147 }