"""Unit tests for Spotify Charts Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.spotify_charts.snowflake_executor import \ SpotifyCharts from tests.conftest import SubstringMatcher @pytest.fixture def mock_sql_loader(): """Return sql_loader mock.""" sql_loader_path = ( 'feed_ingestion.flows.spotify_charts.snowflake_executor.sql_loader') with patch(sql_loader_path) as sql_loader: yield sql_loader @pytest.fixture def mock_executor(sf_config_mock, monkeypatch): """Yield executor context.""" connect_mock = MagicMock() monkeypatch.setattr(connector, 'connect', connect_mock) executor = SpotifyCharts(sf_config_mock) with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock: executor.fetchall = MagicMock() 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_table='temp_table') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'CREATE OR REPLACE TRANSIENT', 'test_db.test_schema.temp_table']), params={}) def test_load_temp_staging_raw_table(mock_aws, mock_executor, aws_config_mock): """Test load_temp_staging_raw_table method.""" mock_executor.load_temp_staging_raw_table( temp_staging_raw_table='load_temp_staging_raw_table', aws=aws_config_mock, key_dir='s3://somepath') assert mock_executor.fetchall.call_count == 1 mock_executor.fetchall.assert_any_call( SubstringMatcher( containing=['COPY INTO', 'test_db.test_schema']), params={ 'aws_key_id': 'FOOBARKEY', 'aws_secret_key': 'FOOBARSECRET', 'aws_token': 'FOOBARTOKEN', 's3_path': 's3://somepath'}) def test_clean_staging_raw_table(mock_executor): """Test clean_staging_raw_table method.""" mock_executor.clean_staging_raw_table( staging_raw_table='staging_raw_table', date='2017-11-16', chart_type='chart_type', frequency='frequency', countries=['US', 'GLOBAL', 'CA']) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'DELETE FROM', 'test_db.test_schema.staging_raw_table', 'chart_type', 'frequency' ]), params={ 'date': '2017-11-16', 'chart_type': 'chart_type', 'frequency': 'frequency', 'countries': ['US', 'GLOBAL', 'CA']}) def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" mock_executor.load_staging_raw_table( temp_staging_raw_table='temp_table', staging_raw_table='staging_raw_table', date='2021-02-10', chart_type='chart_type', frequency='frequency', country='country') mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'test_db.test_schema.staging_raw_table', 'FROM test_db.test_schema.temp_table', 'chart_type', 'frequency', 'country' ]), params={ 'download_date': '2021-02-10', 'chart_type': 'chart_type', 'frequency': 'frequency', 'country': 'country' }) def test_load_spotify_tables_spotify_chart(mock_executor): """Test load_spotify_tables method.""" mock_executor.load_spotify_tables( table_name='spotify_chart', date='2021-02-10', chart_type='chart_type', frequency='frequency', countries=['US', 'GLOBAL', 'CA']) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'spotify_chart', 'FROM test_db.test_schema.staging_raw_spotify_charts', 'chart_type', 'frequency' ]), params={ 'date': '2021-02-10', 'chart_type': 'chart_type', 'frequency': 'frequency', 'countries': ['US', 'GLOBAL', 'CA'] }) def test_load_spotify_tables_spotify_track(mock_executor): """Test load_spotify_tables method.""" mock_executor.load_spotify_tables( table_name='spotify_track', date='2021-02-10', chart_type='chart_type', frequency='frequency', countries=['US', 'GLOBAL', 'CA']) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'MERGE INTO', 'spotify_track', 'FROM test_db.test_schema.staging_raw_spotify_charts', 'chart_type', 'frequency' ]), params={ 'date': '2021-02-10', 'chart_type': 'chart_type', 'frequency': 'frequency', 'countries': ['US', 'GLOBAL', 'CA'] }) def test_load_spotify_tables_spotify_label(mock_executor): """Test load_spotify_tables method.""" mock_executor.load_spotify_tables( table_name='spotify_label', date='2021-02-10', chart_type='chart_type', frequency='frequency', countries=['US', 'GLOBAL', 'CA']) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'MERGE INTO', 'spotify_label', 'FROM test_db.test_schema.staging_raw_spotify_charts', 'chart_type', 'frequency' ]), params={ 'date': '2021-02-10', 'chart_type': 'chart_type', 'frequency': 'frequency', 'countries': ['US', 'GLOBAL', 'CA'] }) def test_load_spotify_tables_spotify_track_artist(mock_executor): """Test load_spotify_tables method.""" mock_executor.load_spotify_tables( table_name='spotify_track_artist', date='2021-02-10', chart_type='chart_type', frequency='frequency', countries=['US', 'GLOBAL', 'CA']) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'MERGE INTO', 'spotify_track_artist', 'FROM test_db.test_schema.staging_raw_spotify_charts', 'chart_type', 'frequency' ]), params={ 'date': '2021-02-10', 'chart_type': 'chart_type', 'frequency': 'frequency', 'countries': ['US', 'GLOBAL', 'CA'] }) def test_load_spotify_tables_spotify_artist(mock_executor): """Test load_spotify_tables method.""" mock_executor.load_spotify_tables( table_name='spotify_artist', date='2021-02-10', chart_type='chart_type', frequency='frequency', countries=['US', 'GLOBAL', 'CA']) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'MERGE INTO', 'spotify_artist', 'FROM test_db.test_schema.staging_raw_spotify_charts', 'chart_type', 'frequency' ]), params={ 'date': '2021-02-10', 'chart_type': 'chart_type', 'frequency': 'frequency', 'countries': ['US', 'GLOBAL', 'CA'] })