"""Unit tests for Spotify Marketshare Week Ending Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.spotify_marketshare_week_ending. \ snowflake_executor import SpotifyMarketshareWeekEndingSF 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 = SpotifyMarketshareWeekEndingSF(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): 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_spotify_market_share_week_ending_20220504') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.' 'temp_staging_raw_spotify_market' '_share_week_ending_20220504', '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_spotify_' 'market_share_week_ending_20220504', aws=aws_config_mock, key_dir='s3://somepath') 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_spotify_market' '_share_week_ending_20220504']), params={ 'aws_key_id': 'test_id', 'aws_secret_key': 'test_secret', 'aws_token': 'test_token', 's3_path': 's3://somepath'}) def test_clean_staging_raw_table(mock_executor, aws_config_mock): """Test load_temp_staging_raw_table method.""" mock_executor.clean_staging_raw_table( staging_raw_table='staging_raw_spotify_' 'market_share_week_ending', date='2022-05-04') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=['DELETE FROM ', 'test_db.test_schema.' 'staging_raw_spotify_' 'market_share_week_ending', 'WHERE REPORT_DATE = %(date)s;']), params={'date': '2022-05-04'}) def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" mock_executor.load_staging_raw_table( 'temp_staging_raw_spotify_market_share_week_ending_20220504', 'staging_raw_spotify_market_share_week_ending', date='2022-05-04') 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_' 'spotify_market_share_week_ending ', 'FROM test_db.test_schema.temp_staging_raw' '_spotify_market_share_week_ending_20220504' ]), params={'date': '2022-05-04'})