"""Unit tests for YouTube Red Marketshare Snowflake SQL executor.""" import datetime from unittest.mock import MagicMock from unittest.mock import patch from freezegun import freeze_time import pytest from snowflake_connector.etl_connector import connector from feed_ingestion.flows import YouTubeRedMarketshareSF 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 = YouTubeRedMarketshareSF(sf_config_mock) with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock: yield executor @freeze_time('2018-05-01') def test_clean_staging_raw_table(mock_executor): """Test clean_staging_raw_table method.""" mock_executor.clean_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_youtube_red_market_share']), params={ 'start_date': '2018-05-01' }) @freeze_time('2018-05-01') def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" staging_raw_red_summary_music = 'test_red_music_summary' staging_raw_red_label_summary_music = 'test_red_label_music_summary' staging_raw_red_label_summary_subscribers = 'test_red_label_subscriber_summary' # noqa: E501 first_day = '2018-05-01' last_day = '2018-05-31' mock_executor.load_staging_raw_table( staging_raw_red_summary_music=staging_raw_red_summary_music, staging_raw_red_label_summary_music=staging_raw_red_label_summary_music, # noqa: E501 staging_raw_red_label_summary_subscribers=staging_raw_red_label_summary_subscribers, # noqa: E501 first_day=first_day, last_day=last_day ) 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_youtube_red_market_share', 'test_db.test_schema.test_red_music_summary', 'test_db.test_schema.test_red_label_music_summary', 'test_db.test_schema.test_red_label_subscriber_summary' ]), params={ 'start_date': '2018-05-01', 'end_date': '2018-05-31', 'ingestion_time': datetime.datetime(2018, 5, 1), 'storeid': 569 }) @freeze_time('2018-05-01') 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': 569, 'processeddaytime': datetime.datetime(2018, 5, 1) })