Skip to content

Commit b818ae7

Browse files
chore(ci): add the Event Handler feature to nox tests (#4581)
1 parent 6d31ed4 commit b818ae7

31 files changed

+182
-82
lines changed

noxfile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ def test_with_only_required_packages(session: nox.Session):
5454
# Metrics - Base provider
5555
# Middleware factory without tracer
5656
# Typing
57+
# Event Handler without OpenAPI
5758
build_and_run_test(
5859
session,
5960
folders=[
6061
f"{PREFIX_TESTS_FUNCTIONAL}/logger/required_dependencies/",
6162
f"{PREFIX_TESTS_FUNCTIONAL}/metrics/required_dependencies/",
6263
f"{PREFIX_TESTS_FUNCTIONAL}/middleware_factory/required_dependencies/",
6364
f"{PREFIX_TESTS_FUNCTIONAL}/typing/required_dependencies/",
65+
f"{PREFIX_TESTS_FUNCTIONAL}/event_handler/required_dependencies/",
6466
],
6567
)
6668

@@ -134,3 +136,19 @@ def test_with_aws_encryption_sdk_as_required_package(session: nox.Session):
134136
],
135137
extras="datamasking",
136138
)
139+
140+
141+
@nox.session()
142+
@nox.parametrize("pydantic", ["1.10", "2.0"])
143+
def test_with_pydantic_required_package(session: nox.Session, pydantic: str):
144+
"""Tests that only depends for required libraries"""
145+
# Event Handler OpenAPI
146+
147+
session.install(f"pydantic>={pydantic}")
148+
149+
build_and_run_test(
150+
session,
151+
folders=[
152+
f"{PREFIX_TESTS_FUNCTIONAL}/event_handler/_pydantic/",
153+
],
154+
)

tests/__init__.py

Whitespace-only changes.

tests/functional/event_handler/_pydantic/__init__.py

Whitespace-only changes.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
from pydantic import BaseModel
2+
3+
from aws_lambda_powertools.event_handler import content_types
4+
from aws_lambda_powertools.event_handler.api_gateway import (
5+
ApiGatewayResolver,
6+
Response,
7+
)
8+
from aws_lambda_powertools.event_handler.openapi.exceptions import RequestValidationError
9+
from tests.functional.utils import load_event
10+
11+
LOAD_GW_EVENT = load_event("apiGatewayProxyEvent.json")
12+
13+
14+
def test_exception_handler_with_data_validation():
15+
# GIVEN a resolver with an exception handler defined for RequestValidationError
16+
app = ApiGatewayResolver(enable_validation=True)
17+
18+
@app.exception_handler(RequestValidationError)
19+
def handle_validation_error(ex: RequestValidationError):
20+
return Response(
21+
status_code=422,
22+
content_type=content_types.TEXT_PLAIN,
23+
body=f"Invalid data. Number of errors: {len(ex.errors())}",
24+
)
25+
26+
@app.get("/my/path")
27+
def get_lambda(param: int): ...
28+
29+
# WHEN calling the event handler
30+
# AND a RequestValidationError is raised
31+
result = app(LOAD_GW_EVENT, {})
32+
33+
# THEN call the exception_handler
34+
assert result["statusCode"] == 422
35+
assert result["multiValueHeaders"]["Content-Type"] == [content_types.TEXT_PLAIN]
36+
assert result["body"] == "Invalid data. Number of errors: 1"
37+
38+
39+
def test_exception_handler_with_data_validation_pydantic_response():
40+
# GIVEN a resolver with an exception handler defined for RequestValidationError
41+
app = ApiGatewayResolver(enable_validation=True)
42+
43+
class Err(BaseModel):
44+
msg: str
45+
46+
@app.exception_handler(RequestValidationError)
47+
def handle_validation_error(ex: RequestValidationError):
48+
return Response(
49+
status_code=422,
50+
content_type=content_types.APPLICATION_JSON,
51+
body=Err(msg=f"Invalid data. Number of errors: {len(ex.errors())}"),
52+
)
53+
54+
@app.get("/my/path")
55+
def get_lambda(param: int): ...
56+
57+
# WHEN calling the event handler
58+
# AND a RequestValidationError is raised
59+
result = app(LOAD_GW_EVENT, {})
60+
61+
# THEN exception handler's pydantic response should be serialized correctly
62+
assert result["statusCode"] == 422
63+
assert result["body"] == '{"msg":"Invalid data. Number of errors: 1"}'
64+
65+
66+
def test_data_validation_error():
67+
# GIVEN a resolver without an exception handler
68+
app = ApiGatewayResolver(enable_validation=True)
69+
70+
@app.get("/my/path")
71+
def get_lambda(param: int): ...
72+
73+
# WHEN calling the event handler
74+
# AND a RequestValidationError is raised
75+
result = app(LOAD_GW_EVENT, {})
76+
77+
# THEN call the exception_handler
78+
assert result["statusCode"] == 422
79+
assert result["multiValueHeaders"]["Content-Type"] == [content_types.APPLICATION_JSON]
80+
assert "missing" in result["body"]

0 commit comments

Comments
 (0)