Coverage for tests/test_tutorial/test_body_nested_models/test_tutorial009.py: 100%
24 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import importlib 1abcdef
3import pytest 1abcdef
4from dirty_equals import IsDict 1abcdef
5from fastapi.testclient import TestClient 1abcdef
7from ...utils import needs_py39 1abcdef
10@pytest.fixture( 1abcdef
11 name="client",
12 params=[
13 "tutorial009",
14 pytest.param("tutorial009_py39", marks=needs_py39),
15 ],
16)
17def get_client(request: pytest.FixtureRequest): 1abcdef
18 mod = importlib.import_module(f"docs_src.body_nested_models.{request.param}") 1abcdef
20 client = TestClient(mod.app) 1abcdef
21 return client 1abcdef
24def test_post_body(client: TestClient): 1abcdef
25 data = {"2": 2.2, "3": 3.3} 1ghijkl
26 response = client.post("/index-weights/", json=data) 1ghijkl
27 assert response.status_code == 200, response.text 1ghijkl
28 assert response.json() == data 1ghijkl
31def test_post_invalid_body(client: TestClient): 1abcdef
32 data = {"foo": 2.2, "3": 3.3} 1mnopqr
33 response = client.post("/index-weights/", json=data) 1mnopqr
34 assert response.status_code == 422, response.text 1mnopqr
35 assert response.json() == IsDict( 1mnopqr
36 {
37 "detail": [
38 {
39 "type": "int_parsing",
40 "loc": ["body", "foo", "[key]"],
41 "msg": "Input should be a valid integer, unable to parse string as an integer",
42 "input": "foo",
43 }
44 ]
45 }
46 ) | IsDict(
47 # TODO: remove when deprecating Pydantic v1
48 {
49 "detail": [
50 {
51 "loc": ["body", "__key__"],
52 "msg": "value is not a valid integer",
53 "type": "type_error.integer",
54 }
55 ]
56 }
57 )
60def test_openapi_schema(client: TestClient): 1abcdef
61 response = client.get("/openapi.json") 1stuvwx
62 assert response.status_code == 200, response.text 1stuvwx
63 assert response.json() == { 1stuvwx
64 "openapi": "3.1.0",
65 "info": {"title": "FastAPI", "version": "0.1.0"},
66 "paths": {
67 "/index-weights/": {
68 "post": {
69 "responses": {
70 "200": {
71 "description": "Successful Response",
72 "content": {"application/json": {"schema": {}}},
73 },
74 "422": {
75 "description": "Validation Error",
76 "content": {
77 "application/json": {
78 "schema": {
79 "$ref": "#/components/schemas/HTTPValidationError"
80 }
81 }
82 },
83 },
84 },
85 "summary": "Create Index Weights",
86 "operationId": "create_index_weights_index_weights__post",
87 "requestBody": {
88 "content": {
89 "application/json": {
90 "schema": {
91 "title": "Weights",
92 "type": "object",
93 "additionalProperties": {"type": "number"},
94 }
95 }
96 },
97 "required": True,
98 },
99 }
100 }
101 },
102 "components": {
103 "schemas": {
104 "ValidationError": {
105 "title": "ValidationError",
106 "required": ["loc", "msg", "type"],
107 "type": "object",
108 "properties": {
109 "loc": {
110 "title": "Location",
111 "type": "array",
112 "items": {
113 "anyOf": [{"type": "string"}, {"type": "integer"}]
114 },
115 },
116 "msg": {"title": "Message", "type": "string"},
117 "type": {"title": "Error Type", "type": "string"},
118 },
119 },
120 "HTTPValidationError": {
121 "title": "HTTPValidationError",
122 "type": "object",
123 "properties": {
124 "detail": {
125 "title": "Detail",
126 "type": "array",
127 "items": {"$ref": "#/components/schemas/ValidationError"},
128 }
129 },
130 },
131 }
132 },
133 }