Skip to content

Commit ca53cb1

Browse files
author
Dan Xie
committed
update 7
1 parent 92c409b commit ca53cb1

File tree

4 files changed

+74
-31
lines changed

4 files changed

+74
-31
lines changed

src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/china-support/china-forward-function/tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
"""
2+
conftest file
3+
"""
14
import pytest
25

36
# Global variables for AWS configuration

src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/china-support/china-forward-function/tests/test_handler.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import pytest
1+
"""
2+
Tests for the AWS Lambda handler that forwards events to Step Functions.
3+
"""
24
import os
35
from unittest.mock import patch, MagicMock
6+
7+
import pytest
48
import handler
59

610

@@ -11,8 +15,9 @@ def mock_env_variables(aws_settings):
1115
"""
1216
region = aws_settings["region"]
1317
partition = aws_settings["partition"]
14-
with patch.object(handler, "SFN_ARN", f"arn:{partition}:states:{region}:123456789012:stateMachine:test-sfn"):
15-
yield
18+
sfn_arn = f"arn:{partition}:states:{region}:123456789012:stateMachine:test-sfn"
19+
with patch.object(handler, "SFN_ARN", sfn_arn):
20+
yield sfn_arn
1621

1722

1823
@pytest.fixture
@@ -40,19 +45,25 @@ def mock_boto3_session():
4045

4146
@pytest.fixture
4247
def mock_stepfunction():
48+
"""
49+
Fixture to mock the Stepfunction class in the handler module.
50+
"""
4351
mock_sfn = MagicMock()
4452
mock_sfn.invoke_sfn_execution.return_value = ({"executionArn": "test-arn"}, "test-state-name")
4553
with patch("handler.Stepfunction", return_value=mock_sfn):
4654
yield mock_sfn
4755

4856

4957
class TestLambdaHandler:
58+
"""Tests for the lambda_handler function."""
59+
5060
def test_lambda_handler_with_organizations_event(
51-
self, mock_env_variables, mock_boto3_session, mock_stepfunction, aws_settings
61+
self, mock_env_variables, mock_boto3_session, mock_stepfunction, aws_settings # pylint: disable=unused-argument
5262
):
63+
"""Test handling of an AWS Organizations event."""
5364
region = aws_settings["region"]
5465
partition = aws_settings["partition"]
55-
# Test with an AWS Organizations event
66+
5667
event = {
5768
"source": "aws.organizations",
5869
"detail-type": "AWS API Call via CloudTrail",
@@ -68,24 +79,29 @@ def test_lambda_handler_with_organizations_event(
6879
handler.Stepfunction.assert_called_once()
6980

7081
# Check that invoke_sfn_execution was called with correct parameters
82+
expected_arn = f"arn:{partition}:states:{region}:123456789012:stateMachine:test-sfn"
7183
mock_stepfunction.invoke_sfn_execution.assert_called_once_with(
72-
sfn_arn=f"arn:{partition}:states:{region}:123456789012:stateMachine:test-sfn", input_data=event
84+
sfn_arn=expected_arn,
85+
input_data=event
7386
)
7487

7588
def test_lambda_handler_with_non_organizations_event(
76-
self, mock_env_variables, mock_boto3_session, mock_stepfunction
89+
self, mock_env_variables, mock_boto3_session, mock_stepfunction # pylint: disable=unused-argument
7790
):
78-
# Test with a non-AWS Organizations event
79-
event = {"source": "aws.ec2", "detail-type": "EC2 Instance State-change Notification"}
91+
"""Test handling of a non-AWS Organizations event."""
92+
event = {
93+
"source": "aws.ec2",
94+
"detail-type": "EC2 Instance State-change Notification"
95+
}
8096

8197
handler.lambda_handler(event, {})
8298

8399
# Check that Stepfunction was not instantiated
84100
handler.Stepfunction.assert_not_called()
85101
mock_stepfunction.invoke_sfn_execution.assert_not_called()
86102

87-
def test_lambda_handler_with_missing_source(self, mock_env_variables, mock_boto3_session, mock_stepfunction):
88-
# Test with an event missing the source field
103+
def test_lambda_handler_with_missing_source(self, mock_env_variables, mock_boto3_session, mock_stepfunction): # pylint: disable=unused-argument
104+
"""Test handling of an event missing the source field."""
89105
event = {"detail-type": "Some Event", "detail": {}}
90106

91107
handler.lambda_handler(event, {})
@@ -96,11 +112,14 @@ def test_lambda_handler_with_missing_source(self, mock_env_variables, mock_boto3
96112

97113

98114
def test_sfn_name_extraction(aws_settings):
115+
"""Test extraction of Step Function name from ARN."""
99116
sfn_name = "test-sfn"
100-
sfn_arn = f"arn:{aws_settings['partition']}:states:{aws_settings['region']}:123456789012:stateMachine:{sfn_name}"
117+
region = aws_settings["region"]
118+
partition = aws_settings["partition"]
119+
sfn_arn = f"arn:{partition}:states:{region}:123456789012:stateMachine:{sfn_name}"
101120
with patch.dict(os.environ, {"SFN_ARN": sfn_arn}):
102121
# Re-import to get the updated SFN_ARN value
103122
import importlib
104123

105124
importlib.reload(handler)
106-
assert handler.sfn_name == sfn_name
125+
assert handler.sfn_name == sfn_name

src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/china-support/china-forward-function/tests/test_stepfunction_helper.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import json
2-
import pytest
32
from decimal import Decimal
43
from unittest.mock import MagicMock, patch
4+
5+
import pytest
56
from stepfunction_helper import Stepfunction, convert_decimals
67

78

@@ -20,15 +21,29 @@ def test_convert_dict_with_decimals(self):
2021

2122
def test_convert_nested_structure(self):
2223
data = {
23-
"items": [{"price": Decimal("10.5"), "quantity": 2}, {"price": Decimal("20.7"), "quantity": 1}],
24+
"items": [
25+
{"price": Decimal("10.5"), "quantity": 2},
26+
{"price": Decimal("20.7"), "quantity": 1}
27+
],
2428
"total": Decimal("41.7"),
2529
}
2630
result = convert_decimals(data)
27-
expected = {"items": [{"price": "10.5", "quantity": 2}, {"price": "20.7", "quantity": 1}], "total": "41.7"}
31+
expected = {
32+
"items": [
33+
{"price": "10.5", "quantity": 2},
34+
{"price": "20.7", "quantity": 1}
35+
],
36+
"total": "41.7"
37+
}
2838
assert result == expected
2939

3040
def test_non_decimal_values_unchanged(self):
31-
data = {"name": "Test", "active": True, "count": 5, "items": ["a", "b", "c"]}
41+
data = {
42+
"name": "Test",
43+
"active": True,
44+
"count": 5,
45+
"items": ["a", "b", "c"]
46+
}
3247
assert convert_decimals(data) == data
3348

3449

@@ -101,7 +116,8 @@ def test_invoke_sfn_execution_with_decimal_data(self, stepfunction, mock_session
101116

102117
with patch("uuid.uuid4", return_value="mocked-uuid"):
103118
stepfunction.invoke_sfn_execution(
104-
sfn_arn=f"arn:{partition}:states:{region}:123456789012:stateMachine:test-sfn", input_data=input_data
119+
sfn_arn=f"arn:{partition}:states:{region}:123456789012:stateMachine:test-sfn",
120+
input_data=input_data
105121
)
106122

107123
mock_client.start_execution.assert_called_once_with(
@@ -123,4 +139,4 @@ def test_invoke_sfn_execution_exception(self, stepfunction, mock_session, aws_se
123139
)
124140

125141
assert "Test exception" in str(excinfo.value)
126-
stepfunction.logger.error.assert_called_once()
142+
stepfunction.logger.error.assert_called_once()

src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/china-support/tests/test_create_s3_cn.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# test_create_s3_cn.py
1+
"""
2+
s3 bucket creation script
3+
"""
24

3-
import os
45
import pytest
56
from unittest.mock import patch, MagicMock
67

78
# Import the module to test
89
import create_s3_cn
910

11+
1012
@pytest.fixture
1113
def mock_env_vars():
1214
"""
@@ -15,13 +17,13 @@ def mock_env_vars():
1517
# Save original values
1618
original_region = create_s3_cn.REGION_DEFAULT
1719
original_account_id = create_s3_cn.MANAGEMENT_ACCOUNT_ID
18-
20+
1921
# Patch the module variables directly
2022
create_s3_cn.REGION_DEFAULT = "cn-northwest-1"
2123
create_s3_cn.MANAGEMENT_ACCOUNT_ID = "123456789012"
22-
24+
2325
yield
24-
26+
2527
# Restore original values
2628
create_s3_cn.REGION_DEFAULT = original_region
2729
create_s3_cn.MANAGEMENT_ACCOUNT_ID = original_account_id
@@ -43,7 +45,8 @@ def mock_logger():
4345
with patch("create_s3_cn.LOGGER") as mock_logger:
4446
yield mock_logger
4547

