11"""Comprehensive tests for UploadFile OpenAPI schema generation and validation coverage."""
22
3- import pytest
4- from typing_extensions import Annotated
3+ from typing import Optional , Union
54from unittest .mock import Mock
65
6+ from typing_extensions import Annotated
7+
78from aws_lambda_powertools .event_handler import APIGatewayRestResolver
8- from aws_lambda_powertools .event_handler .openapi .params import File , UploadFile , fix_upload_file_schema_references
99from 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 ,
1113)
12- from typing import Union , Optional
14+ from aws_lambda_powertools . event_handler . openapi . params import File , UploadFile , fix_upload_file_schema_references
1315
1416
1517class TestUploadFileComprehensiveCoverage :
@@ -28,7 +30,7 @@ def upload_file(file: Annotated[UploadFile, File()]):
2830 upload_path = schema .paths ["/upload" ]
2931 request_body = upload_path .post .requestBody
3032 multipart_content = request_body .content ["multipart/form-data" ]
31-
33+
3234 assert multipart_content .schema_ is not None
3335
3436 def test_upload_file_schema_fix_resolves_references (self ):
@@ -43,18 +45,18 @@ def upload_file(file: Annotated[UploadFile, File()]):
4345 # Convert to dict for processing by fix function
4446 schema_dict = schema .model_dump ()
4547 fix_upload_file_schema_references (schema_dict )
46-
48+
4749 # Verify components exist and are processed
4850 assert "components" in schema_dict
4951
5052 def test_upload_file_validation_methods (self ):
5153 """Test UploadFile validation methods for coverage."""
5254 upload_file = UploadFile (file = b"test content" , filename = "test.txt" )
53-
55+
5456 # Test __get_validators__ method
5557 validators = upload_file .__get_validators__ ()
5658 assert callable (next (validators ))
57-
59+
5860 # Test _validate_with_info method - this covers lines in validation
5961 validation_info = Mock ()
6062 validated = upload_file ._validate_with_info (b"content" , validation_info )
@@ -65,12 +67,12 @@ def test_upload_file_pydantic_schema_methods(self):
6567 # Test __get_pydantic_json_schema__ - expect description to be included
6668 json_schema = UploadFile .__get_pydantic_json_schema__ (Mock (), Mock ())
6769 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" ,
7173 }
7274 assert json_schema == expected_schema
73-
75+
7476 # Test __modify_schema__
7577 field_schema = {"type" : "object" }
7678 UploadFile .__modify_schema__ (field_schema )
@@ -84,16 +86,16 @@ def test_validation_middleware_functions(self):
8486 mock_field .alias = "test_field"
8587 assert _get_field_value ({"test_field" : "value" }, mock_field ) == "value"
8688 assert _get_field_value (None , mock_field ) is None
87-
89+
8890 # Test field without alias (AttributeError path)
8991 mock_field_no_alias = Mock (spec = []) # No alias attribute
9092 assert _get_field_value ({"test" : "value" }, mock_field_no_alias ) is None
91-
93+
9294 # 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+
9799 # Test _convert_value_type for UploadFile conversion
98100 upload_file = _convert_value_type (b"content" , UploadFile )
99101 assert isinstance (upload_file , UploadFile )
@@ -105,7 +107,7 @@ def test_schema_fix_edge_cases(self):
105107 empty_schema = {}
106108 fix_upload_file_schema_references (empty_schema )
107109 assert empty_schema == {}
108-
110+
109111 # Test with schema missing components
110112 schema_no_components = {"paths" : {}}
111113 fix_upload_file_schema_references (schema_no_components )
@@ -116,15 +118,12 @@ def test_upload_file_multipart_handling(self):
116118 app = APIGatewayRestResolver ()
117119
118120 @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 ()]):
123122 return {"files" : 2 }
124123
125124 schema = app .get_openapi_schema ()
126125 schema_dict = schema .model_dump ()
127126 fix_upload_file_schema_references (schema_dict )
128-
127+
129128 # Verify multipart handling works without errors
130129 assert schema_dict is not None
0 commit comments