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" ``` 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..19462e4a19e 100644 --- a/examples/data_masking/tests/test_lambda_mask.py +++ b/examples/data_masking/tests/test_lambda_mask.py @@ -4,18 +4,19 @@ 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() 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..7ee389a8b13 100644 --- a/examples/event_handler_graphql/src/assert_async_graphql_response.py +++ b/examples/event_handler_graphql/src/assert_async_graphql_response.py @@ -10,15 +10,16 @@ ) -@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() diff --git a/examples/event_handler_graphql/src/assert_graphql_response.py b/examples/event_handler_graphql/src/assert_graphql_response.py index d78698e109b..4fe51554332 100644 --- a/examples/event_handler_graphql/src/assert_graphql_response.py +++ b/examples/event_handler_graphql/src/assert_graphql_response.py @@ -8,15 +8,16 @@ 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() 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..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 @@ -4,15 +4,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: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() 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..bf684d41292 100644 --- a/examples/idempotency/tests/test_with_dynamodb_local.py +++ b/examples/idempotency/tests/test_with_dynamodb_local.py @@ -5,18 +5,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" + + 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() diff --git a/examples/idempotency/tests/test_with_io_operations.py b/examples/idempotency/tests/test_with_io_operations.py index 9d455906889..3a620827d32 100644 --- a/examples/idempotency/tests/test_with_io_operations.py +++ b/examples/idempotency/tests/test_with_io_operations.py @@ -5,18 +5,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" + + 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() diff --git a/examples/logger/src/fake_lambda_context_for_logger.py b/examples/logger/src/fake_lambda_context_for_logger.py index d3b3efc98f9..bf608530c48 100644 --- a/examples/logger/src/fake_lambda_context_for_logger.py +++ b/examples/logger/src/fake_lambda_context_for_logger.py @@ -4,15 +4,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() 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()