-
Notifications
You must be signed in to change notification settings - Fork 429
chore(ci): add the Event Handler feature to nox tests #4581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
heitorlessa
merged 3 commits into
aws-powertools:develop
from
leandrodamascena:feat/ci-nox-event-handler
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
File renamed without changes.
80 changes: 80 additions & 0 deletions
80
tests/functional/event_handler/_pydantic/test_api_gateway.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from pydantic import BaseModel | ||
|
||
from aws_lambda_powertools.event_handler import content_types | ||
from aws_lambda_powertools.event_handler.api_gateway import ( | ||
ApiGatewayResolver, | ||
Response, | ||
) | ||
from aws_lambda_powertools.event_handler.openapi.exceptions import RequestValidationError | ||
from tests.functional.utils import load_event | ||
|
||
LOAD_GW_EVENT = load_event("apiGatewayProxyEvent.json") | ||
|
||
|
||
def test_exception_handler_with_data_validation(): | ||
# GIVEN a resolver with an exception handler defined for RequestValidationError | ||
app = ApiGatewayResolver(enable_validation=True) | ||
|
||
@app.exception_handler(RequestValidationError) | ||
def handle_validation_error(ex: RequestValidationError): | ||
return Response( | ||
status_code=422, | ||
content_type=content_types.TEXT_PLAIN, | ||
body=f"Invalid data. Number of errors: {len(ex.errors())}", | ||
) | ||
|
||
@app.get("/my/path") | ||
def get_lambda(param: int): ... | ||
|
||
# WHEN calling the event handler | ||
# AND a RequestValidationError is raised | ||
result = app(LOAD_GW_EVENT, {}) | ||
|
||
# THEN call the exception_handler | ||
assert result["statusCode"] == 422 | ||
assert result["multiValueHeaders"]["Content-Type"] == [content_types.TEXT_PLAIN] | ||
assert result["body"] == "Invalid data. Number of errors: 1" | ||
|
||
|
||
def test_exception_handler_with_data_validation_pydantic_response(): | ||
# GIVEN a resolver with an exception handler defined for RequestValidationError | ||
app = ApiGatewayResolver(enable_validation=True) | ||
|
||
class Err(BaseModel): | ||
msg: str | ||
|
||
@app.exception_handler(RequestValidationError) | ||
def handle_validation_error(ex: RequestValidationError): | ||
return Response( | ||
status_code=422, | ||
content_type=content_types.APPLICATION_JSON, | ||
body=Err(msg=f"Invalid data. Number of errors: {len(ex.errors())}"), | ||
) | ||
|
||
@app.get("/my/path") | ||
def get_lambda(param: int): ... | ||
|
||
# WHEN calling the event handler | ||
# AND a RequestValidationError is raised | ||
result = app(LOAD_GW_EVENT, {}) | ||
|
||
# THEN exception handler's pydantic response should be serialized correctly | ||
assert result["statusCode"] == 422 | ||
assert result["body"] == '{"msg":"Invalid data. Number of errors: 1"}' | ||
|
||
|
||
def test_data_validation_error(): | ||
# GIVEN a resolver without an exception handler | ||
app = ApiGatewayResolver(enable_validation=True) | ||
|
||
@app.get("/my/path") | ||
def get_lambda(param: int): ... | ||
|
||
# WHEN calling the event handler | ||
# AND a RequestValidationError is raised | ||
result = app(LOAD_GW_EVENT, {}) | ||
|
||
# THEN call the exception_handler | ||
assert result["statusCode"] == 422 | ||
assert result["multiValueHeaders"]["Content-Type"] == [content_types.APPLICATION_JSON] | ||
assert "missing" in result["body"] |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
73 changes: 73 additions & 0 deletions
73
tests/functional/event_handler/required_dependencies/conftest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import json | ||
|
||
import pytest | ||
|
||
from tests.functional.utils import load_event | ||
|
||
|
||
@pytest.fixture | ||
def json_dump(): | ||
# our serializers reduce length to save on costs; fixture to replicate separators | ||
return lambda obj: json.dumps(obj, separators=(",", ":")) | ||
|
||
|
||
@pytest.fixture | ||
def validation_schema(): | ||
return { | ||
"$schema": "https://json-schema.org/draft-07/schema", | ||
"$id": "https://example.com/example.json", | ||
"type": "object", | ||
"title": "Sample schema", | ||
"description": "The root schema comprises the entire JSON document.", | ||
"examples": [{"message": "hello world", "username": "lessa"}], | ||
"required": ["message", "username"], | ||
"properties": { | ||
"message": { | ||
"$id": "#/properties/message", | ||
"type": "string", | ||
"title": "The message", | ||
"examples": ["hello world"], | ||
}, | ||
"username": { | ||
"$id": "#/properties/username", | ||
"type": "string", | ||
"title": "The username", | ||
"examples": ["lessa"], | ||
}, | ||
}, | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def raw_event(): | ||
return {"message": "hello hello", "username": "blah blah"} | ||
|
||
|
||
@pytest.fixture | ||
def gw_event(): | ||
return load_event("apiGatewayProxyEvent.json") | ||
|
||
|
||
@pytest.fixture | ||
def gw_event_http(): | ||
return load_event("apiGatewayProxyV2Event.json") | ||
|
||
|
||
@pytest.fixture | ||
def gw_event_alb(): | ||
return load_event("albMultiValueQueryStringEvent.json") | ||
|
||
|
||
@pytest.fixture | ||
def gw_event_lambda_url(): | ||
return load_event("lambdaFunctionUrlEventWithHeaders.json") | ||
|
||
|
||
@pytest.fixture | ||
def gw_event_vpc_lattice(): | ||
return load_event("vpcLatticeV2EventWithHeaders.json") | ||
|
||
|
||
@pytest.fixture | ||
def gw_event_vpc_lattice_v1(): | ||
return load_event("vpcLatticeEvent.json") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.