Skip to content

Commit 89fa015

Browse files
committed
refactor: reduce cognitive complexity in UploadFile schema test
1 parent dd4d8a7 commit 89fa015

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

tests/functional/event_handler/_pydantic/test_uploadfile_openapi_schema.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
class TestUploadFileOpenAPISchema:
1010
"""Test UploadFile OpenAPI schema generation."""
1111

12-
def test_upload_file_openapi_schema(self):
13-
"""Test OpenAPI schema generation with UploadFile."""
12+
def _create_test_app(self):
13+
"""Create test application with upload endpoints."""
1414
app = APIGatewayRestResolver()
1515

1616
@app.post("/upload-single")
@@ -33,41 +33,55 @@ def upload_multiple_files(
3333
"total_size": primary_file.size + len(secondary_file),
3434
}
3535

36-
# Generate OpenAPI schema
37-
schema = app.get_openapi_schema()
36+
return app
3837

39-
# Print schema for debugging
40-
schema_dict = schema.model_dump()
38+
def _print_multipart_schemas(self, schema_dict):
39+
"""Print multipart form data schemas from paths."""
4140
print("SCHEMA PATHS:")
4241
for path, path_item in schema_dict["paths"].items():
4342
print(f"Path: {path}")
44-
if "post" in path_item:
45-
if "requestBody" in path_item["post"]:
46-
if "content" in path_item["post"]["requestBody"]:
47-
if "multipart/form-data" in path_item["post"]["requestBody"]["content"]:
48-
print(" Found multipart/form-data")
49-
content = path_item["post"]["requestBody"]["content"]["multipart/form-data"]
50-
print(f" Schema: {json.dumps(content, indent=2)}")
5143

44+
# Merged nested if statements
45+
if (
46+
"post" in path_item
47+
and "requestBody" in path_item["post"]
48+
and "content" in path_item["post"]["requestBody"]
49+
and "multipart/form-data" in path_item["post"]["requestBody"]["content"]
50+
):
51+
print(" Found multipart/form-data")
52+
content = path_item["post"]["requestBody"]["content"]["multipart/form-data"]
53+
print(f" Schema: {json.dumps(content, indent=2)}")
54+
55+
def _print_file_components(self, schema_dict):
56+
"""Print file-related components from schema."""
5257
print("\nSCHEMA COMPONENTS:")
53-
if "components" in schema_dict and "schemas" in schema_dict["components"]:
54-
for name, comp_schema in schema_dict["components"]["schemas"].items():
55-
if "file" in name.lower() or "upload" in name.lower():
56-
print(f"Component: {name}")
57-
print(f" {json.dumps(comp_schema, indent=2)}")
58+
components = schema_dict.get("components", {})
59+
schemas = components.get("schemas", {})
60+
61+
for name, comp_schema in schemas.items():
62+
if "file" in name.lower() or "upload" in name.lower():
63+
print(f"Component: {name}")
64+
print(f" {json.dumps(comp_schema, indent=2)}")
65+
66+
def test_upload_file_openapi_schema(self):
67+
"""Test OpenAPI schema generation with UploadFile."""
68+
# Setup test app with file upload endpoints
69+
app = self._create_test_app()
70+
71+
# Generate OpenAPI schema
72+
schema = app.get_openapi_schema()
73+
schema_dict = schema.model_dump()
74+
75+
# Print debug information (optional)
76+
self._print_multipart_schemas(schema_dict)
77+
self._print_file_components(schema_dict)
5878

5979
# Basic verification
6080
paths = schema.paths
6181
assert "/upload-single" in paths
6282
assert "/upload-multiple" in paths
63-
64-
# Verify upload-single endpoint exists
65-
upload_single = paths["/upload-single"]
66-
assert upload_single.post is not None
67-
68-
# Verify upload-multiple endpoint exists
69-
upload_multiple = paths["/upload-multiple"]
70-
assert upload_multiple.post is not None
83+
assert paths["/upload-single"].post is not None
84+
assert paths["/upload-multiple"].post is not None
7185

7286
# Print success
7387
print("\n✅ Basic OpenAPI schema generation tests passed")

0 commit comments

Comments
 (0)