"""Unit tests for MRC Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.mrc. \ snowflake_executor import MRCSFExecutor from tests.conftest import SubstringMatcher @pytest.fixture def mock_executor(sf_config_mock, monkeypatch): """Yield executor context.""" connect_mock = MagicMock() monkeypatch.setattr(connector, 'connect', connect_mock) executor = MRCSFExecutor(sf_config_mock) with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock: yield executor def test_create_temp_staging_raw_table(mock_executor): """Test create_temp_staging_raw_table method.""" mock_executor.create_temp_staging_raw_table( 'temp_staging_raw_mrc_20200708', download_date='20200708') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.' 'temp_staging_raw_mrc_20200708', 'CREATE OR REPLACE TRANSIENT TABLE']), params={}) def test_create_temp_staging_raw_mapping_table(mock_executor): """Test create_temp_staging_raw_table method.""" mock_executor.create_temp_staging_raw_table( 'temp_staging_raw_mrc_collection_metadata_20200708', download_date='20200708') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.' 'temp_staging_raw_mrc_collection_metadata_20200708', 'CREATE OR REPLACE TRANSIENT TABLE']), params={}) def test_load_temp_staging_raw_table(mock_executor, aws_config_mock): """Test load_temp_staging_raw_table method.""" mock_executor.load_temp_staging_raw_table( temp_staging_raw_table='temp_staging_raw_mrc_20200708', aws=aws_config_mock, key_dir='s3://somepath', download_date='20200708') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=['COPY INTO', 'test_db.test_schema.temp_staging_raw_mrc_20200708']), params={ 'aws_key_id': 'test', 'aws_secret_key': 'test', 's3_path': 's3://somepath'}) def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" mock_executor.load_staging_raw_table( 'staging_raw_mrc_20200708', 'staging_raw_mrc', '20200708', download_date='20200708', file='Orchard_AR_Daily_20200708.xlsx') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'test_db.test_schema.staging_raw_mrc ', 'temp.filename,', 'temp.download_date', 'FROM test_db.test_schema.staging_raw_mrc_20200708' ]), params={'filename': 'Orchard_AR_Daily_20200708.xlsx', 'download_date': '20200708'})