Skip to content

Commit a2ff1f1

Browse files
authored
fix: fix broken test (#700)
1 parent 87ae345 commit a2ff1f1

File tree

8 files changed

+73
-59
lines changed

8 files changed

+73
-59
lines changed

cdk/my_service/api_construct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from aws_cdk.aws_lambda_python_alpha import PythonLayerVersion
66
from aws_cdk.aws_logs import RetentionDays
77
from constructs import Construct
8-
from my_service.api_db_construct import ApiDbConstruct # type: ignore
98

109
import cdk.my_service.constants as constants
10+
from cdk.my_service.api_db_construct import ApiDbConstruct
1111

1212

1313
class ApiConstruct(Construct):

cdk/my_service/service_stack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from cdk_nag import AwsSolutionsChecks, NagSuppressions
66
from constructs import Construct
77
from git import Repo
8-
from my_service.api_construct import ApiConstruct # type: ignore
98

9+
from cdk.my_service.api_construct import ApiConstruct
1010
from cdk.my_service.configuration.configuration_construct import ConfigurationStore
1111
from cdk.my_service.constants import CONFIGURATION_NAME, ENVIRONMENT, SERVICE_NAME
1212

poetry.lock

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

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ aws-lambda-env-modeler = "*"
2828
[tool.poetry.dev-dependencies]
2929
# CDK
3030
service-cdk = {path = "cdk", develop = true}
31-
aws-cdk-lib = ">=2.75.0"
31+
aws-cdk-lib = ">=2.88.0"
3232
constructs = ">=10.0.0"
3333
cdk-nag = ">2.0.0"
34-
"aws-cdk.aws-lambda-python-alpha" = "^2.75.0-alpha.0"
34+
"aws-cdk.aws-lambda-python-alpha" = "^2.88.0-alpha.0"
3535
# DEV
3636
pytest = "*"
3737
pytest-mock = "*"

service/dal/db_handler.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
from abc import ABC, abstractmethod
1+
from abc import ABC, ABCMeta, abstractmethod
22

33
from service.dal.schemas.db import OrderEntry
44

55

6-
class DalHandler(ABC):
6+
class _SingletonMeta(ABCMeta):
7+
_instances: dict = {}
8+
9+
def __call__(cls, *args, **kwargs):
10+
if cls not in cls._instances:
11+
cls._instances[cls] = super(_SingletonMeta, cls).__call__(*args, **kwargs)
12+
return cls._instances[cls]
13+
14+
15+
class DalHandler(ABC, metaclass=_SingletonMeta):
716

817
@abstractmethod
918
def create_order_in_db(self, customer_name: str, order_item_count: int) -> OrderEntry:

service/handlers/create_order.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def create_order(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any
4242
# we want to extract and parse the HTTP body from the api gw envelope
4343
create_input: CreateOrderRequest = parse(event=event, model=CreateOrderRequest, envelope=ApiGatewayEnvelope)
4444
logger.info('got create order request', extra={'order_item_count': create_input.order_item_count})
45-
except (ValidationError, TypeError) as exc:
45+
except (ValidationError, TypeError) as exc: # pragma: no cover
4646
logger.error('event failed input validation', extra={'error': str(exc)})
4747
return build_response(http_status=HTTPStatus.BAD_REQUEST, body={})
4848

tests/integration/test_create_order.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def test_handler_200_ok(mocker, table_name: str):
7272
assert response['Item']['order_item_count'] == order_item_count
7373

7474

75-
def test_internal_server_error():
75+
def test_internal_server_error(mocker):
76+
mock_dynamic_configuration(mocker, MOCKED_SCHEMA)
7677
db_handler: DynamoDalHandler = DynamoDalHandler('table')
7778
table = db_handler._get_db_handler()
7879
stubber = Stubber(table.meta.client)
@@ -81,6 +82,8 @@ def test_internal_server_error():
8182
body = CreateOrderRequest(customer_name='RanTheBuilder', order_item_count=5)
8283
response = call_create_order(generate_api_gw_event(body.model_dump()))
8384
assert response['statusCode'] == HTTPStatus.INTERNAL_SERVER_ERROR
85+
stubber.deactivate()
86+
DynamoDalHandler._instances = {}
8487

8588

8689
def test_handler_bad_request(mocker):

tests/unit/test_db_handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ def test_raise_exception():
1313
stubber.activate()
1414
with pytest.raises(InternalServerException):
1515
db_handler.create_order_in_db(customer_name='customer', order_item_count=5)
16+
stubber.deactivate()
17+
DynamoDalHandler._instances = {}

0 commit comments

Comments
 (0)