"""Unit tests for amazon_music specific Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.apple_id_mapping_sme import config from feed_ingestion.flows.apple_id_mapping_sme import snowflake_executor 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 = snowflake_executor.AppleIDMapping(sf_config_mock) aws_params = { 'aws_key_id': 'test_id', 'aws_secret_key': 'test_secret', 'aws_token': 'test_token'} with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock, patch.object(executor, 'get_aws_params', return_value=aws_params): executor.fetchall = MagicMock() yield executor def test_create_temp_staging_raw_table(mock_executor): """Test create_temp_staging_raw_table method.""" for report in config.reports: mock_executor.create_temp_staging_raw_table( temp_staging_raw_table='temp_table', report=report) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'CREATE OR REPLACE TRANSIENT', 'test_db.test_schema.temp_table']), params={}) assert mock_executor.ex_mock.call_count == len(config.reports) def test_load_temp_staging_raw_table(mock_executor, aws_config_mock): """Test load_temp_staging_raw_table method.""" for report in config.reports: mock_executor.load_temp_staging_raw_table( temp_staging_raw_table='load_temp_staging_raw_table', aws={}, key_dir='s3://somepath', error_limit=1, report=report) mock_executor.fetchall.assert_any_call( SubstringMatcher( containing=['COPY INTO', 'test_db.test_schema']), params={ 'aws_key_id': 'test_id', 'aws_secret_key': 'test_secret', 'aws_token': 'test_token', 's3_path': 's3://somepath'}) assert mock_executor.fetchall.call_count == 2 def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" for report in config.reports: mock_executor.load_staging_raw_table( temp_staging_raw_table='temp_table', staging_raw_table='staging_raw_table', date='2019-09-14', report=report) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'MERGE INTO', 'test_db.test_schema.staging_raw_table', ]), params={}) def test_update_sony_apple_id_mapping(mock_executor): """Test update_sony_apple_id_mapping method.""" mock_executor.update_sony_apple_id_mapping() mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'sony_apple_id_mapping', 'staging_raw_apple_album_mapping', 'staging_raw_apple_track_mapping' ]), params={})