"""Unit tests for Spotify Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.youtube_video.snowflake_executor import YoutubeVideo from tests.conftest import SubstringMatcher @pytest.fixture def mock_sql_loader(): """Return sql_loader mock.""" sql_loader_path = ( 'feed_ingestion.flows.spotify.youtube_video.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 = YoutubeVideo(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') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.temp_staging_raw_table', 'CREATE OR REPLACE TRANSIENT TABLE']), params={}) def test_clean_staging_raw_table_theorchard(mock_executor): """Test clean_staging_raw_table method.""" mock_executor.clean_staging_raw_table('theorchard') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=['test_db.test_schema.staging_raw_youtube_video_report' '']), params={'licensor': 'theorchard'}) def test_load_staging_raw_table(mock_executor): """Test create_temp_staging_raw_table method.""" mock_executor.load_staging_raw_table('temp_staging_raw_table') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.temp_staging_raw_table', 'test_db.test_schema.staging_raw_youtube_video_report', 'INSERT INTO']), params={}) def test_update_staging_raw_table(mock_executor): """Test update_staging_raw_table method.""" mock_executor.update_staging_raw_table( date='2021-01-01', licensor='theorchard') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'UPDATE test_db.test_schema.staging_raw_youtube_video_report'] ), params={'download_date': '2021-01-01', 'licensor': 'theorchard'}) def test_update_mnc_and_owner_columns(mock_executor): """Test update_mnc_and_owner_columns method.""" mock_executor.update_mnc_and_owner_columns( date='2021-01-01', licensor='theorchard', channel_names_table_name='channel_names_table_name') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'UPDATE test_db.test_schema.channel_names_table_name'] ), params={'download_date': '2021-01-01', 'licensor': 'theorchard'})