Coverage for tests/test_tutorial/test_pydantic_v1_in_v2/test_tutorial004.py: 100%
25 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
1import sys 1abcdefg
3import pytest 1abcdefg
4from fastapi._compat import PYDANTIC_V2 1abcdefg
5from inline_snapshot import snapshot 1abcdefg
7from tests.utils import skip_module_if_py_gte_314 1abcdefg
9if sys.version_info >= (3, 14): 1abcdefg
10 skip_module_if_py_gte_314() 1g
12if not PYDANTIC_V2: 1abcdef
13 pytest.skip("This test is only for Pydantic v2", allow_module_level=True) 1abcdef
16import importlib 1abcdef
18from fastapi.testclient import TestClient 1abcdef
20from ...utils import needs_py39, needs_py310 1abcdef
23@pytest.fixture( 1abcdef
24 name="client",
25 params=[
26 "tutorial004_an",
27 pytest.param("tutorial004_an_py39", marks=needs_py39),
28 pytest.param("tutorial004_an_py310", marks=needs_py310),
29 ],
30)
31def get_client(request: pytest.FixtureRequest): 1abcdef
32 mod = importlib.import_module(f"docs_src.pydantic_v1_in_v2.{request.param}") 1abcdef
34 c = TestClient(mod.app) 1abcdef
35 return c 1abcdef
38def test_call(client: TestClient): 1abcdef
39 response = client.post("/items/", json={"item": {"name": "Foo", "size": 3.4}}) 1hijklm
40 assert response.status_code == 200, response.text 1hijklm
41 assert response.json() == { 1hijklm
42 "name": "Foo",
43 "description": None,
44 "size": 3.4,
45 }
48def test_openapi_schema(client: TestClient): 1abcdef
49 response = client.get("/openapi.json") 1nopqrs
50 assert response.status_code == 200, response.text 1nopqrs
51 assert response.json() == snapshot( 1nopqrs
52 {
53 "openapi": "3.1.0",
54 "info": {"title": "FastAPI", "version": "0.1.0"},
55 "paths": {
56 "/items/": {
57 "post": {
58 "summary": "Create Item",
59 "operationId": "create_item_items__post",
60 "requestBody": {
61 "content": {
62 "application/json": {
63 "schema": {
64 "allOf": [
65 {
66 "$ref": "#/components/schemas/Body_create_item_items__post"
67 }
68 ],
69 "title": "Body",
70 }
71 }
72 },
73 "required": True,
74 },
75 "responses": {
76 "200": {
77 "description": "Successful Response",
78 "content": {
79 "application/json": {
80 "schema": {"$ref": "#/components/schemas/Item"}
81 }
82 },
83 },
84 "422": {
85 "description": "Validation Error",
86 "content": {
87 "application/json": {
88 "schema": {
89 "$ref": "#/components/schemas/HTTPValidationError"
90 }
91 }
92 },
93 },
94 },
95 }
96 }
97 },
98 "components": {
99 "schemas": {
100 "Body_create_item_items__post": {
101 "properties": {
102 "item": {
103 "allOf": [{"$ref": "#/components/schemas/Item"}],
104 "title": "Item",
105 }
106 },
107 "type": "object",
108 "required": ["item"],
109 "title": "Body_create_item_items__post",
110 },
111 "HTTPValidationError": {
112 "properties": {
113 "detail": {
114 "items": {
115 "$ref": "#/components/schemas/ValidationError"
116 },
117 "type": "array",
118 "title": "Detail",
119 }
120 },
121 "type": "object",
122 "title": "HTTPValidationError",
123 },
124 "Item": {
125 "properties": {
126 "name": {"type": "string", "title": "Name"},
127 "description": {"type": "string", "title": "Description"},
128 "size": {"type": "number", "title": "Size"},
129 },
130 "type": "object",
131 "required": ["name", "size"],
132 "title": "Item",
133 },
134 "ValidationError": {
135 "properties": {
136 "loc": {
137 "items": {
138 "anyOf": [{"type": "string"}, {"type": "integer"}]
139 },
140 "type": "array",
141 "title": "Location",
142 },
143 "msg": {"type": "string", "title": "Message"},
144 "type": {"type": "string", "title": "Error Type"},
145 },
146 "type": "object",
147 "required": ["loc", "msg", "type"],
148 "title": "ValidationError",
149 },
150 }
151 },
152 }
153 )