"""Unit tests for YouTube Channel Names Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.youtube_channel_names.snowflake_executor import \ YouTubeChannelNamesSME, \ YouTubeChannelNamesTheOrchard from tests.conftest import SubstringMatcher @pytest.fixture(params=[ (YouTubeChannelNamesTheOrchard, 'theorchard'), (YouTubeChannelNamesSME, 'sme') ]) def mock_executor_and_params(request, sf_config_mock, monkeypatch): """Yield executor, expected_report_type, expected_licensor.""" connect_mock = MagicMock() monkeypatch.setattr(connector, 'connect', connect_mock) executor_class = request.param[0] expected_licensor = request.param[1] executor = executor_class(sf_config_mock) with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock: executor.fetchall = MagicMock() executor.fetchone = MagicMock() yield executor, expected_licensor def test_get_missing_channels(mock_executor_and_params): """Test get_missing_channels.""" mock_executor, licensor = mock_executor_and_params mock_executor.get_missing_channels('2020-07-01') assert mock_executor.fetchall.call_count == 1 mock_executor.fetchall.assert_any_call( SubstringMatcher( containing=[ 'SELECT channelid FROM', 'SELECT DISTINCT(channelid) FROM', 'test_db.test_schema.fact_youtube_asset_analytics fya', 'MINUS', 'SELECT channel_id FROM', 'test_db.test_schema.dim_youtube_channel_names']), params={'date': '2020-07-01', 'licensor': licensor}) def test_update_dim_table(mock_aws, mock_executor_and_params): """Test update_dim_table.""" mock_executor, _ = mock_executor_and_params mock_executor.update_dim_table( 's3://somepath', 'test_pattern', 'test') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'MERGE INTO', 'test_db.test_schema.dim_youtube_channel_names']), params={'aws_key_id': 'FOOBARKEY', 'aws_secret_key': 'FOOBARSECRET', 'aws_token': 'FOOBARTOKEN'}) def test_create_temp_staging_raw_table(mock_executor_and_params): """Test create_temp_staging_raw_table method.""" mock_executor, _ = mock_executor_and_params 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={'dim_table': 'dim_youtube_channel_names'}) def test_load_temp_staging_raw_table( mock_aws, mock_executor_and_params, aws_config_mock): """Test load_temp_staging_raw_table method.""" mock_executor, _ = mock_executor_and_params mock_executor.load_temp_staging_raw_table( temp_staging_raw_table='load_temp_staging_raw_table', aws=aws_config_mock, key_dir='s3://somepath', file_pattern='test_pattern') 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', 'file_pattern': 'test_pattern'})