Coverage for tests/test_tutorial/test_pydantic_v1_in_v2/test_tutorial003.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_py310 1abcdef
23@pytest.fixture( 1abcdef
24 name="client",
25 params=[
26 "tutorial003_an",
27 pytest.param("tutorial003_an_py310", marks=needs_py310),
28 ],
29)
30def get_client(request: pytest.FixtureRequest): 1abcdef
31 mod = importlib.import_module(f"docs_src.pydantic_v1_in_v2.{request.param}") 1abcdef
33 c = TestClient(mod.app) 1abcdef
34 return c 1abcdef
37def test_call(client: TestClient): 1abcdef
38 response = client.post("/items/", json={"name": "Foo", "size": 3.4}) 1hijklm
39 assert response.status_code == 200, response.text 1hijklm
40 assert response.json() == { 1hijklm
41 "name": "Foo",
42 "description": None,
43 "size": 3.4,
44 }
47def test_openapi_schema(client: TestClient): 1abcdef
48 response = client.get("/openapi.json") 1nopqrs
49 assert response.status_code == 200, response.text 1nopqrs
50 assert response.json() == snapshot( 1nopqrs
51 {
52 "openapi": "3.1.0",
53 "info": {"title": "FastAPI", "version": "0.1.0"},
54 "paths": {
55 "/items/": {
56 "post": {
57 "summary": "Create Item",
58 "operationId": "create_item_items__post",
59 "requestBody": {
60 "content": {
61 "application/json": {
62 "schema": {
63 "allOf": [
64 {"$ref": "#/components/schemas/Item"}
65 ],
66 "title": "Item",
67 }
68 }
69 },
70 "required": True,
71 },
72 "responses": {
73 "200": {
74 "description": "Successful Response",
75 "content": {
76 "application/json": {
77 "schema": {
78 "$ref": "#/components/schemas/ItemV2"
79 }
80 }
81 },
82 },
83 "422": {
84 "description": "Validation Error",
85 "content": {
86 "application/json": {
87 "schema": {
88 "$ref": "#/components/schemas/HTTPValidationError"
89 }
90 }
91 },
92 },
93 },
94 }
95 }
96 },
97 "components": {
98 "schemas": {
99 "HTTPValidationError": {
100 "properties": {
101 "detail": {
102 "items": {
103 "$ref": "#/components/schemas/ValidationError"
104 },
105 "type": "array",
106 "title": "Detail",
107 }
108 },
109 "type": "object",
110 "title": "HTTPValidationError",
111 },
112 "Item": {
113 "properties": {
114 "name": {"type": "string", "title": "Name"},
115 "description": {"type": "string", "title": "Description"},
116 "size": {"type": "number", "title": "Size"},
117 },
118 "type": "object",
119 "required": ["name", "size"],
120 "title": "Item",
121 },
122 "ItemV2": {
123 "properties": {
124 "name": {"type": "string", "title": "Name"},
125 "description": {
126 "anyOf": [{"type": "string"}, {"type": "null"}],
127 "title": "Description",
128 },
129 "size": {"type": "number", "title": "Size"},
130 },
131 "type": "object",
132 "required": ["name", "size"],
133 "title": "ItemV2",
134 },
135 "ValidationError": {
136 "properties": {
137 "loc": {
138 "items": {
139 "anyOf": [{"type": "string"}, {"type": "integer"}]
140 },
141 "type": "array",
142 "title": "Location",
143 },
144 "msg": {"type": "string", "title": "Message"},
145 "type": {"type": "string", "title": "Error Type"},
146 },
147 "type": "object",
148 "required": ["loc", "msg", "type"],
149 "title": "ValidationError",
150 },
151 }
152 },
153 }
154 )