Skip to content

Commit 53744e0

Browse files
authored
refactor: output schema (#29)
* refactor: added output schema * add tests
1 parent 38c6044 commit 53744e0

File tree

10 files changed

+111
-71
lines changed

10 files changed

+111
-71
lines changed

Pipfile.lock

Lines changed: 56 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cdk/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
install_requires=[
2626
'aws-cdk-lib>=2.0.0',
2727
'constructs>=10.0.0',
28-
'aws-cdk.aws-lambda-python-alpha==2.19.0-alpha.0',
28+
'aws-cdk.aws-lambda-python-alpha==2.24.1-alpha.0',
2929
],
3030
)

dev_requirements.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
-e ./cdk
33
attrs==21.4.0
44
aws-cdk-lib==2.24.1
5-
aws-cdk.aws-lambda-python-alpha==2.19.0a0
6-
boto3==1.22.13
7-
botocore==1.25.13
5+
aws-cdk.aws-lambda-python-alpha==2.24.1a0
6+
boto3==1.23.3
7+
botocore==1.26.3
88
cattrs==1.10.0
9-
certifi==2021.10.8
9+
certifi==2022.5.18
1010
cfgv==3.3.1
1111
charset-normalizer==2.0.12 ; python_version >= '3'
1212
click==8.1.3
1313
colorama==0.4.4 ; python_version > '3.4'
14-
constructs==10.1.7
14+
constructs==10.1.11
1515
coverage[toml]==6.3.3
1616
distlib==0.3.4
1717
filelock==3.7.0
@@ -27,15 +27,15 @@ iniconfig==1.1.1
2727
isort==5.10.1
2828
jinja2==3.1.2
2929
jmespath==1.0.0
30-
jsii==1.58.0
30+
jsii==1.59.0
3131
mando==0.6.4
3232
markdown==3.3.7
3333
markupsafe==2.1.1
3434
mccabe==0.6.1
3535
mergedeep==1.3.4
3636
mkdocs-git-revision-date-plugin==0.3.2
3737
mkdocs-material-extensions==1.0.3
38-
mkdocs-material==8.2.14
38+
mkdocs-material==8.2.15
3939
mkdocs==1.3.0
4040
nodeenv==1.6.0
4141
packaging==21.3
@@ -68,7 +68,7 @@ tomli==2.0.1
6868
typing-extensions==4.2.0
6969
urllib3==1.26.9
7070
virtualenv==20.14.1
71-
watchdog==2.1.7
71+
watchdog==2.1.8
7272
xenon==0.9.0
7373
yapf==0.32.0
7474
zipp==3.8.0

docs/pipeline.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ All steps can be run locally using the makefile. See details below:
1313
- Install dev dependencies
1414
- Run pre-commit checks as defined in `.pre-commit-config.yaml`
1515
- Lint with flake8 as defined in `.flake8` - run `make lint` in the IDE
16-
- Verfiy that Python imports are sorted according to standard - run `make sort` in the IDE
16+
- Verify that Python imports are sorted according to standard - run `make sort` in the IDE
1717
- Python formatter Yapf as defined in `.style` - run `make yapf` in the IDE
1818
- Python complexity checks: radon and xenon - run `make complex` in the IDE
1919
- Unit tests. Run `make unit` to run unit tests in the IDE

lambda_requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
-i https://pypi.org/simple
22
aws-lambda-powertools==1.25.10
33
aws-xray-sdk==2.9.0
4-
boto3==1.22.13
5-
botocore==1.25.13
4+
boto3==1.23.3
5+
botocore==1.26.3
66
dnspython==2.2.1
77
email-validator==1.2.1
88
fastjsonschema==2.15.3

