"""Unit tests for Spotify Marketshare 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 import config from feed_ingestion.flows.spotify_marketshare. \ snowflake_executor import SpotifyMarketshareSF 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 = SpotifyMarketshareSF(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.""" for report in config.reports: table_name = 'temp_staging_raw_spotify_market_share_{}'. format(report) kwargs = {'download_date': '2024-05-01'} mock_executor.create_temp_staging_raw_table(table_name, **kwargs) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.{}'.format(table_name), 'CREATE OR REPLACE TRANSIENT 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: table_name = 'temp_staging_raw_spotify_market_share_{}'. format(report) kwargs = {'download_date': '2024-05-01'} mock_executor.load_temp_staging_raw_table( temp_staging_raw_table=table_name, **kwargs, aws=aws_config_mock, key_dir='s3://somepath') mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=['COPY INTO', 'test_db.test_schema.{}'.format(table_name)]), params={ 'aws_key_id': 'test_id', 'aws_secret_key': 'test_secret', 'aws_token': 'test_token', 's3_path': 's3://somepath'}) assert mock_executor.ex_mock.call_count == len(config.reports) def test_clean_staging_raw_table(mock_executor): """Test clean_staging_raw_table method.""" mock_executor.clean_staging_raw_table('staging_raw_table', '2018-05-01') 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_table', 'WHERE report_start_date =']), params={'date': '2018-05-01'}) def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" kwards = { 'reports': { 'revshare': { 'file_pattern': 'spotify-revshare-for-theorchard-20185.txt', 'temp_table_name': 'temp_staging_raw_spotify_market_share_revshare_20180501'}, 'legend': { 'file_pattern': 'spotify-legend-for-theorchard-20185.txt', 'temp_table_name': 'temp_staging_raw_spotify_market_share_legend_20180501'}}, 'download_date': '2024-06-01'} mock_executor.load_staging_raw_table( 'temp_staging_raw_table', 'staging_raw_table', '2018-05-01', **kwards) assert mock_executor.ex_mock.call_count == 1 params = dict(config.product_types) params.update({'date': '2018-05-01', 'storeid': 286}) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'test_db.test_schema.staging_raw_table', 'FROM test_db.test_schema.{}'.format( 'temp_staging_raw_spotify_market_share_revshare_20180501'), 'LEFT JOIN', 'FROM test_db.test_schema.{}'.format( 'temp_staging_raw_spotify_market_share_legend_20180501')]), params=params) def test_load_marketshare_data(mock_executor): """Test load_marketshare_data method.""" mock_executor.load_marketshare_data('2018-05-01') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'test_db.test_schema.fact_market_share']), params={'date': '2018-05-01', 'storeid': 286})