46-
def test_create_s3_bucket_china(mock_env_vars, mock_cloudformation, mock_logger):
48+
49+
def test_create_s3_bucket_china(mock_env_vars, mock_cloudformation, mock_logger): # pylint: disable=unused-argument
4750
"""Test creating an S3 bucket in China region."""
4851
# Arrange
4952
bucket_name = "adf-china-bootstrap-cn-northwest-1-123456789012"
@@ -54,7 +57,7 @@ def test_create_s3_bucket_china(mock_env_vars, mock_cloudformation, mock_logger)
5457
# Assert
5558
# Remove this line as it's incorrect - we're not calling the mock directly
5659
# mock_cloudformation.assert_called_once()
57-
60+
5861
# Instead, check that create_stack was called on the mock instance
5962
mock_cloudformation.create_stack.assert_called_once()
6063

@@ -66,7 +69,7 @@ def test_create_s3_bucket_china(mock_env_vars, mock_cloudformation, mock_logger)
6669
assert kwargs["region"] == "cn-northwest-1"
6770
assert kwargs["deployment_account_region"] == "cn-northwest-1"
6871
assert kwargs["stack_name"] == "adf-regional-base-china-bucket"
69-
assert kwargs["account_id"] == "123456789012"
72+
assert kwargs["account_id"] == "123456789012"
7073

