Skip to content

Commit f65b743

Browse files
Add e2e tests
1 parent 30e3571 commit f65b743

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import json
2+
from typing import Any, Dict, Type, Union
3+
4+
from pydantic import BaseModel
5+
6+
from aws_lambda_powertools.utilities.parser import parse
7+
from aws_lambda_powertools.utilities.typing import LambdaContext
8+
9+
AnyInheritedModel = Union[Type[BaseModel], BaseModel]
10+
RawDictOrModel = Union[Dict[str, Any], AnyInheritedModel]
11+
12+
13+
class ModelWithUnionType(BaseModel):
14+
name: str
15+
profile: RawDictOrModel
16+
17+
18+
def lambda_handler(event: ModelWithUnionType, context: LambdaContext):
19+
event = json.dumps(event)
20+
21+
event_parsed = parse(event=event, model=ModelWithUnionType)
22+
23+
return {"name": event_parsed.name}

tests/e2e/parser/test_parser.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def handler_with_dataclass_arn(infrastructure: dict) -> str:
2020
return infrastructure.get("HandlerWithDataclass", "")
2121

2222

23+
@pytest.fixture
24+
def handler_with_type_model_class(infrastructure: dict) -> str:
25+
return infrastructure.get("HandlerWithModelTypeClass", "")
26+
27+
2328
@pytest.mark.xdist_group(name="parser")
2429
def test_parser_with_basic_model(handler_with_basic_model_arn):
2530
# GIVEN
@@ -66,3 +71,19 @@ def test_parser_with_dataclass(handler_with_dataclass_arn):
6671
ret = parser_execution["Payload"].read().decode("utf-8")
6772

6873
assert "powertools" in ret
74+
75+
76+
@pytest.mark.xdist_group(name="parser")
77+
def test_parser_with_type_model(handler_with_type_model_class):
78+
# GIVEN
79+
payload = json.dumps({"name": "powertools", "profile": {"description": "python", "size": "XXL"}})
80+
81+
# WHEN
82+
parser_execution, _ = data_fetcher.get_lambda_response(
83+
lambda_arn=handler_with_type_model_class,
84+
payload=payload,
85+
)
86+
87+
ret = parser_execution["Payload"].read().decode("utf-8")
88+
89+
assert "powertools" in ret

0 commit comments

Comments
 (0)