Coverage for tests/test_tutorial/test_query_params/test_tutorial005.py: 100%
16 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
1from dirty_equals import IsDict 1abcde
2from fastapi.testclient import TestClient 1abcde
4from docs_src.query_params.tutorial005 import app 1abcde
6client = TestClient(app) 1abcde
9def test_foo_needy_very(): 1abcde
10 response = client.get("/items/foo?needy=very") 1abcde
11 assert response.status_code == 200 1abcde
12 assert response.json() == {"item_id": "foo", "needy": "very"} 1abcde
15def test_foo_no_needy(): 1abcde
16 response = client.get("/items/foo") 1abcde
17 assert response.status_code == 422 1abcde
18 assert response.json() == IsDict( 1abcde
19 {
20 "detail": [
21 {
22 "type": "missing",
23 "loc": ["query", "needy"],
24 "msg": "Field required",
25 "input": None,
26 }
27 ]
28 }
29 ) | IsDict(
30 # TODO: remove when deprecating Pydantic v1
31 {
32 "detail": [
33 {
34 "loc": ["query", "needy"],
35 "msg": "field required",
36 "type": "value_error.missing",
37 }
38 ]
39 }
40 )
43def test_openapi_schema(): 1abcde
44 response = client.get("/openapi.json") 1abcde
45 assert response.status_code == 200 1abcde
46 assert response.json() == { 1abcde
47 "openapi": "3.1.0",
48 "info": {"title": "FastAPI", "version": "0.1.0"},
49 "paths": {
50 "/items/{item_id}": {
51 "get": {
52 "responses": {
53 "200": {
54 "description": "Successful Response",
55 "content": {"application/json": {"schema": {}}},
56 },
57 "422": {
58 "description": "Validation Error",
59 "content": {
60 "application/json": {
61 "schema": {
62 "$ref": "#/components/schemas/HTTPValidationError"
63 }
64 }
65 },
66 },
67 },
68 "summary": "Read User Item",
69 "operationId": "read_user_item_items__item_id__get",
70 "parameters": [
71 {
72 "required": True,
73 "schema": {"title": "Item Id", "type": "string"},
74 "name": "item_id",
75 "in": "path",
76 },
77 {
78 "required": True,
79 "schema": {"title": "Needy", "type": "string"},
80 "name": "needy",
81 "in": "query",
82 },
83 ],
84 }
85 }
86 },
87 "components": {
88 "schemas": {
89 "ValidationError": {
90 "title": "ValidationError",
91 "required": ["loc", "msg", "type"],
92 "type": "object",
93 "properties": {
94 "loc": {
95 "title": "Location",
96 "type": "array",
97 "items": {
98 "anyOf": [{"type": "string"}, {"type": "integer"}]
99 },
100 },
101 "msg": {"title": "Message", "type": "string"},
102 "type": {"title": "Error Type", "type": "string"},
103 },
104 },
105 "HTTPValidationError": {
106 "title": "HTTPValidationError",
107 "type": "object",
108 "properties": {
109 "detail": {
110 "title": "Detail",
111 "type": "array",
112 "items": {"$ref": "#/components/schemas/ValidationError"},
113 }
114 },
115 },
116 }
117 },
118 }