1- from unittest .mock import Mock , patch
21from unittest import TestCase
32
43from samtranslator .model .eventsources .push import Schedule
54from samtranslator .model .lambda_ import LambdaFunction
65from samtranslator .model .exceptions import InvalidEventException
6+ from parameterized import parameterized
77
88
99class ScheduleEventSource (TestCase ):
@@ -13,6 +13,27 @@ def setUp(self):
1313 self .schedule_event_source .Schedule = "rate(1 minute)"
1414 self .func = LambdaFunction ("func" )
1515
16+ def test_to_cloudformation_returns_permission_and_schedule_resources (self ):
17+ resources = self .schedule_event_source .to_cloudformation (function = self .func )
18+ self .assertEqual (len (resources ), 2 )
19+ self .assertEqual (resources [0 ].resource_type , "AWS::Events::Rule" )
20+ self .assertEqual (resources [1 ].resource_type , "AWS::Lambda::Permission" )
21+
22+ schedule = resources [0 ]
23+ self .assertEqual (schedule .ScheduleExpression , "rate(1 minute)" )
24+ self .assertIsNone (schedule .State )
25+
26+ def test_to_cloudformation_transforms_enabled_boolean_to_state (self ):
27+ self .schedule_event_source .Enabled = True
28+ resources = self .schedule_event_source .to_cloudformation (function = self .func )
29+ schedule = resources [0 ]
30+ self .assertEqual (schedule .State , "ENABLED" )
31+
32+ self .schedule_event_source .Enabled = False
33+ resources = self .schedule_event_source .to_cloudformation (function = self .func )
34+ schedule = resources [0 ]
35+ self .assertEqual (schedule .State , "DISABLED" )
36+
1637 def test_to_cloudformation_with_retry_policy (self ):
1738 retry_policy = {"MaximumRetryAttempts" : "10" , "MaximumEventAgeInSeconds" : "300" }
1839 self .schedule_event_source .RetryPolicy = retry_policy
@@ -70,3 +91,19 @@ def test_to_cloudformation_with_dlq_generated_with_intrinsic_function_custom_log
7091 self .schedule_event_source .DeadLetterConfig = dead_letter_config
7192 with self .assertRaises (InvalidEventException ):
7293 self .schedule_event_source .to_cloudformation (function = self .func )
94+
95+ @parameterized .expand (
96+ [
97+ (True , "Enabled" ),
98+ (True , "Disabled" ),
99+ (True , {"FN:FakeIntrinsic" : "something" }),
100+ (False , "Enabled" ),
101+ (False , "Disabled" ),
102+ (False , {"FN:FakeIntrinsic" : "something" }),
103+ ]
104+ )
105+ def test_to_cloudformation_invalid_defined_both_enabled_and_state_provided (self , enabled_value , state_value ):
106+ self .schedule_event_source .Enabled = enabled_value
107+ self .schedule_event_source .State = state_value
108+ with self .assertRaises (InvalidEventException ):
109+ self .schedule_event_source .to_cloudformation (function = self .func )
0 commit comments