Best Python code snippet using localstack_python
test_stepfunctions.py
Source:test_stepfunctions.py
...135 definition = json.dumps(definition)136 result = self.sfn_client.create_state_machine(137 name=CHOICE_STATE_MACHINE_NAME, definition=definition, roleArn=role_arn)138 # assert that the SM has been created139 self.assert_machine_created(state_machines_before)140 # run state machine141 state_machines = self.sfn_client.list_state_machines()['stateMachines']142 sm_arn = [m['stateMachineArn'] for m in state_machines if m['name'] == CHOICE_STATE_MACHINE_NAME][0]143 input = {'x': '1', 'y': '2'}144 result = self.sfn_client.start_execution(stateMachineArn=sm_arn, input=json.dumps(input))145 self.assertTrue(result.get('executionArn'))146 # define expected output147 test_output = {**input, 'added': {'Hello': TEST_RESULT_VALUE}}148 def check_result():149 result = self._get_execution_results(sm_arn)150 self.assertEqual(result, test_output)151 # assert that the result is correct152 retry(check_result, sleep=2, retries=10)153 # clean up154 self.sfn_client.delete_state_machine(stateMachineArn=sm_arn)155 def test_create_run_map_state_machine(self):156 names = ['Bob', 'Meg', 'Joe']157 test_input = [{'map': name} for name in names]158 test_output = [{'Hello': name} for name in names]159 state_machines_before = self.sfn_client.list_state_machines()['stateMachines']160 role_arn = aws_stack.role_arn('sfn_role')161 definition = clone(MAP_STATE_MACHINE_DEF)162 lambda_arn_3 = aws_stack.lambda_function_arn(TEST_LAMBDA_NAME_3)163 definition['States']['ExampleMapState']['Iterator']['States']['CallLambda']['Resource'] = lambda_arn_3164 definition = json.dumps(definition)165 result = self.sfn_client.create_state_machine(166 name=MAP_STATE_MACHINE_NAME, definition=definition, roleArn=role_arn)167 # assert that the SM has been created168 state_machines_after = self.assert_machine_created(state_machines_before)169 # run state machine170 sm_arn = [m['stateMachineArn'] for m in state_machines_after if m['name'] == MAP_STATE_MACHINE_NAME][0]171 result = self.sfn_client.start_execution(stateMachineArn=sm_arn, input=json.dumps(test_input))172 self.assertTrue(result.get('executionArn'))173 def check_invocations():174 self.assertIn(lambda_arn_3, lambda_api.LAMBDA_EXECUTOR.function_invoke_times)175 # assert that the result is correct176 result = self._get_execution_results(sm_arn)177 self.assertEqual(result, test_output)178 # assert that the lambda has been invoked by the SM execution179 retry(check_invocations, sleep=1, retries=10)180 # clean up181 self.sfn_client.delete_state_machine(stateMachineArn=sm_arn)182 def test_create_run_state_machine(self):183 state_machines_before = self.sfn_client.list_state_machines()['stateMachines']184 # create state machine185 role_arn = aws_stack.role_arn('sfn_role')186 definition = clone(STATE_MACHINE_DEF)187 lambda_arn_1 = aws_stack.lambda_function_arn(TEST_LAMBDA_NAME_1)188 lambda_arn_2 = aws_stack.lambda_function_arn(TEST_LAMBDA_NAME_2)189 definition['States']['step1']['Resource'] = lambda_arn_1190 definition['States']['step2']['Resource'] = lambda_arn_2191 definition = json.dumps(definition)192 result = self.sfn_client.create_state_machine(193 name=STATE_MACHINE_NAME, definition=definition, roleArn=role_arn)194 # assert that the SM has been created195 self.assert_machine_created(state_machines_before)196 # run state machine197 state_machines = self.sfn_client.list_state_machines()['stateMachines']198 sm_arn = [m['stateMachineArn'] for m in state_machines if m['name'] == STATE_MACHINE_NAME][0]199 result = self.sfn_client.start_execution(stateMachineArn=sm_arn)200 self.assertTrue(result.get('executionArn'))201 def check_invocations():202 self.assertIn(lambda_arn_1, lambda_api.LAMBDA_EXECUTOR.function_invoke_times)203 self.assertIn(lambda_arn_2, lambda_api.LAMBDA_EXECUTOR.function_invoke_times)204 # assert that the result is correct205 result = self._get_execution_results(sm_arn)206 self.assertEqual(result['result_value'], {'Hello': TEST_RESULT_VALUE})207 # assert that the lambda has been invoked by the SM execution208 retry(check_invocations, sleep=0.7, retries=25)209 # clean up210 self.sfn_client.delete_state_machine(stateMachineArn=sm_arn)211 def assert_machine_created(self, state_machines_before):212 def check():213 state_machines_after = self.sfn_client.list_state_machines()['stateMachines']214 self.assertEqual(len(state_machines_after), len(state_machines_before) + 1)215 return state_machines_after216 return retry(check, sleep=1, retries=4)217 def _get_execution_results(self, sm_arn):218 response = self.sfn_client.list_executions(stateMachineArn=sm_arn)219 executions = sorted(response['executions'], key=lambda x: x['startDate'])220 execution = executions[-1]221 result = self.sfn_client.get_execution_history(executionArn=execution['executionArn'])222 events = sorted(result['events'], key=lambda event: event['timestamp'])223 result = json.loads(events[-1]['executionSucceededEventDetails']['output'])...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!