Coverage for tests/test_tutorial/test_body_nested_models/test_tutorial009.py: 100%
22 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
6@pytest.fixture(name="client") 1abcde
7def get_client(): 1abcde
8 from docs_src.body_nested_models.tutorial009 import app 1abcde
10 client = TestClient(app) 1abcde
11 return client 1abcde
14def test_post_body(client: TestClient): 1abcde
15 data = {"2": 2.2, "3": 3.3} 1abcde
16 response = client.post("/index-weights/", json=data) 1abcde
17 assert response.status_code == 200, response.text 1abcde
18 assert response.json() == data 1abcde
21def test_post_invalid_body(client: TestClient): 1abcde
22 data = {"foo": 2.2, "3": 3.3} 1abcde
23 response = client.post("/index-weights/", json=data) 1abcde
24 assert response.status_code == 422, response.text 1abcde
25 assert response.json() == IsDict( 1abcde
26 {
27 "detail": [
28 {
29 "type": "int_parsing",
30 "loc": ["body", "foo", "[key]"],
31 "msg": "Input should be a valid integer, unable to parse string as an integer",
32 "input": "foo",
33 }
34 ]
35 }
36 ) | IsDict(
37 # TODO: remove when deprecating Pydantic v1
38 {
39 "detail": [
40 {
41 "loc": ["body", "__key__"],
42 "msg": "value is not a valid integer",
43 "type": "type_error.integer",
44 }
45 ]
46 }
47 )
50def test_openapi_schema(client: TestClient): 1abcde
51 response = client.get("/openapi.json") 1abcde
52 assert response.status_code == 200, response.text 1abcde
53 assert response.json() == { 1abcde
54 "openapi": "3.1.0",
55 "info": {"title": "FastAPI", "version": "0.1.0"},
56 "paths": {
57 "/index-weights/": {
58 "post": {
59 "responses": {
60 "200": {
61 "description": "Successful Response",
62 "content": {"application/json": {"schema": {}}},
63 },
64 "422": {
65 "description": "Validation Error",
66 "content": {
67 "application/json": {
68 "schema": {
69 "$ref": "#/components/schemas/HTTPValidationError"
70 }
71 }
72 },
73 },
74 },
75 "summary": "Create Index Weights",
76 "operationId": "create_index_weights_index_weights__post",
77 "requestBody": {
78 "content": {
79 "application/json": {
80 "schema": {
81 "title": "Weights",
82 "type": "object",
83 "additionalProperties": {"type": "number"},
84 }
85 }
86 },
87 "required": True,
88 },
89 }
90 }
91 },
92 "components": {
93 "schemas": {
94 "ValidationError": {
95 "title": "ValidationError",
96 "required": ["loc", "msg", "type"],
97 "type": "object",
98 "properties": {
99 "loc": {
100 "title": "Location",
101 "type": "array",
102 "items": {
103 "anyOf": [{"type": "string"}, {"type": "integer"}]
104 },
105 },
106 "msg": {"title": "Message", "type": "string"},
107 "type": {"title": "Error Type", "type": "string"},
108 },
109 },
110 "HTTPValidationError": {
111 "title": "HTTPValidationError",
112 "type": "object",
113 "properties": {
114 "detail": {
115 "title": "Detail",
116 "type": "array",
117 "items": {"$ref": "#/components/schemas/ValidationError"},
118 }
119 },
120 },
121 }
122 },
123 }