"""Test handler.""" from unittest.mock import MagicMock, patch from botocore.errorfactory import ClientError import index @patch('index.boto3') @patch('index.get_current_logger') @patch('index.uuid.uuid4', return_value='1234567890') @patch('index.path.basename', return_value='bulk-metadata-ingester') @patch('index.config.STATE_MACHINE_ARN', 'bulk-metadata-ingester-arn') @patch('index.get_execution_id', return_value='123456789') def test_handler( mock_get_execution_id, mock_path_basename, mock_uuid, mock_get_logger, mock_boto3, cloudwatch_event): """Test handler.""" mock_get_logger.return_value = MagicMock() expected_value = 'Execution ARN' mock_client = MagicMock() mock_client.start_execution.return_value = {'executionArn': expected_value} mock_boto3.client.return_value = mock_client return_value = index.handler(cloudwatch_event, {}) assert return_value == expected_value mock_client.start_execution.assert_called_with( stateMachineArn='bulk-metadata-ingester-arn', name='bulk-metadata-ingester-1234567890', input='{"key": "bulk-metadata/ALL_DDEX_TO_CSV_2023-02-10_2023-03-01.csv", "bucket": "qa-bulk-metadata-ingester", "execution_name": "bulk-metadata-ingester-1234567890", "execution_id": "123456789", "state_machine_name": "test-bulk-metadata-ingester-sfn", "correlation_id": "1234567890"}' # noqa: E501 ) @patch('index.s3.client.get_object') def test_get_execution_id(mock_s3_get_object): """Test get_execution_id.""" bucket = 'qa-bulk-metadata-ingester' key = 'bulk-metadata/input_files/ALL_DDEX_TO_CSV_2023-02-10_2023-03-01.csv' s3_file_data = MagicMock() s3_file_data.read.return_value = '{"execution_id": "123456789"}' mock_s3_get_object.return_value = {'Body': s3_file_data} execution_id = index.get_execution_id(bucket, key) assert execution_id == '123456789' @patch('index.s3.resource.Object') @patch('index.s3.client.get_object', side_effect=ClientError({'Error': {'Code': 'NoSuchKey'}}, '')) # noqa: E501 @patch('index.uuid.uuid4', return_value='123456789') def test_get_execution_id_new(mock_uuid, mock_s3_get_object, mock_s3_object): """Test get_execution_id generates a new id.""" bucket = 'qa-bulk-metadata-ingester' key = 'bulk-metadata/input_files/ALL_DDEX_TO_CSV_2023-02-10_2023-03-01.csv' execution_id = index.get_execution_id(bucket, key) assert execution_id == '123456789' mock_s3_object.assert_called_with( bucket, 'bulk-metadata/input_files/execution_id.json' ) mock_s3_object.return_value.put.assert_called_with( Body='{"execution_id": "123456789"}' )