Coverage for tests/test_tutorial/test_schema_extra_example/test_tutorial001.py: 100%
17 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 fastapi.testclient import TestClient 1abcde
4from ...utils import needs_pydanticv2 1abcde
7@pytest.fixture(name="client") 1abcde
8def get_client(): 1abcde
9 from docs_src.schema_extra_example.tutorial001 import app 1abcde
11 client = TestClient(app) 1abcde
12 return client 1abcde
15@needs_pydanticv2 1abcde
16def test_post_body_example(client: TestClient): 1abcde
17 response = client.put( 1abcde
18 "/items/5",
19 json={
20 "name": "Foo",
21 "description": "A very nice Item",
22 "price": 35.4,
23 "tax": 3.2,
24 },
25 )
26 assert response.status_code == 200 1abcde
29@needs_pydanticv2 1abcde
30def test_openapi_schema(client: TestClient): 1abcde
31 response = client.get("/openapi.json") 1abcde
32 assert response.status_code == 200, response.text 1abcde
33 # insert_assert(response.json())
34 assert response.json() == { 1abcde
35 "openapi": "3.1.0",
36 "info": {"title": "FastAPI", "version": "0.1.0"},
37 "paths": {
38 "/items/{item_id}": {
39 "put": {
40 "summary": "Update Item",
41 "operationId": "update_item_items__item_id__put",
42 "parameters": [
43 {
44 "name": "item_id",
45 "in": "path",
46 "required": True,
47 "schema": {"type": "integer", "title": "Item Id"},
48 }
49 ],
50 "requestBody": {
51 "required": True,
52 "content": {
53 "application/json": {
54 "schema": {"$ref": "#/components/schemas/Item"}
55 }
56 },
57 },
58 "responses": {
59 "200": {
60 "description": "Successful Response",
61 "content": {"application/json": {"schema": {}}},
62 },
63 "422": {
64 "description": "Validation Error",
65 "content": {
66 "application/json": {
67 "schema": {
68 "$ref": "#/components/schemas/HTTPValidationError"
69 }
70 }
71 },
72 },
73 },
74 }
75 }
76 },
77 "components": {
78 "schemas": {
79 "HTTPValidationError": {
80 "properties": {
81 "detail": {
82 "items": {"$ref": "#/components/schemas/ValidationError"},
83 "type": "array",
84 "title": "Detail",
85 }
86 },
87 "type": "object",
88 "title": "HTTPValidationError",
89 },
90 "Item": {
91 "properties": {
92 "name": {"type": "string", "title": "Name"},
93 "description": {
94 "anyOf": [{"type": "string"}, {"type": "null"}],
95 "title": "Description",
96 },
97 "price": {"type": "number", "title": "Price"},
98 "tax": {
99 "anyOf": [{"type": "number"}, {"type": "null"}],
100 "title": "Tax",
101 },
102 },
103 "type": "object",
104 "required": ["name", "price"],
105 "title": "Item",
106 "examples": [
107 {
108 "description": "A very nice Item",
109 "name": "Foo",
110 "price": 35.4,
111 "tax": 3.2,
112 }
113 ],
114 },
115 "ValidationError": {
116 "properties": {
117 "loc": {
118 "items": {
119 "anyOf": [{"type": "string"}, {"type": "integer"}]
120 },
121 "type": "array",
122 "title": "Location",
123 },
124 "msg": {"type": "string", "title": "Message"},
125 "type": {"type": "string", "title": "Error Type"},
126 },
127 "type": "object",
128 "required": ["loc", "msg", "type"],
129 "title": "ValidationError",
130 },
131 }
132 },
133 }