Skip to content

Commit c766c10

Browse files
committed
improv: test validator decorator
1 parent cce2ab4 commit c766c10

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

aws_lambda_powertools/utilities/validation/validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def validator(
2828

2929
if outbound_schema:
3030
logger.debug("Validating outbound event")
31-
validate_data_against_schema(data=event, schema=outbound_schema)
31+
validate_data_against_schema(data=response, schema=outbound_schema)
3232

3333
return response
3434

tests/functional/validator/conftest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ def schema_array():
6868
}
6969

7070

71+
@pytest.fixture
72+
def schema_response():
73+
return {
74+
"$schema": "http://json-schema.org/draft-07/schema",
75+
"$id": "http://example.com/example.json",
76+
"type": "object",
77+
"title": "Sample outgoing schema",
78+
"description": "The root schema comprises the entire JSON document.",
79+
"examples": [{"statusCode": 200, "body": "response"}],
80+
"required": ["statusCode", "body"],
81+
"properties": {
82+
"statusCode": {"$id": "#/properties/statusCode", "type": "integer", "title": "The statusCode"},
83+
"body": {"$id": "#/properties/body", "type": "string", "title": "The response"},
84+
},
85+
}
86+
87+
7188
@pytest.fixture
7289
def raw_event():
7390
return {"message": "hello hello", "username": "blah blah"}
@@ -88,6 +105,11 @@ def wrapped_event_base64_json_string():
88105
return {"data": "eyJtZXNzYWdlIjogImhlbGxvIGhlbGxvIiwgInVzZXJuYW1lIjogImJsYWggYmxhaCJ9="}
89106

90107

108+
@pytest.fixture
109+
def raw_response():
110+
return {"statusCode": 200, "body": "response"}
111+
112+
91113
@pytest.fixture
92114
def apigateway_event():
93115
return {

tests/functional/validator/test_validator.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from aws_lambda_powertools.utilities.validation import envelopes, exceptions, validate
3+
from aws_lambda_powertools.utilities.validation import envelopes, exceptions, validate, validator
44

55

66
def test_validate_raw_event(schema, raw_event):
@@ -72,13 +72,34 @@ def test_cloudwatch_logs_envelope(cloudwatch_logs_schema, cloudwatch_logs_event)
7272
validate(event=cloudwatch_logs_event, schema=cloudwatch_logs_schema, envelope=envelopes.CLOUDWATCH_LOGS)
7373

7474

75-
def test_validator_incoming():
76-
raise NotImplementedError()
75+
def test_validator_incoming(schema, raw_event):
76+
@validator(inbound_schema=schema)
77+
def lambda_handler(evt, context):
78+
pass
7779

80+
lambda_handler(raw_event, {})
7881

79-
def test_validator_outgoing():
80-
raise NotImplementedError()
8182

83+
def test_validator_outgoing(schema_response, raw_response):
84+
@validator(outbound_schema=schema_response)
85+
def lambda_handler(evt, context):
86+
return raw_response
8287

83-
def test_validator_incoming_and_outgoing():
84-
raise NotImplementedError()
88+
lambda_handler({}, {})
89+
90+
91+
def test_validator_incoming_and_outgoing(schema, schema_response, raw_event, raw_response):
92+
@validator(inbound_schema=schema, outbound_schema=schema_response)
93+
def lambda_handler(evt, context):
94+
return raw_response
95+
96+
lambda_handler(raw_event, {})
97+
98+
99+
def test_validator_propagates_exception(schema, raw_event, schema_response):
100+
@validator(inbound_schema=schema, outbound_schema=schema_response)
101+
def lambda_handler(evt, context):
102+
raise ValueError("Bubble up")
103+
104+
with pytest.raises(ValueError):
105+
lambda_handler(raw_event, {})

0 commit comments

Comments
 (0)