"""Unit tests for GooglePlaySL class overriding StageLoader component.""" from unittest import mock from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.youtube_monthly import snowflake_executor from tests.conftest import SubstringMatcher from tests.flows.youtube_monthly.test_tasks import SOURCE_FILES_DICT @pytest.fixture def mock_executor( sf_config_mock, monkeypatch) -> snowflake_executor.YoutubeMonthlySF: """Yield executor context.""" connect_mock = MagicMock() monkeypatch.setattr(connector, 'connect', connect_mock) executor = snowflake_executor.YoutubeMonthlySF(sf_config_mock) with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock: executor.fetchall = MagicMock() yield executor def test_clean_staging_raw_table(mock_executor): """Test clean_staging_raw_table method.""" staging_raw_table = 'my_staging_raw_table' date = '2022-11-01' mock_executor.clean_staging_raw_table( staging_raw_table=staging_raw_table, download_date=date, mcn_account='acc18' ) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'DELETE FROM test_db.test_schema.my_staging_raw_table', 'WHERE download_date = ', ]), params={'download_date': '2022-11-01', 'mcn_account': 'acc18'}) assert mock_executor.ex_mock.call_count == 1 @pytest.mark.parametrize( 'report_name', [ 'adj_claim_summary', ] ) def test_create_temp_staging_raw_table(mock_executor, report_name): """Test create_temp_staging_raw_table method.""" mock_executor.create_temp_staging_raw_table( table_name='temp_table', report_name=report_name, ) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'CREATE OR REPLACE TRANSIENT TABLE', 'test_db.test_schema.temp_table', ]), params={}) assert mock_executor.ex_mock.call_count == 1 @patch.object(snowflake_executor, 'boto3') def test_load_temp_staging_raw_table( boto3_mock, mock_executor: snowflake_executor.YoutubeMonthlySF): """Test clean_staging_raw_table method.""" temp_staging_raw_table = 'temp_my_staging_raw_table' mock_executor.load_temp_staging_raw_table( temp_staging_raw_table=temp_staging_raw_table, filenames=[SOURCE_FILES_DICT['files'][0]['file_name']], s3_dir_path='s3://test_bucket/temp/location/', lines_to_skip=1, ) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'COPY INTO test_db.test_schema.temp_my_staging_raw_table', 'FROM %(s3_dir_path)s', 'SKIP_HEADER=%(lines_to_skip)s', ]), params=dict( file_name=['source_orchard.csv.gz'], s3_dir_path='s3://test_bucket/temp/location/', aws_key_id=boto3_mock.Session().get_credentials().access_key, aws_secret_key=boto3_mock.Session().get_credentials().secret_key, aws_token=boto3_mock.Session().get_credentials().token, lines_to_skip=1, ) ) assert mock_executor.ex_mock.call_count == 1 def test_load_staging_raw_table( mock_executor: snowflake_executor.YoutubeMonthlySF): """Test create_temp_staging_raw_table method.""" temp_staging_table_name = 'temp_staging_raw_table' staging_table_name = 'staging_raw_table' date = '2022-11-01' report_name = 'test_report_name' mock_executor.load_staging_raw_table( temp_staging_raw_table=temp_staging_table_name, staging_raw_table=staging_table_name, date=date, report_name=report_name, mcn_account='theorchardmusic', filenames=[SOURCE_FILES_DICT['files'][0]['file_name']], ) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO test_db.test_schema.staging_raw_table', 'FROM test_db.test_schema.temp_staging_raw_table', ]), params=dict( date=date, report_name=report_name, ingestion_time=mock.ANY, account='theorchardmusic', file_name=['source_orchard.csv.gz'], ) )