7174
# Verify parameters passed to CloudFormation
7275
parameters = kwargs["parameters"]
@@ -78,7 +81,7 @@ def test_create_s3_bucket_china(mock_env_vars, mock_cloudformation, mock_logger)
7881
mock_logger.info.assert_called_with("Deploy S3 bucket %s...", bucket_name)
7982

8083

81-
def test_create_s3_bucket_exception( mock_env_vars, mock_cloudformation, mock_logger):
84+
def test_create_s3_bucket_exception(mock_env_vars, mock_cloudformation, mock_logger): # pylint: disable=unused-argument
8285
"""Test exception handling when creating an S3 bucket fails."""
8386
# Arrange
8487
bucket_name = "adf-china-bootstrap-cn-northwest-1-123456789012"
@@ -92,12 +95,13 @@ def test_create_s3_bucket_exception( mock_env_vars, mock_cloudformation, mock_lo
9295
mock_logger.error.assert_called_once()
9396
assert "Failed to process _create_s3_bucket" in mock_logger.error.call_args[0][0]
9497

95-
def test_main_function(mock_env_vars, monkeypatch):
98+
99+
def test_main_function(mock_env_vars, monkeypatch): # pylint: disable=unused-argument
96100
"""Test the main function calls _create_s3_bucket with correct bucket name."""
97101
# Arrange
98102
mock_create_bucket = MagicMock()
99103
monkeypatch.setattr(create_s3_cn, "_create_s3_bucket", mock_create_bucket)
100-
104+
101105
# Update expected bucket name to match actual implementation
102106
expected_bucket_name = "adf-china-bootstrap-cn-northwest-1-123456789012"
103107

@@ -107,7 +111,8 @@ def test_main_function(mock_env_vars, monkeypatch):
107111
# Assert
108112
mock_create_bucket.assert_called_once_with(expected_bucket_name)
109113

110-
def test_cloudformation_template_path(mock_env_vars, mock_cloudformation):
114+
115+
def test_cloudformation_template_path(mock_env_vars, mock_cloudformation): # pylint: disable=unused-argument
111116
"""Test the CloudFormation template path is correct."""
112117
# Arrange
113118
bucket_name = "test-bucket"

0 commit comments

Comments
 (0)