"""Test catalog_ingestion helper.""" from datetime import datetime from unittest.mock import call, MagicMock, patch from ddex_ingester_common.constants.rds_queries import ( INSERT_INTO_CATALOG_INGESTION, INSERT_INTO_CATALOG_INGESTION_ACTION, INSERT_INTO_CATALOG_INGESTION_VALIDATION_RESULT ) from ddex_ingester_common.helpers.catalog_ingestion import ( CatalogIngestion, CatalogIngestionAction, CatalogIngestionSession, CatalogIngestionValidationResult, ) import pytest state_machine_name = 'state_machine_name 1' execution_name = 'execution_name 1' timestamp = '2022-03-24T12:38:38.509729Z' @pytest.fixture() def catalog_ingestion_session(): """Session fixture.""" yield CatalogIngestionSession( 'host', 'db_name', 'user', 'password' ) @pytest.fixture() def catalog_ingestion_session_with_models(catalog_ingestion_session): """Session fixture with models.""" models = [ CatalogIngestion( state_machine_name=state_machine_name, state_machine_execution_name=execution_name, catalog_ingestion_source_id=1, s3_bucket_name='execution_bucket', s3_key_name='execution_path/foo/', ingest_format='ddex', timestamp=timestamp ), CatalogIngestionValidationResult( state_machine_name=state_machine_name, state_machine_execution_name=execution_name, validation_rule_id=1, response='Reject', message='Some message' ), CatalogIngestionValidationResult( state_machine_name=state_machine_name, state_machine_execution_name=execution_name, validation_rule_id=2, response='Reject', message='Another message' ), CatalogIngestionAction( state_machine_name=state_machine_name, state_machine_execution_name=execution_name, action='insert', entity_type='artwork', result='Success', time_created=timestamp ), ] catalog_ingestion_session.add(models) yield catalog_ingestion_session def test_catalog_ingestion_session_add(catalog_ingestion_session): """Test adding a model to the session.""" model = CatalogIngestion( state_machine_name='state_machine', state_machine_execution_name='execution_name', catalog_ingestion_source_id=1, s3_bucket_name='execution_bucket', s3_key_name='execution_path/foo/', ingest_format='ddex', timestamp=datetime.utcnow().isoformat() + 'Z' ) catalog_ingestion_session.add( model ) assert model in catalog_ingestion_session.data['catalog_ingestion'] def test_catalog_ingestion_session_add_multiple_models( catalog_ingestion_session): """Test adding multiple models to the session.""" catalog_ingestion = CatalogIngestion( state_machine_name='state_machine', state_machine_execution_name='execution_name', catalog_ingestion_source_id=1, s3_bucket_name='execution_bucket', s3_key_name='execution_path/foo/', ingest_format='ddex', timestamp=datetime.utcnow().isoformat() + 'Z' ) validation_result = CatalogIngestionValidationResult( state_machine_name='state_machine', state_machine_execution_name='execution_name', validation_rule_id=1, response='Reject', message='Some message' ) validation_result_2 = CatalogIngestionValidationResult( state_machine_name='state_machine', state_machine_execution_name='execution_name', validation_rule_id=2, response='Reject', message='Some message' ) catalog_ingestion_session.add([ catalog_ingestion, validation_result, validation_result_2 ]) assert len(catalog_ingestion_session.data['catalog_ingestion']) == 1 assert catalog_ingestion in catalog_ingestion_session.data[ 'catalog_ingestion'] assert len( catalog_ingestion_session.data['catalog_ingestion_validation_result'] ) == 2 assert validation_result in catalog_ingestion_session.data[ 'catalog_ingestion_validation_result'] assert validation_result_2 in catalog_ingestion_session.data[ 'catalog_ingestion_validation_result'] assert len(catalog_ingestion_session.data['catalog_ingestion_action']) == 0 @patch('ddex_ingester_common.helpers.catalog_ingestion.mysql_connection') def test_catalog_ingestion_session_save( mock_mysql_connection, catalog_ingestion_session_with_models): """Test saving models in session to the RDS DB.""" mock_rds_conn = MagicMock() mock_mysql_connection.return_value.__enter__.return_value = mock_rds_conn mock_cursor = MagicMock() mock_rds_conn.cursor.return_value.__enter__.return_value = mock_cursor expected_calls = [ call( INSERT_INTO_CATALOG_INGESTION, [( state_machine_name, execution_name, 'execution_bucket', 'execution_path/foo/', 'ddex', timestamp, 1, None, None, None, None )] ), call( INSERT_INTO_CATALOG_INGESTION_ACTION, [( state_machine_name, execution_name, 'insert', 'artwork', 'Success', timestamp, None, None, None, None, None, None, None, None, None, None, None, None, None # noqa )] ), call( INSERT_INTO_CATALOG_INGESTION_VALIDATION_RESULT, [ ( state_machine_name, execution_name, 1, 'Reject', 'Some message', None, None ), ( state_machine_name, execution_name, 2, 'Reject', 'Another message', None, None ), ] ) ] catalog_ingestion_session_with_models.save() mock_cursor.executemany.assert_has_calls(expected_calls)