Coverage for tests/test_tutorial/test_pydantic_v1_in_v2/test_tutorial002.py: 100%
26 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
13if not PYDANTIC_V2: 1abcdef
14 pytest.skip("This test is only for Pydantic v2", allow_module_level=True) 1abcdef
16import importlib 1abcdef
18import pytest 1abcdef
19from fastapi.testclient import TestClient 1abcdef
21from ...utils import needs_py310 1abcdef
24@pytest.fixture( 1abcdef
25 name="client",
26 params=[
27 "tutorial002_an",
28 pytest.param("tutorial002_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={"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 {"$ref": "#/components/schemas/Item"}
66 ],
67 "title": "Item",
68 }
69 }
70 },
71 "required": True,
72 },
73 "responses": {
74 "200": {
75 "description": "Successful Response",
76 "content": {
77 "application/json": {
78 "schema": {"$ref": "#/components/schemas/Item"}
79 }
80 },
81 },
82 "422": {
83 "description": "Validation Error",
84 "content": {
85 "application/json": {
86 "schema": {
87 "$ref": "#/components/schemas/HTTPValidationError"
88 }
89 }
90 },
91 },
92 },
93 }
94 }
95 },
96 "components": {
97 "schemas": {
98 "HTTPValidationError": {
99 "properties": {
100 "detail": {
101 "items": {
102 "$ref": "#/components/schemas/ValidationError"
103 },
104 "type": "array",
105 "title": "Detail",
106 }
107 },
108 "type": "object",
109 "title": "HTTPValidationError",
110 },
111 "Item": {
112 "properties": {
113 "name": {"type": "string", "title": "Name"},
114 "description": {"type": "string", "title": "Description"},
115 "size": {"type": "number", "title": "Size"},
116 },
117 "type": "object",
118 "required": ["name", "size"],
119 "title": "Item",
120 },
121 "ValidationError": {
122 "properties": {
123 "loc": {
124 "items": {
125 "anyOf": [{"type": "string"}, {"type": "integer"}]
126 },
127 "type": "array",
128 "title": "Location",
129 },
130 "msg": {"type": "string", "title": "Message"},
131 "type": {"type": "string", "title": "Error Type"},
132 },
133 "type": "object",
134 "required": ["loc", "msg", "type"],
135 "title": "ValidationError",
136 },
137 }
138 },
139 }
140 )