1
1
"""Comprehensive tests for UploadFile OpenAPI schema generation and validation coverage."""
2
2
3
- import pytest
4
- from typing_extensions import Annotated
3
+ from typing import Optional , Union
5
4
from unittest .mock import Mock
6
5
6
+ from typing_extensions import Annotated
7
+
7
8
from aws_lambda_powertools .event_handler import APIGatewayRestResolver
8
- from aws_lambda_powertools .event_handler .openapi .params import File , UploadFile , fix_upload_file_schema_references
9
9
from aws_lambda_powertools .event_handler .middlewares .openapi_validation import (
10
- _get_field_value , _resolve_field_type , _convert_value_type
10
+ _convert_value_type ,
11
+ _get_field_value ,
12
+ _resolve_field_type ,
11
13
)
12
- from typing import Union , Optional
14
+ from aws_lambda_powertools . event_handler . openapi . params import File , UploadFile , fix_upload_file_schema_references
13
15
14
16
15
17
class TestUploadFileComprehensiveCoverage :
@@ -28,7 +30,7 @@ def upload_file(file: Annotated[UploadFile, File()]):
28
30
upload_path = schema .paths ["/upload" ]
29
31
request_body = upload_path .post .requestBody
30
32
multipart_content = request_body .content ["multipart/form-data" ]
31
-
33
+
32
34
assert multipart_content .schema_ is not None
33
35
34
36
def test_upload_file_schema_fix_resolves_references (self ):
@@ -43,18 +45,18 @@ def upload_file(file: Annotated[UploadFile, File()]):
43
45
# Convert to dict for processing by fix function
44
46
schema_dict = schema .model_dump ()
45
47
fix_upload_file_schema_references (schema_dict )
46
-
48
+
47
49
# Verify components exist and are processed
48
50
assert "components" in schema_dict
49
51
50
52
def test_upload_file_validation_methods (self ):
51
53
"""Test UploadFile validation methods for coverage."""
52
54
upload_file = UploadFile (file = b"test content" , filename = "test.txt" )
53
-
55
+
54
56
# Test __get_validators__ method
55
57
validators = upload_file .__get_validators__ ()
56
58
assert callable (next (validators ))
57
-
59
+
58
60
# Test _validate_with_info method - this covers lines in validation
59
61
validation_info = Mock ()
60
62
validated = upload_file ._validate_with_info (b"content" , validation_info )
@@ -65,12 +67,12 @@ def test_upload_file_pydantic_schema_methods(self):
65
67
# Test __get_pydantic_json_schema__ - expect description to be included
66
68
json_schema = UploadFile .__get_pydantic_json_schema__ (Mock (), Mock ())
67
69
expected_schema = {
68
- "type" : "string" ,
69
- "format" : "binary" ,
70
- "description" : "A file uploaded as part of a multipart/form-data request"
70
+ "type" : "string" ,
71
+ "format" : "binary" ,
72
+ "description" : "A file uploaded as part of a multipart/form-data request" ,
71
73
}
72
74
assert json_schema == expected_schema
73
-
75
+
74
76
# Test __modify_schema__
75
77
field_schema = {"type" : "object" }
76
78
UploadFile .__modify_schema__ (field_schema )
@@ -84,16 +86,16 @@ def test_validation_middleware_functions(self):
84
86
mock_field .alias = "test_field"
85
87
assert _get_field_value ({"test_field" : "value" }, mock_field ) == "value"
86
88
assert _get_field_value (None , mock_field ) is None
87
-
89
+
88
90
# Test field without alias (AttributeError path)
89
91
mock_field_no_alias = Mock (spec = []) # No alias attribute
90
92
assert _get_field_value ({"test" : "value" }, mock_field_no_alias ) is None
91
-
93
+
92
94
# Test _resolve_field_type with different Union scenarios
93
- assert _resolve_field_type (Union [str , None ]) == str
94
- assert _resolve_field_type (Optional [int ]) == int
95
- assert _resolve_field_type (str ) == str
96
-
95
+ assert _resolve_field_type (Union [str , None ]) is str
96
+ assert _resolve_field_type (Optional [int ]) is int
97
+ assert _resolve_field_type (str ) is str
98
+
97
99
# Test _convert_value_type for UploadFile conversion
98
100
upload_file = _convert_value_type (b"content" , UploadFile )
99
101
assert isinstance (upload_file , UploadFile )
@@ -105,7 +107,7 @@ def test_schema_fix_edge_cases(self):
105
107
empty_schema = {}
106
108
fix_upload_file_schema_references (empty_schema )
107
109
assert empty_schema == {}
108
-
110
+
109
111
# Test with schema missing components
110
112
schema_no_components = {"paths" : {}}
111
113
fix_upload_file_schema_references (schema_no_components )
@@ -116,15 +118,12 @@ def test_upload_file_multipart_handling(self):
116
118
app = APIGatewayRestResolver ()
117
119
118
120
@app .post ("/upload-multi" )
119
- def upload_multiple (
120
- primary : Annotated [UploadFile , File ()],
121
- secondary : Annotated [bytes , File ()]
122
- ):
121
+ def upload_multiple (primary : Annotated [UploadFile , File ()], secondary : Annotated [bytes , File ()]):
123
122
return {"files" : 2 }
124
123
125
124
schema = app .get_openapi_schema ()
126
125
schema_dict = schema .model_dump ()
127
126
fix_upload_file_schema_references (schema_dict )
128
-
127
+
129
128
# Verify multipart handling works without errors
130
129
assert schema_dict is not None
0 commit comments