"""Unit tests for Music Analytics Report Flow Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.music_analytics_reports. \ snowflake_executor import MusicAnalyticsReportsSE from tests.conftest import SubstringMatcher _date = '2022-12-07' @pytest.fixture def mock_sql_loader(): """Return sql_loader mock.""" sql_loader_path = ( 'feed_ingestion.flows.' 'music_analytics_in_review.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 = MusicAnalyticsReportsSE(sf_config_mock) with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock: yield executor def test_create_temp_staging_raw_table(mock_executor): """Test create_temp_staging_raw_table method.""" table = 'temp_staging_raw_music_analytics_in_review_20221207' mock_executor.create_temp_staging_raw_table(table) # MagicMock().assert_called_once_with() assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_called_once_with( SubstringMatcher( containing=['CREATE OR REPLACE', 'test_db.test_schema.' f'{table}']), params={}) def test_load_temp_staging_raw_table(mock_aws, mock_executor): """Test load_temp_staging_raw_table method.""" mock_executor.load_temp_staging_raw_table('test_table', { 'access_key': 'K', 'access_secret': 'S' }, {'report': 's3://test/report.csv'}) assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_called_once_with( SubstringMatcher( containing=['COPY INTO', 'test_db.test_schema.test_table']), params={'s3_path': 's3://test/report.csv', 'aws_key_id': 'FOOBARKEY', 'aws_secret_key': 'FOOBARSECRET', 'aws_token': 'FOOBARTOKEN'}) def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" mock_executor.load_staging_raw_table( 'test_temp', 'staging_raw_music_analytics_in_review', _date, kwargs={'report': 's3://test/errors.csv'}) assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_called_once_with( SubstringMatcher( containing=[ 'INSERT INTO', 'test_db.test_schema.staging_' 'raw_music_analytics_in_review']), params={'download_date': '2022-12-07', 'filename': None})