Skip to content

Commit 83222dd

Browse files
refactor(tests): add LambdaContext type in tests (#6214)
* Type `LambdaContext` types in tests * Remove types on parameters * Fix highlight --------- Co-authored-by: Leandro Damascena <[email protected]>
1 parent 609faeb commit 83222dd

21 files changed

+183
-166
lines changed

docs/core/event_handler/appsync.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ Here's an example of how you can test your synchronous resolvers:
586586

587587
=== "assert_graphql_response_module.py"
588588

589-
```python hl_lines="11"
589+
```python hl_lines="10"
590590
--8<-- "examples/event_handler_graphql/src/assert_graphql_response_module.py"
591591
```
592592

docs/core/event_handler/bedrock_agents.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ Test your routes by passing an [Agent for Amazon Bedrock proxy event](https://do
319319

320320
=== "assert_bedrock_agent_response.py"
321321

322-
```python hl_lines="21-23 27"
322+
```python hl_lines="22-24 28"
323323
--8<-- "examples/event_handler_bedrock_agents/src/assert_bedrock_agent_response.py"
324324
```
325325

docs/core/metrics.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ You can read standard output and assert whether metrics have been flushed. Here'
425425

426426
This will be needed when using `capture_cold_start_metric=True`, or when both `Metrics` and `single_metric` are used.
427427

428-
```python hl_lines="20-21 27"
428+
```python hl_lines="21-22 28"
429429
--8<-- "examples/metrics/src/assert_multiple_emf_blobs.py"
430430
```
431431

docs/utilities/data_masking.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ Testing your code with a simple erase operation
671671

672672
=== "test_lambda_mask.py"
673673

674-
```python hl_lines="22"
674+
```python
675675
--8<-- "examples/data_masking/tests/test_lambda_mask.py"
676676
```
677677

examples/batch_processing/src/getting_started_with_test.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ def load_event(path: Path):
1111
return json.load(f)
1212

1313

14-
@pytest.fixture
15-
def lambda_context():
16-
@dataclass
17-
class LambdaContext:
18-
function_name: str = "test"
19-
memory_limit_in_mb: int = 128
20-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
21-
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
14+
@dataclass
15+
class LambdaContext:
16+
function_name: str = "test"
17+
memory_limit_in_mb: int = 128
18+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
19+
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
20+
2221

22+
@pytest.fixture
23+
def lambda_context() -> LambdaContext:
2324
return LambdaContext()
2425

2526

@@ -29,7 +30,7 @@ def sqs_event():
2930
return load_event(path=Path("events/sqs_event.json"))
3031

3132

32-
def test_app_batch_partial_response(sqs_event, lambda_context):
33+
def test_app_batch_partial_response(sqs_event, lambda_context: LambdaContext):
3334
# GIVEN
3435
processor_result = processor # access processor for additional assertions
3536
successful_record = sqs_event["Records"][0]

examples/data_masking/tests/test_lambda_mask.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
import test_lambda_mask
55

66

7-
@pytest.fixture
8-
def lambda_context():
9-
@dataclass
10-
class LambdaContext:
11-
function_name: str = "test"
12-
memory_limit_in_mb: int = 128
13-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:111111111:function:test"
14-
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
7+
@dataclass
8+
class LambdaContext:
9+
function_name: str = "test"
10+
memory_limit_in_mb: int = 128
11+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:111111111:function:test"
12+
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
13+
14+
def get_remaining_time_in_millis(self) -> int:
15+
return 5
1516

16-
def get_remaining_time_in_millis(self) -> int:
17-
return 5
1817

18+
@pytest.fixture
19+
def lambda_context() -> LambdaContext:
1920
return LambdaContext()
2021

2122

examples/event_handler_bedrock_agents/src/assert_bedrock_agent_response.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
import pytest
55

66

7-
@pytest.fixture
8-
def lambda_context():
9-
@dataclass
10-
class LambdaContext:
11-
function_name: str = "test"
12-
memory_limit_in_mb: int = 128
13-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
14-
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
7+
@dataclass
8+
class LambdaContext:
9+
function_name: str = "test"
10+
memory_limit_in_mb: int = 128
11+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
12+
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
13+
1514

15+
@pytest.fixture
16+
def lambda_context() -> LambdaContext:
1617
return LambdaContext()
1718

1819

19-
def test_lambda_handler(lambda_context):
20+
def test_lambda_handler(lambda_context: LambdaContext):
2021
minimal_event = {
2122
"apiPath": "/current_time",
2223
"httpMethod": "GET",

examples/event_handler_graphql/src/assert_async_graphql_response.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
)
1111

1212

13-
@pytest.fixture
14-
def lambda_context():
15-
@dataclass
16-
class LambdaContext:
17-
function_name: str = "test"
18-
memory_limit_in_mb: int = 128
19-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
20-
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
13+
@dataclass
14+
class LambdaContext:
15+
function_name: str = "test"
16+
memory_limit_in_mb: int = 128
17+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
18+
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
19+
2120

21+
@pytest.fixture
22+
def lambda_context() -> LambdaContext:
2223
return LambdaContext()
2324

2425

examples/event_handler_graphql/src/assert_graphql_response.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
from assert_graphql_response_module import Location, app # instance of AppSyncResolver
99

1010

11-
@pytest.fixture
12-
def lambda_context():
13-
@dataclass
14-
class LambdaContext:
15-
function_name: str = "test"
16-
memory_limit_in_mb: int = 128
17-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
18-
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
11+
@dataclass
12+
class LambdaContext:
13+
function_name: str = "test"
14+
memory_limit_in_mb: int = 128
15+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
16+
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
17+
1918

19+
@pytest.fixture
20+
def lambda_context() -> LambdaContext:
2021
return LambdaContext()
2122

2223

examples/event_handler_rest/src/assert_alb_api_resolver_response.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
import pytest
55

66

7-
@pytest.fixture
8-
def lambda_context():
9-
@dataclass
10-
class LambdaContext:
11-
function_name: str = "test"
12-
memory_limit_in_mb: int = 128
13-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
14-
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
7+
@dataclass
8+
class LambdaContext:
9+
function_name: str = "test"
10+
memory_limit_in_mb: int = 128
11+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
12+
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
13+
1514

15+
@pytest.fixture
16+
def lambda_context() -> LambdaContext:
1617
return LambdaContext()
1718

1819

19-
def test_lambda_handler(lambda_context):
20+
def test_lambda_handler(lambda_context: LambdaContext):
2021
minimal_event = {
2122
"path": "/todos",
2223
"httpMethod": "GET",

examples/event_handler_rest/src/assert_function_url_api_resolver_response.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
import pytest
55

66

7-
@pytest.fixture
8-
def lambda_context():
9-
@dataclass
10-
class LambdaContext:
11-
function_name: str = "test"
12-
memory_limit_in_mb: int = 128
13-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
14-
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
7+
@dataclass
8+
class LambdaContext:
9+
function_name: str = "test"
10+
memory_limit_in_mb: int = 128
11+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
12+
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
13+
1514

15+
@pytest.fixture
16+
def lambda_context() -> LambdaContext:
1617
return LambdaContext()
1718

1819

19-
def test_lambda_handler(lambda_context):
20+
def test_lambda_handler(lambda_context: LambdaContext):
2021
minimal_event = {
2122
"rawPath": "/todos",
2223
"requestContext": {

examples/event_handler_rest/src/assert_http_api_resolver_response.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
import pytest
55

66

7-
@pytest.fixture
8-
def lambda_context():
9-
@dataclass
10-
class LambdaContext:
11-
function_name: str = "test"
12-
memory_limit_in_mb: int = 128
13-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
14-
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
7+
@dataclass
8+
class LambdaContext:
9+
function_name: str = "test"
10+
memory_limit_in_mb: int = 128
11+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
12+
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
13+
1514

15+
@pytest.fixture
16+
def lambda_context() -> LambdaContext:
1617
return LambdaContext()
1718

1819

19-
def test_lambda_handler(lambda_context):
20+
def test_lambda_handler(lambda_context: LambdaContext):
2021
minimal_event = {
2122
"rawPath": "/todos",
2223
"requestContext": {

examples/event_handler_rest/src/assert_rest_api_resolver_response.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
import pytest
55

66

7-
@pytest.fixture
8-
def lambda_context():
9-
@dataclass
10-
class LambdaContext:
11-
function_name: str = "test"
12-
memory_limit_in_mb: int = 128
13-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
14-
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
7+
@dataclass
8+
class LambdaContext:
9+
function_name: str = "test"
10+
memory_limit_in_mb: int = 128
11+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:123456789012:function:test"
12+
aws_request_id: str = "da658bd3-2d6f-4e7b-8ec2-937234644fdc"
13+
1514

15+
@pytest.fixture
16+
def lambda_context() -> LambdaContext:
1617
return LambdaContext()
1718

1819

examples/idempotency/tests/test_disabling_idempotency_utility.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44
import pytest
55

66

7-
@pytest.fixture
8-
def lambda_context():
9-
@dataclass
10-
class LambdaContext:
11-
function_name: str = "test"
12-
memory_limit_in_mb: int = 128
13-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
14-
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
7+
@dataclass
8+
class LambdaContext:
9+
function_name: str = "test"
10+
memory_limit_in_mb: int = 128
11+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
12+
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
13+
14+
def get_remaining_time_in_millis(self) -> int:
15+
return 5
1516

16-
def get_remaining_time_in_millis(self) -> int:
17-
return 5
1817

18+
@pytest.fixture
19+
def lambda_context() -> LambdaContext:
1920
return LambdaContext()
2021

2122

22-
def test_idempotent_lambda_handler(monkeypatch, lambda_context):
23+
def test_idempotent_lambda_handler(monkeypatch, lambda_context: LambdaContext):
2324
# Set POWERTOOLS_IDEMPOTENCY_DISABLED before calling decorated functions
2425
monkeypatch.setenv("POWERTOOLS_IDEMPOTENCY_DISABLED", 1)
2526

examples/idempotency/tests/test_with_dynamodb_local.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
import pytest
66

77

8-
@pytest.fixture
9-
def lambda_context():
10-
@dataclass
11-
class LambdaContext:
12-
function_name: str = "test"
13-
memory_limit_in_mb: int = 128
14-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
15-
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
8+
@dataclass
9+
class LambdaContext:
10+
function_name: str = "test"
11+
memory_limit_in_mb: int = 128
12+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
13+
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
14+
15+
def get_remaining_time_in_millis(self) -> int:
16+
return 5
1617

17-
def get_remaining_time_in_millis(self) -> int:
18-
return 5
1918

19+
@pytest.fixture
20+
def lambda_context() -> LambdaContext:
2021
return LambdaContext()
2122

2223

examples/idempotency/tests/test_with_io_operations.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
import pytest
66

77

8-
@pytest.fixture
9-
def lambda_context():
10-
@dataclass
11-
class LambdaContext:
12-
function_name: str = "test"
13-
memory_limit_in_mb: int = 128
14-
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
15-
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
8+
@dataclass
9+
class LambdaContext:
10+
function_name: str = "test"
11+
memory_limit_in_mb: int = 128
12+
invoked_function_arn: str = "arn:aws:lambda:eu-west-1:809313241:function:test"
13+
aws_request_id: str = "52fdfc07-2182-154f-163f-5f0f9a621d72"
14+
15+
def get_remaining_time_in_millis(self) -> int:
16+
return 5
1617

17-
def get_remaining_time_in_millis(self) -> int:
18-
return 5
1918

19+
@pytest.fixture
20+
def lambda_context() -> LambdaContext:
2021
return LambdaContext()
2122

2223

0 commit comments

Comments
 (0)