service/handlers/my_handler.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
from service.handlers.schemas.dynamic_configuration import FeatureFlagsNames, MyConfiguration
1111
from service.handlers.schemas.env_vars import MyHandlerEnvVars
1212
from service.handlers.schemas.input import Input
13+
from service.handlers.schemas.output import Output
1314
from service.handlers.utils.dynamic_configuration import get_dynamic_configuration_store, parse_configuration
1415
from service.handlers.utils.env_vars_parser import get_environment_variables, init_environment_variables
1516
from service.handlers.utils.http_responses import build_response
1617
from service.handlers.utils.observability import logger, metrics, tracer
1718

1819

1920
@tracer.capture_method(capture_response=False)
20-
def inner_function_example(my_name: str, order_item_count: int) -> Dict[str, Any]:
21+
def inner_function_example(my_name: str, order_item_count: int) -> Output:
2122
# process input, etc. return output
2223
config_store = get_dynamic_configuration_store()
2324
campaign: bool = config_store.evaluate(
@@ -32,7 +33,7 @@ def inner_function_example(my_name: str, order_item_count: int) -> Dict[str, Any
3233
default=False,
3334
)
3435
logger.debug('premium feature flag value', extra={'premium': premium})
35-
return {'message': 'success'}
36+
return Output(success=True, order_item_count=order_item_count)
3637

3738

3839
@init_environment_variables(model=MyHandlerEnvVars)
@@ -60,7 +61,7 @@ def my_handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]:
6061
logger.error('event failed input validation', extra={'error': str(exc)})
6162
return build_response(http_status=HTTPStatus.BAD_REQUEST, body={})
6263

63-
response: Dict[str, str] = inner_function_example(input.my_name, input.order_item_count)
64+
response: Output = inner_function_example(input.my_name, input.order_item_count)
6465
logger.info('inner_function_example finished successfully')
6566
metrics.add_metric(name='ValidEvents', unit=MetricUnit.Count, value=1)
66-
return build_response(http_status=HTTPStatus.OK, body=response)
67+
return build_response(http_status=HTTPStatus.OK, body=response.dict())

service/handlers/schemas/output.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pydantic import BaseModel, PositiveInt
2+
3+
4+
class Output(BaseModel):
5+
success: bool
6+
order_item_count: PositiveInt

tests/e2e/test_my_handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ def test_handler_200_ok():
1414
response = requests.post(f'{api_gw_url}/api/{GW_RESOURCE}', data=body.json())
1515
assert response.status_code == HTTPStatus.OK
1616
body_dict = json.loads(response.text)
17-
assert body_dict['message'] == 'success'
17+
assert body_dict['success']
18+
assert body_dict['order_item_count'] == 5
1819

1920

2021
def test_handler_bad_request():

tests/unit/test_my_handler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def test_handler_200_ok(mocker):
7171
response = my_handler(generate_api_gw_event(body.dict()), generate_context())
7272
assert response['statusCode'] == HTTPStatus.OK
7373
body_dict = json.loads(response['body'])
74-
assert body_dict['message'] == 'success'
74+
assert body_dict['success']
75+
assert body_dict['order_item_count'] == 5
7576

7677

7778
def test_handler_bad_request(mocker):

tests/unit/test_my_handler_output.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
from aws_lambda_powertools.utilities.parser import ValidationError
3+
4+
from service.handlers.schemas.output import Output
5+
6+
7+
def test_invalid_success():
8+
with pytest.raises(ValidationError):
9+
Output(success=4, order_item_count=4)
10+
11+
12+
def test_invalid_items():
13+
with pytest.raises(ValidationError):
14+
Output(success=4, order_item_count=1.1)
15+
16+
17+
def test_invalid_items_negative():
18+
with pytest.raises(ValidationError):
19+
Output(success=4, order_item_count='-1')
20+
21+
22+
def test_invalid_items_zero():
23+
with pytest.raises(ValidationError):
24+
Output(success=4, order_item_count=0)
25+
26+
27+
def test_valid_output():
28+
Output(success=True, order_item_count=4)

0 commit comments

Comments
 (0)