Skip to content

Commit 56497f9

Browse files
Addressing Simon's feedback
1 parent 83ecc37 commit 56497f9

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

tests/unit/event_handler/__init__.py

Whitespace-only changes.

tests/unit/event_handler/_pydantic/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
from pydantic import __version__
3+
4+
5+
@pytest.fixture(scope="session")
6+
def pydanticv1_only():
7+
8+
version = __version__.split(".")
9+
if version[0] != "1":
10+
pytest.skip("pydanticv1 test only")
11+
12+
13+
@pytest.fixture(scope="session")
14+
def pydanticv2_only():
15+
16+
version = __version__.split(".")
17+
if version[0] != "2":
18+
pytest.skip("pydanticv2 test only")
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
from aws_lambda_powertools.event_handler.openapi.models import OpenAPIExtensions
4+
5+
6+
@pytest.mark.usefixtures("pydanticv1_only")
7+
def test_openapi_extensions_with_dict():
8+
# GIVEN we create an OpenAPIExtensions object with a dict
9+
extensions = OpenAPIExtensions(openapi_extensions={"x-amazon-apigateway": {"foo": "bar"}})
10+
11+
# THEN we get a dict with the extension
12+
assert extensions.dict(exclude_none=True) == {"x-amazon-apigateway": {"foo": "bar"}}
13+
14+
15+
@pytest.mark.usefixtures("pydanticv1_only")
16+
def test_openapi_extensions_with_proxy_models():
17+
18+
# GIVEN we create an models using OpenAPIExtensions as a "Proxy" Model
19+
class MyModelFoo(OpenAPIExtensions):
20+
foo: str
21+
22+
class MyModelBar(OpenAPIExtensions):
23+
bar: str
24+
foo: MyModelFoo
25+
26+
value_to_serialize = MyModelBar(
27+
bar="bar",
28+
foo=MyModelFoo(foo="foo"),
29+
openapi_extensions={"x-amazon-apigateway": {"foo": "bar"}},
30+
)
31+
32+
value_to_return = value_to_serialize.dict(exclude_none=True)
33+
34+
# THEN we get a dict with the value serialized
35+
assert value_to_return == {"bar": "bar", "foo": {"foo": "foo"}, "x-amazon-apigateway": {"foo": "bar"}}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
from aws_lambda_powertools.event_handler.openapi.models import OpenAPIExtensions
4+
5+
6+
@pytest.mark.usefixtures("pydanticv2_only")
7+
def test_openapi_extensions_with_dict():
8+
# GIVEN we create an OpenAPIExtensions object with a dict
9+
extensions = OpenAPIExtensions(openapi_extensions={"x-amazon-apigateway": {"foo": "bar"}})
10+
11+
# THEN we get a dict with the extension
12+
assert extensions.model_dump(exclude_none=True) == {"x-amazon-apigateway": {"foo": "bar"}}
13+
14+
15+
@pytest.mark.usefixtures("pydanticv2_only")
16+
def test_openapi_extensions_with_proxy_models():
17+
18+
# GIVEN we create an models using OpenAPIExtensions as a "Proxy" Model
19+
class MyModelFoo(OpenAPIExtensions):
20+
foo: str
21+
22+
class MyModelBar(OpenAPIExtensions):
23+
bar: str
24+
foo: MyModelFoo
25+
26+
value_to_serialize = MyModelBar(
27+
bar="bar",
28+
foo=MyModelFoo(foo="foo"),
29+
openapi_extensions={"x-amazon-apigateway": {"foo": "bar"}},
30+
)
31+
32+
value_to_return = value_to_serialize.model_dump(exclude_none=True)
33+
34+
# THEN we get a dict with the value serialized
35+
assert value_to_return == {"bar": "bar", "foo": {"foo": "foo"}, "x-amazon-apigateway": {"foo": "bar"}}

0 commit comments

Comments
 (0)