Skip to content

refactor(tests): add LambdaContext type in tests #6214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 changes: 1 addition & 1 deletion docs/core/event_handler/appsync.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

Expand Down
2 changes: 1 addition & 1 deletion docs/core/event_handler/bedrock_agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

Expand Down
2 changes: 1 addition & 1 deletion docs/core/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

Expand Down
2 changes: 1 addition & 1 deletion docs/utilities/data_masking.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```

Expand Down
19 changes: 10 additions & 9 deletions examples/batch_processing/src/getting_started_with_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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]
Expand Down
21 changes: 11 additions & 10 deletions examples/data_masking/tests/test_lambda_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
17 changes: 9 additions & 8 deletions examples/event_handler_graphql/src/assert_graphql_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
23 changes: 12 additions & 11 deletions examples/idempotency/tests/test_disabling_idempotency_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 11 additions & 10 deletions examples/idempotency/tests/test_with_dynamodb_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
21 changes: 11 additions & 10 deletions examples/idempotency/tests/test_with_io_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
Loading
Loading