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