"""Set ingestion state tests.""" from unittest.mock import patch from common.constants.grps_ingestion_status import INGEST_FAILED, \ INGEST_SKIPPED_ALREADY_EXISTS, INGEST_SUCCEEDED from common.constants.sfn_status import FAILURE, SUCCESS from src.constants.queries import UPDATE_GRPS_INGESTER_EXTENDED, \ UPDATE_INGESTION_STATE from src.index import handler @patch('src.index.execute_snowflake_query') def test_success_status(mock_execute_snowflake_query, context): """Lambda function to set success ingestion state.""" context['status'] = SUCCESS handler(context, None) mock_execute_snowflake_query.assert_any_call( UPDATE_INGESTION_STATE, {'status': INGEST_SUCCEEDED, 'grps_ingestion_id': 12345678}) @patch('src.index.execute_snowflake_query') def test_failure_status(mock_execute_snowflake_query, context): """Lambda function to set failure ingestion state.""" context['status'] = FAILURE handler(context, None) mock_execute_snowflake_query.assert_any_call( UPDATE_INGESTION_STATE, {'status': INGEST_FAILED, 'grps_ingestion_id': 12345678}) @patch('src.index.execute_snowflake_query') def test_failure_on_first_step(mock_execute_snowflake_query, failed_of_first_step_context): """Lambda function to set failure ingestion state.""" result = handler(failed_of_first_step_context, None) mock_execute_snowflake_query.assert_any_call( UPDATE_GRPS_INGESTER_EXTENDED, { 'grps_ingestion_id': 251, 'isrcs': [], 'deleted_tracks': [], 'error': '{"Error": "RepOwnerDoNotIngestException", ' '"Cause": "{not found}"}' } ) assert result['grps_ingestion_id'] == 251 assert result['product']['upc'] == '196874264003' @patch('src.index.execute_snowflake_query') def test_extended_meta_update(mock_execute_snowflake_query, context): """Lambda function to set failure ingestion state.""" context['status'] = FAILURE handler(context, None) mock_execute_snowflake_query.assert_any_call( UPDATE_GRPS_INGESTER_EXTENDED, { 'grps_ingestion_id': 12345678, 'isrcs': ['USSM12308808', 'USSM12308807'], 'deleted_tracks': [5, 6, 7], 'error': None } ) @patch('src.index.execute_snowflake_query') def test_product_already_exists(mock_execute_snowflake_query, product_already_exists_context): """Lambda function to set failure ingestion state.""" handler(product_already_exists_context, None) mock_execute_snowflake_query.assert_any_call( UPDATE_INGESTION_STATE, {'status': INGEST_SKIPPED_ALREADY_EXISTS, 'grps_ingestion_id': 76})