1+ import json
12import sys
23from unittest .mock import Mock , patch
34
5+ import pytest
6+
47
58@patch ("boto3.client" )
69def test_trigger_async_processing_error (mock_boto_client : Mock , mock_env : Mock ):
@@ -18,11 +21,12 @@ def test_trigger_async_processing_error(mock_boto_client: Mock, mock_env: Mock):
1821 # perform operation
1922 event_data = {"test" : "data" }
2023 # Should not raise exception even if Lambda invoke fails
21- trigger_async_processing (event = event_data , event_id = "evt123" )
24+ with pytest .raises (Exception ):
25+ trigger_async_processing (event = event_data , event_id = "evt123" )
2226
23- # assertions
24- mock_boto_client .assert_called_once_with ("lambda" )
25- mock_lambda_client .invoke .assert_called_once ()
27+ # assertions
28+ mock_boto_client .assert_called_once_with ("lambda" )
29+ mock_lambda_client .invoke .assert_called_once ()
2630
2731
2832@patch ("boto3.client" )
@@ -47,3 +51,91 @@ def test_trigger_async_processing(
4751 # assertions
4852 mock_boto_client .assert_called_once_with ("lambda" )
4953 mock_lambda_client .invoke .assert_called_once ()
54+
55+
56+ @patch ("boto3.client" )
57+ def test_trigger_pull_request_processing (
58+ mock_boto_client : Mock ,
59+ mock_env : Mock ,
60+ ):
61+ """Test triggering async processing"""
62+ # set up mocks
63+ mock_cf_client = Mock (name = "cloudformation_client" )
64+ mock_lambda_client = Mock (name = "lambda_client" )
65+
66+ # Make boto3.client return the right mock depending on service
67+ def client_side_effect (service_name , * args , ** kwargs ):
68+ if service_name == "cloudformation" :
69+ return mock_cf_client
70+ elif service_name == "lambda" :
71+ return mock_lambda_client
72+ else :
73+ raise ValueError (f"Unexpected client: { service_name } " )
74+
75+ mock_boto_client .side_effect = client_side_effect
76+
77+ mock_cf_client .describe_stacks .return_value = {
78+ "Stacks" : [
79+ {
80+ "StackName" : "mystack" ,
81+ "Outputs" : [{"OutputKey" : "SlackBotLambdaArn" , "OutputValue" : "output_SlackBotLambdaArn" }],
82+ }
83+ ]
84+ }
85+
86+ # delete and import module to test
87+ if "app.utils.handler_utils" in sys .modules :
88+ del sys .modules ["app.utils.handler_utils" ]
89+ from app .utils .handler_utils import trigger_pull_request_processing
90+
91+ # perform operation
92+ event_data = {"test" : "data" }
93+ trigger_pull_request_processing (pull_request_id = "123" , event = event_data , event_id = "evt123" )
94+
95+ # assertions
96+ expected_lambda_payload = {
97+ "pull_request_processing" : True ,
98+ "slack_event" : {"event" : {"test" : "data" }, "event_id" : "evt123" },
99+ }
100+
101+ mock_lambda_client .invoke .assert_called_once_with (
102+ FunctionName = "output_SlackBotLambdaArn" , InvocationType = "Event" , Payload = json .dumps (expected_lambda_payload )
103+ )
104+
105+
106+ @patch ("boto3.client" )
107+ def test_trigger_pull_request_processing_error (
108+ mock_boto_client : Mock ,
109+ mock_env : Mock ,
110+ ):
111+ """Test triggering async processing"""
112+ # set up mocks
113+ mock_cf_client = Mock (name = "cloudformation_client" )
114+ mock_lambda_client = Mock (name = "lambda_client" )
115+
116+ # Make boto3.client return the right mock depending on service
117+ def client_side_effect (service_name , * args , ** kwargs ):
118+ if service_name == "cloudformation" :
119+ return mock_cf_client
120+ elif service_name == "lambda" :
121+ return mock_lambda_client
122+ else :
123+ raise ValueError (f"Unexpected client: { service_name } " )
124+
125+ mock_boto_client .side_effect = client_side_effect
126+
127+ mock_cf_client .invoke .side_effect = Exception ("Lambda invoke error" )
128+
129+ # delete and import module to test
130+ if "app.utils.handler_utils" in sys .modules :
131+ del sys .modules ["app.utils.handler_utils" ]
132+ from app .utils .handler_utils import trigger_pull_request_processing
133+
134+ # perform operation
135+ event_data = {"test" : "data" }
136+ with pytest .raises (Exception ):
137+ trigger_pull_request_processing (pull_request_id = "123" , event = event_data , event_id = "evt123" )
138+
139+ # assertions
140+
141+ mock_lambda_client .invoke .assert_not_called ()
0 commit comments