"""Test handler.""" from unittest.mock import ANY, MagicMock, patch from ddex_ingester_common.constants.catalog_ingestion import FAILURE from index import handler, write_catalog_ingestion @patch('index.write_catalog_ingestion') @patch('index.update_data_ingest_status') def test_handler( mock_update_data_ingest_status, mock_write_catalog_ingestion, context, ): """Test the main handler.""" context['write_catalog_ingestion'] = True expected_status = context['status'] output = handler(context, None) mock_write_catalog_ingestion.assert_called_once_with( context, expected_status, None, ANY ) assert output @patch('index.write_catalog_ingestion') @patch('index.update_data_ingest_status') def test_handler_failure( mock_update_data_ingest_status, mock_write_catalog_ingestion, context, ): """Test the main handler with a failure status.""" context['write_catalog_ingestion'] = True context['status'] = FAILURE expected_status = context['status'] error_message = str(context['context']['errors']) output = handler(context, None) mock_write_catalog_ingestion.assert_called_once_with( context, expected_status, error_message, ANY ) assert output @patch('index.config') @patch('index.CatalogIngestion') def test_write_catalog_ingestion( mock_catalog_ingestion, mock_config, context ): """Test write catalog ingestion pulls data from context and saves.""" mock_config.catalog_ingestion_session = MagicMock() mock_logger = MagicMock() status = 'Success' error_message = str(context.get('context').get('errors')) write_catalog_ingestion(context, status, error_message, mock_logger) context = context.get('context') expected_state_machine_name = context.get('state_machine_name') expected_execution_name = context.get('execution_name') expected_bucket = context.get('bucket') expected_key = context.get('key') expected_execution_start_time = context.get('execution_start_time') mock_catalog_ingestion.assert_called_once_with( state_machine_name=expected_state_machine_name, state_machine_execution_name=expected_execution_name, catalog_ingestion_source_id='bulk-metadata-ingester', s3_bucket_name=expected_bucket, s3_key_name=expected_key, ingest_format='csv', timestamp=expected_execution_start_time, vendor_id=context.get('product').get('vendor_id'), subaccount_id=context.get('product').get('subaccount_id'), status=status, error_message=error_message, ) mock_config.catalog_ingestion_session.add.assert_called_once() mock_config.catalog_ingestion_session.save.assert_called_once()