Coverage for tests / test_tutorial / test_body_nested_models / test_tutorial009.py: 100%
23 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1import importlib 1abcd
3import pytest 1abcd
4from fastapi.testclient import TestClient 1abcd
5from inline_snapshot import snapshot 1abcd
8@pytest.fixture( 1abcd
9 name="client",
10 params=[
11 "tutorial009_py310",
12 ],
13)
14def get_client(request: pytest.FixtureRequest): 1abcd
15 mod = importlib.import_module(f"docs_src.body_nested_models.{request.param}") 1mnopqrstuv
17 client = TestClient(mod.app) 1mnopqrstuv
18 return client 1mnopqrstuv
21def test_post_body(client: TestClient): 1abcd
22 data = {"2": 2.2, "3": 3.3} 1efgh
23 response = client.post("/index-weights/", json=data) 1efgh
24 assert response.status_code == 200, response.text 1efgh
25 assert response.json() == data 1efgh
28def test_post_invalid_body(client: TestClient): 1abcd
29 data = {"foo": 2.2, "3": 3.3} 1ijkl
30 response = client.post("/index-weights/", json=data) 1ijkl
31 assert response.status_code == 422, response.text 1ijkl
32 assert response.json() == { 1ijkl
33 "detail": [
34 {
35 "type": "int_parsing",
36 "loc": ["body", "foo", "[key]"],
37 "msg": "Input should be a valid integer, unable to parse string as an integer",
38 "input": "foo",
39 }
40 ]
41 }
44def test_openapi_schema(client: TestClient): 1abcd
45 response = client.get("/openapi.json") 1wxyz
46 assert response.status_code == 200, response.text 1wxyz
47 assert response.json() == snapshot( 1wxyz
48 {
49 "openapi": "3.1.0",
50 "info": {"title": "FastAPI", "version": "0.1.0"},
51 "paths": {
52 "/index-weights/": {
53 "post": {
54 "responses": {
55 "200": {
56 "description": "Successful Response",
57 "content": {"application/json": {"schema": {}}},
58 },
59 "422": {
60 "description": "Validation Error",
61 "content": {
62 "application/json": {
63 "schema": {
64 "$ref": "#/components/schemas/HTTPValidationError"
65 }
66 }
67 },
68 },
69 },
70 "summary": "Create Index Weights",
71 "operationId": "create_index_weights_index_weights__post",
72 "requestBody": {
73 "content": {
74 "application/json": {
75 "schema": {
76 "title": "Weights",
77 "type": "object",
78 "additionalProperties": {"type": "number"},
79 }
80 }
81 },
82 "required": True,
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 "input": {"title": "Input"},
104 "ctx": {"title": "Context", "type": "object"},
105 },
106 },
107 "HTTPValidationError": {
108 "title": "HTTPValidationError",
109 "type": "object",
110 "properties": {
111 "detail": {
112 "title": "Detail",
113 "type": "array",
114 "items": {
115 "$ref": "#/components/schemas/ValidationError"
116 },
117 }
118 },
119 },
120 }
121 },
122 }
123 )