From 7b10885a7494e550f16c5812bbe0dce69b730fc9 Mon Sep 17 00:00:00 2001 From: Bas Date: Mon, 3 Mar 2025 11:44:58 +0100 Subject: [PATCH 1/3] Type `LambdaContext` types in tests --- .../src/getting_started_with_test.py | 19 ++++++------ .../data_masking/tests/test_lambda_mask.py | 23 +++++++------- .../src/assert_bedrock_agent_response.py | 19 ++++++------ .../src/assert_async_graphql_response.py | 19 ++++++------ .../src/assert_graphql_response.py | 19 ++++++------ .../src/assert_alb_api_resolver_response.py | 19 ++++++------ ...sert_function_url_api_resolver_response.py | 19 ++++++------ .../src/assert_http_api_resolver_response.py | 19 ++++++------ .../src/assert_rest_api_resolver_response.py | 19 ++++++------ .../test_disabling_idempotency_utility.py | 23 +++++++------- .../tests/test_with_dynamodb_local.py | 23 +++++++------- .../tests/test_with_io_operations.py | 23 +++++++------- .../src/fake_lambda_context_for_logger.py | 19 ++++++------ .../metrics/src/assert_multiple_emf_blobs.py | 19 ++++++------ .../functional/idempotency/_boto3/conftest.py | 21 +++++++------ .../idempotency/_redis/test_redis_layer.py | 21 +++++++------ .../idempotency/test_idempotency_redis.py | 31 ++++++++++--------- 17 files changed, 186 insertions(+), 169 deletions(-) diff --git a/examples/batch_processing/src/getting_started_with_test.py b/examples/batch_processing/src/getting_started_with_test.py index 49e78269248..73df04d4d7b 100644 --- a/examples/batch_processing/src/getting_started_with_test.py +++ b/examples/batch_processing/src/getting_started_with_test.py @@ -11,15 +11,16 @@ def load_event(path: Path): return json.load(f) -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" - aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" + aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() @@ -29,7 +30,7 @@ def sqs_event(): return load_event(path=Path("events/sqs_event.json")) -def test_app_batch_partial_response(sqs_event, lambda_context): +def test_app_batch_partial_response(sqs_event, lambda_context: LambdaContext): # GIVEN processor_result = processor # access processor for additional assertions successful_record = sqs_event["Records"][0] diff --git a/examples/data_masking/tests/test_lambda_mask.py b/examples/data_masking/tests/test_lambda_mask.py index 596f065b380..d7a1799a1e0 100644 --- a/examples/data_masking/tests/test_lambda_mask.py +++ b/examples/data_masking/tests/test_lambda_mask.py @@ -4,22 +4,23 @@ import test_lambda_mask -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:111111111:function:test" - aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:111111111:function:test" + aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" + + def get_remaining_time_in_millis(self) -> int: + return 5 - def get_remaining_time_in_millis(self) -> int: - return 5 +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_encrypt_lambda(lambda_context): +def test_encrypt_lambda(lambda_context: LambdaContext): # GIVEN: A sample event for testing event = {"testkey": "testvalue"} diff --git a/examples/event_handler_bedrock_agents/src/assert_bedrock_agent_response.py b/examples/event_handler_bedrock_agents/src/assert_bedrock_agent_response.py index 07f3273961e..4b172ce2df9 100644 --- a/examples/event_handler_bedrock_agents/src/assert_bedrock_agent_response.py +++ b/examples/event_handler_bedrock_agents/src/assert_bedrock_agent_response.py @@ -4,19 +4,20 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" - aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" + aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_lambda_handler(lambda_context): +def test_lambda_handler(lambda_context: LambdaContext): minimal_event = { "apiPath": "/current_time", "httpMethod": "GET", diff --git a/examples/event_handler_graphql/src/assert_async_graphql_response.py b/examples/event_handler_graphql/src/assert_async_graphql_response.py index bb1b429c43c..5ea148492d3 100644 --- a/examples/event_handler_graphql/src/assert_async_graphql_response.py +++ b/examples/event_handler_graphql/src/assert_async_graphql_response.py @@ -10,20 +10,21 @@ ) -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" - aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" + aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() @pytest.mark.asyncio -async def test_async_direct_resolver(lambda_context): +async def test_async_direct_resolver(lambda_context: LambdaContext): # GIVEN fake_event = json.loads(Path("assert_async_graphql_response.json").read_text()) diff --git a/examples/event_handler_graphql/src/assert_graphql_response.py b/examples/event_handler_graphql/src/assert_graphql_response.py index d78698e109b..93be113f445 100644 --- a/examples/event_handler_graphql/src/assert_graphql_response.py +++ b/examples/event_handler_graphql/src/assert_graphql_response.py @@ -8,19 +8,20 @@ from assert_graphql_response_module import Location, app # instance of AppSyncResolver -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" - aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" + aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_direct_resolver(lambda_context): +def test_direct_resolver(lambda_context: LambdaContext): # GIVEN fake_event = json.loads(Path("assert_graphql_response.json").read_text()) diff --git a/examples/event_handler_rest/src/assert_alb_api_resolver_response.py b/examples/event_handler_rest/src/assert_alb_api_resolver_response.py index f6bd54facee..e0981215a8b 100644 --- a/examples/event_handler_rest/src/assert_alb_api_resolver_response.py +++ b/examples/event_handler_rest/src/assert_alb_api_resolver_response.py @@ -4,19 +4,20 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" - aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" + aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_lambda_handler(lambda_context): +def test_lambda_handler(lambda_context: LambdaContext): minimal_event = { "path": "/todos", "httpMethod": "GET", diff --git a/examples/event_handler_rest/src/assert_function_url_api_resolver_response.py b/examples/event_handler_rest/src/assert_function_url_api_resolver_response.py index 865f26b70a3..2c591f640be 100644 --- a/examples/event_handler_rest/src/assert_function_url_api_resolver_response.py +++ b/examples/event_handler_rest/src/assert_function_url_api_resolver_response.py @@ -4,19 +4,20 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" - aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" + aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_lambda_handler(lambda_context): +def test_lambda_handler(lambda_context: LambdaContext): minimal_event = { "rawPath": "/todos", "requestContext": { diff --git a/examples/event_handler_rest/src/assert_http_api_resolver_response.py b/examples/event_handler_rest/src/assert_http_api_resolver_response.py index af294fbc3bc..36b59a69fd2 100644 --- a/examples/event_handler_rest/src/assert_http_api_resolver_response.py +++ b/examples/event_handler_rest/src/assert_http_api_resolver_response.py @@ -4,19 +4,20 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" - aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" + aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_lambda_handler(lambda_context): +def test_lambda_handler(lambda_context: LambdaContext): minimal_event = { "rawPath": "/todos", "requestContext": { diff --git a/examples/event_handler_rest/src/assert_rest_api_resolver_response.py b/examples/event_handler_rest/src/assert_rest_api_resolver_response.py index 4422022ae5f..00d7b557789 100644 --- a/examples/event_handler_rest/src/assert_rest_api_resolver_response.py +++ b/examples/event_handler_rest/src/assert_rest_api_resolver_response.py @@ -4,19 +4,20 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" - aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test" + aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_lambda_handler(lambda_context): +def test_lambda_handler(lambda_context: LambdaContext): minimal_event = { "path": "/todos", "httpMethod": "GET", diff --git a/examples/idempotency/tests/test_disabling_idempotency_utility.py b/examples/idempotency/tests/test_disabling_idempotency_utility.py index f33174cde3d..3aba8a090c8 100644 --- a/examples/idempotency/tests/test_disabling_idempotency_utility.py +++ b/examples/idempotency/tests/test_disabling_idempotency_utility.py @@ -4,22 +4,23 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" - aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" + aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" + + def get_remaining_time_in_millis(self) -> int: + return 5 - def get_remaining_time_in_millis(self) -> int: - return 5 +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_idempotent_lambda_handler(monkeypatch, lambda_context): +def test_idempotent_lambda_handler(monkeypatch, lambda_context: LambdaContext): # Set POWERTOOLS_IDEMPOTENCY_DISABLED before calling decorated functions monkeypatch.setenv("POWERTOOLS_IDEMPOTENCY_DISABLED", 1) diff --git a/examples/idempotency/tests/test_with_dynamodb_local.py b/examples/idempotency/tests/test_with_dynamodb_local.py index 7a9a8fc0234..10eae1d6851 100644 --- a/examples/idempotency/tests/test_with_dynamodb_local.py +++ b/examples/idempotency/tests/test_with_dynamodb_local.py @@ -5,22 +5,23 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" - aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" + aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" + + def get_remaining_time_in_millis(self) -> int: + return 5 - def get_remaining_time_in_millis(self) -> int: - return 5 +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_idempotent_lambda(lambda_context): +def test_idempotent_lambda(lambda_context: LambdaContext): # Configure the boto3 to use the endpoint for the DynamoDB Local instance dynamodb_local_client = boto3.client("dynamodb", endpoint_url="http://localhost:8000") app_test_dynamodb_local.persistence_layer.client = dynamodb_local_client diff --git a/examples/idempotency/tests/test_with_io_operations.py b/examples/idempotency/tests/test_with_io_operations.py index 9d455906889..536fe31b70b 100644 --- a/examples/idempotency/tests/test_with_io_operations.py +++ b/examples/idempotency/tests/test_with_io_operations.py @@ -5,22 +5,23 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" - aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" + aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" + + def get_remaining_time_in_millis(self) -> int: + return 5 - def get_remaining_time_in_millis(self) -> int: - return 5 +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_idempotent_lambda(lambda_context): +def test_idempotent_lambda(lambda_context: LambdaContext): mock_client = MagicMock() app_test_io_operations.persistence_layer.client = mock_client result = app_test_io_operations.handler({"testkey": "testvalue"}, lambda_context) diff --git a/examples/logger/src/fake_lambda_context_for_logger.py b/examples/logger/src/fake_lambda_context_for_logger.py index d3b3efc98f9..66afd358281 100644 --- a/examples/logger/src/fake_lambda_context_for_logger.py +++ b/examples/logger/src/fake_lambda_context_for_logger.py @@ -4,18 +4,19 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" - aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" + aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() -def test_lambda_handler(lambda_context): +def test_lambda_handler(lambda_context: LambdaContext): test_event = {"test": "event"} fake_lambda_context_for_logger_module.handler(test_event, lambda_context) diff --git a/examples/metrics/src/assert_multiple_emf_blobs.py b/examples/metrics/src/assert_multiple_emf_blobs.py index 6ed89460788..9c813632bf5 100644 --- a/examples/metrics/src/assert_multiple_emf_blobs.py +++ b/examples/metrics/src/assert_multiple_emf_blobs.py @@ -5,15 +5,16 @@ import pytest -@pytest.fixture -def lambda_context(): - @dataclass - class LambdaContext: - function_name: str = "test" - memory_limit_in_mb: int = 128 - invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" - aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" +@dataclass +class LambdaContext: + function_name: str = "test" + memory_limit_in_mb: int = 128 + invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test" + aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72" + +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() @@ -21,7 +22,7 @@ def capture_metrics_output_multiple_emf_objects(capsys): return [json.loads(line.strip()) for line in capsys.readouterr().out.split("\n") if line] -def test_log_metrics(capsys, lambda_context): +def test_log_metrics(capsys, lambda_context: LambdaContext): assert_multiple_emf_blobs_module.lambda_handler({}, lambda_context) cold_start_blob, custom_metrics_blob = capture_metrics_output_multiple_emf_objects(capsys) diff --git a/tests/functional/idempotency/_boto3/conftest.py b/tests/functional/idempotency/_boto3/conftest.py index 044c091c12b..0b113c08753 100644 --- a/tests/functional/idempotency/_boto3/conftest.py +++ b/tests/functional/idempotency/_boto3/conftest.py @@ -29,18 +29,19 @@ def lambda_apigw_event(): return load_event("apiGatewayProxyV2Event.json") -@pytest.fixture -def lambda_context(): - class LambdaContext: - def __init__(self): - self.function_name = "test-func" - self.memory_limit_in_mb = 128 - self.invoked_function_arn = "arn:aws:lambda:eu-west-1:809313241234:function:test-func" - self.aws_request_id = "52fdfc07-2182-154f-163f-5f0f9a621d72" +class LambdaContext: + def __init__(self): + self.function_name = "test-func" + self.memory_limit_in_mb = 128 + self.invoked_function_arn = "arn:aws:lambda:eu-west-1:809313241234:function:test-func" + self.aws_request_id = "52fdfc07-2182-154f-163f-5f0f9a621d72" + + def get_remaining_time_in_millis(self) -> int: + return 1000 - def get_remaining_time_in_millis(self) -> int: - return 1000 +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() diff --git a/tests/functional/idempotency/_redis/test_redis_layer.py b/tests/functional/idempotency/_redis/test_redis_layer.py index b2cec340ed2..c2a0976b0ab 100644 --- a/tests/functional/idempotency/_redis/test_redis_layer.py +++ b/tests/functional/idempotency/_redis/test_redis_layer.py @@ -33,18 +33,19 @@ redis_badhost = "badhost" -@pytest.fixture -def lambda_context(): - class LambdaContext: - def __init__(self): - self.function_name = "test-func" - self.memory_limit_in_mb = 128 - self.invoked_function_arn = "arn:aws:lambda:eu-west-1:809313241234:function:test-func" - self.aws_request_id = "52fdfc07-2182-154f-163f-5f0f9a621d72" +class LambdaContext: + def __init__(self): + self.function_name = "test-func" + self.memory_limit_in_mb = 128 + self.invoked_function_arn = "arn:aws:lambda:eu-west-1:809313241234:function:test-func" + self.aws_request_id = "52fdfc07-2182-154f-163f-5f0f9a621d72" + + def get_remaining_time_in_millis(self) -> int: + return 1000 - def get_remaining_time_in_millis(self) -> int: - return 1000 +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() diff --git a/tests/integration/idempotency/test_idempotency_redis.py b/tests/integration/idempotency/test_idempotency_redis.py index bfced379dbf..6d30549e38b 100644 --- a/tests/integration/idempotency/test_idempotency_redis.py +++ b/tests/integration/idempotency/test_idempotency_redis.py @@ -25,24 +25,25 @@ def redis_container_image(): return "public.ecr.aws/docker/library/redis:7.2-alpine" -@pytest.fixture -def lambda_context(): - class LambdaContext: - def __init__(self): - self.function_name = "test-func" - self.memory_limit_in_mb = 128 - self.invoked_function_arn = "arn:aws:lambda:eu-west-1:809313241234:function:test-func" - self.aws_request_id = "52fdfc07-2182-154f-163f-5f0f9a621d72" +class LambdaContext: + def __init__(self): + self.function_name = "test-func" + self.memory_limit_in_mb = 128 + self.invoked_function_arn = "arn:aws:lambda:eu-west-1:809313241234:function:test-func" + self.aws_request_id = "52fdfc07-2182-154f-163f-5f0f9a621d72" + + def get_remaining_time_in_millis(self) -> int: + return 1000 - def get_remaining_time_in_millis(self) -> int: - return 1000 +@pytest.fixture +def lambda_context() -> LambdaContext: return LambdaContext() # test basic def test_idempotent_function_and_lambda_handler_redis_basic( - lambda_context, + lambda_context: LambdaContext, redis_container_image, ): with RedisContainer(image=redis_container_image) as redis_container: @@ -69,7 +70,7 @@ def lambda_handler(event, context): def test_idempotent_function_and_lambda_handler_redis_cache( - lambda_context, + lambda_context: LambdaContext, redis_container_image, ): with RedisContainer(image=redis_container_image) as redis_container: @@ -114,7 +115,7 @@ def lambda_handler(event, context): # test idem-inprogress def test_idempotent_lambda_redis_in_progress( - lambda_context, + lambda_context: LambdaContext, redis_container_image, ): """ @@ -146,7 +147,7 @@ def lambda_handler(event, context): # test -remove def test_idempotent_lambda_redis_delete( - lambda_context, + lambda_context: LambdaContext, redis_container_image, ): with RedisContainer(image=redis_container_image) as redis_container: @@ -175,7 +176,7 @@ def lambda_handler(event, context): assert handler_result2 == result -def test_idempotent_lambda_redis_credential(lambda_context, redis_container_image): +def test_idempotent_lambda_redis_credential(lambda_context: LambdaContext, redis_container_image): with RedisContainer(image=redis_container_image) as redis_container: redis_client = redis_container.get_client() From f1efd08242cb51385c9dcbfff8ef385e1f7a3e86 Mon Sep 17 00:00:00 2001 From: Bas Date: Thu, 6 Mar 2025 15:35:38 +0100 Subject: [PATCH 2/3] Remove types on parameters --- examples/data_masking/tests/test_lambda_mask.py | 2 +- .../event_handler_graphql/src/assert_async_graphql_response.py | 2 +- examples/event_handler_graphql/src/assert_graphql_response.py | 2 +- .../event_handler_rest/src/assert_rest_api_resolver_response.py | 2 +- examples/idempotency/tests/test_with_dynamodb_local.py | 2 +- examples/idempotency/tests/test_with_io_operations.py | 2 +- examples/logger/src/fake_lambda_context_for_logger.py | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/data_masking/tests/test_lambda_mask.py b/examples/data_masking/tests/test_lambda_mask.py index d7a1799a1e0..19462e4a19e 100644 --- a/examples/data_masking/tests/test_lambda_mask.py +++ b/examples/data_masking/tests/test_lambda_mask.py @@ -20,7 +20,7 @@ def lambda_context() -> LambdaContext: return LambdaContext() -def test_encrypt_lambda(lambda_context: LambdaContext): +def test_encrypt_lambda(lambda_context): # GIVEN: A sample event for testing event = {"testkey": "testvalue"} diff --git a/examples/event_handler_graphql/src/assert_async_graphql_response.py b/examples/event_handler_graphql/src/assert_async_graphql_response.py index 5ea148492d3..7ee389a8b13 100644 --- a/examples/event_handler_graphql/src/assert_async_graphql_response.py +++ b/examples/event_handler_graphql/src/assert_async_graphql_response.py @@ -24,7 +24,7 @@ def lambda_context() -> LambdaContext: @pytest.mark.asyncio -async def test_async_direct_resolver(lambda_context: LambdaContext): +async def test_async_direct_resolver(lambda_context): # GIVEN fake_event = json.loads(Path("assert_async_graphql_response.json").read_text()) diff --git a/examples/event_handler_graphql/src/assert_graphql_response.py b/examples/event_handler_graphql/src/assert_graphql_response.py index 93be113f445..4fe51554332 100644 --- a/examples/event_handler_graphql/src/assert_graphql_response.py +++ b/examples/event_handler_graphql/src/assert_graphql_response.py @@ -21,7 +21,7 @@ def lambda_context() -> LambdaContext: return LambdaContext() -def test_direct_resolver(lambda_context: LambdaContext): +def test_direct_resolver(lambda_context): # GIVEN fake_event = json.loads(Path("assert_graphql_response.json").read_text()) diff --git a/examples/event_handler_rest/src/assert_rest_api_resolver_response.py b/examples/event_handler_rest/src/assert_rest_api_resolver_response.py index 00d7b557789..80166b5b548 100644 --- a/examples/event_handler_rest/src/assert_rest_api_resolver_response.py +++ b/examples/event_handler_rest/src/assert_rest_api_resolver_response.py @@ -17,7 +17,7 @@ def lambda_context() -> LambdaContext: return LambdaContext() -def test_lambda_handler(lambda_context: LambdaContext): +def test_lambda_handler(lambda_context): minimal_event = { "path": "/todos", "httpMethod": "GET", diff --git a/examples/idempotency/tests/test_with_dynamodb_local.py b/examples/idempotency/tests/test_with_dynamodb_local.py index 10eae1d6851..bf684d41292 100644 --- a/examples/idempotency/tests/test_with_dynamodb_local.py +++ b/examples/idempotency/tests/test_with_dynamodb_local.py @@ -21,7 +21,7 @@ def lambda_context() -> LambdaContext: return LambdaContext() -def test_idempotent_lambda(lambda_context: LambdaContext): +def test_idempotent_lambda(lambda_context): # Configure the boto3 to use the endpoint for the DynamoDB Local instance dynamodb_local_client = boto3.client("dynamodb", endpoint_url="http://localhost:8000") app_test_dynamodb_local.persistence_layer.client = dynamodb_local_client diff --git a/examples/idempotency/tests/test_with_io_operations.py b/examples/idempotency/tests/test_with_io_operations.py index 536fe31b70b..3a620827d32 100644 --- a/examples/idempotency/tests/test_with_io_operations.py +++ b/examples/idempotency/tests/test_with_io_operations.py @@ -21,7 +21,7 @@ def lambda_context() -> LambdaContext: return LambdaContext() -def test_idempotent_lambda(lambda_context: LambdaContext): +def test_idempotent_lambda(lambda_context): mock_client = MagicMock() app_test_io_operations.persistence_layer.client = mock_client result = app_test_io_operations.handler({"testkey": "testvalue"}, lambda_context) diff --git a/examples/logger/src/fake_lambda_context_for_logger.py b/examples/logger/src/fake_lambda_context_for_logger.py index 66afd358281..bf608530c48 100644 --- a/examples/logger/src/fake_lambda_context_for_logger.py +++ b/examples/logger/src/fake_lambda_context_for_logger.py @@ -17,6 +17,6 @@ def lambda_context() -> LambdaContext: return LambdaContext() -def test_lambda_handler(lambda_context: LambdaContext): +def test_lambda_handler(lambda_context): test_event = {"test": "event"} fake_lambda_context_for_logger_module.handler(test_event, lambda_context) From e1e0fe2d4f63844885d2bf49652041428c83a547 Mon Sep 17 00:00:00 2001 From: Leandro Damascena Date: Thu, 6 Mar 2025 16:22:52 +0000 Subject: [PATCH 3/3] Fix highlight --- docs/core/event_handler/appsync.md | 2 +- docs/core/event_handler/bedrock_agents.md | 2 +- docs/core/metrics.md | 2 +- docs/utilities/data_masking.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/core/event_handler/appsync.md b/docs/core/event_handler/appsync.md index 0c556dedfbf..a006a3f1415 100644 --- a/docs/core/event_handler/appsync.md +++ b/docs/core/event_handler/appsync.md @@ -586,7 +586,7 @@ Here's an example of how you can test your synchronous resolvers: === "assert_graphql_response_module.py" - ```python hl_lines="11" + ```python hl_lines="10" --8<-- "examples/event_handler_graphql/src/assert_graphql_response_module.py" ``` diff --git a/docs/core/event_handler/bedrock_agents.md b/docs/core/event_handler/bedrock_agents.md index 32aa2835491..d5c1d1c9534 100644 --- a/docs/core/event_handler/bedrock_agents.md +++ b/docs/core/event_handler/bedrock_agents.md @@ -319,7 +319,7 @@ Test your routes by passing an [Agent for Amazon Bedrock proxy event](https://do === "assert_bedrock_agent_response.py" - ```python hl_lines="21-23 27" + ```python hl_lines="22-24 28" --8<-- "examples/event_handler_bedrock_agents/src/assert_bedrock_agent_response.py" ``` diff --git a/docs/core/metrics.md b/docs/core/metrics.md index 88f0292231d..e103969e05f 100644 --- a/docs/core/metrics.md +++ b/docs/core/metrics.md @@ -425,7 +425,7 @@ You can read standard output and assert whether metrics have been flushed. Here' This will be needed when using `capture_cold_start_metric=True`, or when both `Metrics` and `single_metric` are used. - ```python hl_lines="20-21 27" + ```python hl_lines="21-22 28" --8<-- "examples/metrics/src/assert_multiple_emf_blobs.py" ``` diff --git a/docs/utilities/data_masking.md b/docs/utilities/data_masking.md index 94e470aa965..1de6419c390 100644 --- a/docs/utilities/data_masking.md +++ b/docs/utilities/data_masking.md @@ -671,7 +671,7 @@ Testing your code with a simple erase operation === "test_lambda_mask.py" -```python hl_lines="22" +```python --8<-- "examples/data_masking/tests/test_lambda_mask.py" ```