Coverage for tests/test_tutorial/test_dependencies/test_tutorial001.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 1abcde
3from fastapi.testclient import TestClient 1abcde
5from docs_src.dependencies.tutorial001 import app 1abcde
7client = TestClient(app) 1abcde
10@pytest.mark.parametrize( 1abcde
11 "path,expected_status,expected_response",
12 [
13 ("/items", 200, {"q": None, "skip": 0, "limit": 100}),
14 ("/items?q=foo", 200, {"q": "foo", "skip": 0, "limit": 100}),
15 ("/items?q=foo&skip=5", 200, {"q": "foo", "skip": 5, "limit": 100}),
16 ("/items?q=foo&skip=5&limit=30", 200, {"q": "foo", "skip": 5, "limit": 30}),
17 ("/users", 200, {"q": None, "skip": 0, "limit": 100}),
18 ],
19)
20def test_get(path, expected_status, expected_response): 1abcde
21 response = client.get(path) 1abcde
22 assert response.status_code == expected_status 1abcde
23 assert response.json() == expected_response 1abcde
26def test_openapi_schema(): 1abcde
27 response = client.get("/openapi.json") 1abcde
28 assert response.status_code == 200, response.text 1abcde
29 assert response.json() == { 1abcde
30 "openapi": "3.1.0",
31 "info": {"title": "FastAPI", "version": "0.1.0"},
32 "paths": {
33 "/items/": {
34 "get": {
35 "responses": {
36 "200": {
37 "description": "Successful Response",
38 "content": {"application/json": {"schema": {}}},
39 },
40 "422": {
41 "description": "Validation Error",
42 "content": {
43 "application/json": {
44 "schema": {
45 "$ref": "#/components/schemas/HTTPValidationError"
46 }
47 }
48 },
49 },
50 },
51 "summary": "Read Items",
52 "operationId": "read_items_items__get",
53 "parameters": [
54 {
55 "required": False,
56 "schema": IsDict(
57 {
58 "anyOf": [{"type": "string"}, {"type": "null"}],
59 "title": "Q",
60 }
61 )
62 | IsDict(
63 # TODO: remove when deprecating Pydantic v1
64 {"title": "Q", "type": "string"}
65 ),
66 "name": "q",
67 "in": "query",
68 },
69 {
70 "required": False,
71 "schema": {
72 "title": "Skip",
73 "type": "integer",
74 "default": 0,
75 },
76 "name": "skip",
77 "in": "query",
78 },
79 {
80 "required": False,
81 "schema": {
82 "title": "Limit",
83 "type": "integer",
84 "default": 100,
85 },
86 "name": "limit",
87 "in": "query",
88 },
89 ],
90 }
91 },
92 "/users/": {
93 "get": {
94 "responses": {
95 "200": {
96 "description": "Successful Response",
97 "content": {"application/json": {"schema": {}}},
98 },
99 "422": {
100 "description": "Validation Error",
101 "content": {
102 "application/json": {
103 "schema": {
104 "$ref": "#/components/schemas/HTTPValidationError"
105 }
106 }
107 },
108 },
109 },
110 "summary": "Read Users",
111 "operationId": "read_users_users__get",
112 "parameters": [
113 {
114 "required": False,
115 "schema": IsDict(
116 {
117 "anyOf": [{"type": "string"}, {"type": "null"}],
118 "title": "Q",
119 }
120 )
121 | IsDict(
122 # TODO: remove when deprecating Pydantic v1
123 {"title": "Q", "type": "string"}
124 ),
125 "name": "q",
126 "in": "query",
127 },
128 {
129 "required": False,
130 "schema": {
131 "title": "Skip",
132 "type": "integer",
133 "default": 0,
134 },
135 "name": "skip",
136 "in": "query",
137 },
138 {
139 "required": False,
140 "schema": {
141 "title": "Limit",
142 "type": "integer",
143 "default": 100,
144 },
145 "name": "limit",
146 "in": "query",
147 },
148 ],
149 }
150 },
151 },
152 "components": {
153 "schemas": {
154 "ValidationError": {
155 "title": "ValidationError",
156 "required": ["loc", "msg", "type"],
157 "type": "object",
158 "properties": {
159 "loc": {
160 "title": "Location",
161 "type": "array",
162 "items": {
163 "anyOf": [{"type": "string"}, {"type": "integer"}]
164 },
165 },
166 "msg": {"title": "Message", "type": "string"},
167 "type": {"title": "Error Type", "type": "string"},
168 },
169 },
170 "HTTPValidationError": {
171 "title": "HTTPValidationError",
172 "type": "object",
173 "properties": {
174 "detail": {
175 "title": "Detail",
176 "type": "array",
177 "items": {"$ref": "#/components/schemas/ValidationError"},
178 }
179 },
180 },
181 }
182 },
183 }