"""Unit tests for Youtube claim specific Snowflake SQL executor.""" from datetime import datetime from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.youtube_claim import config from feed_ingestion.flows.youtube_claim import snowflake_executor from tests.conftest import SubstringMatcher @pytest.fixture(params=[ (snowflake_executor.YoutubeClaimTheOrchard, 'theorchard') ]) 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_theorchard_properties(monkeypatch, sf_config_mock): """Test Youtube Claim specific executor properties.""" monkeypatch.setattr(connector, 'connect', MagicMock()) executor = snowflake_executor.YoutubeClaimTheOrchard(sf_config_mock) assert executor.feed_name == config.feed_name + '_theorchard' assert executor.storeid == config.storeid assert executor.feedid == config.feedid assert executor.licensor == 'theorchard' assert executor.staging_raw_table == config.snowflake_table_names.get( 'staging_raw_theorchard') def test_create_temp_staging_raw_table(mock_executor_and_params): """Test create_temp_staging_raw_table method.""" mock_executor, licensor = mock_executor_and_params staging_raw_table = config.snowflake_table_names['staging_raw_theorchard'] date_obj = datetime.today() account = 'TEST' temp_table_name = config.temp_table_name_template.format( staging_raw_table=staging_raw_table, date=date_obj, account=account) mock_executor.create_temp_staging_raw_table( temp_staging_raw_table=temp_table_name) 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.{staging_raw_table}_' '{date}_{account}'.format( staging_raw_table=staging_raw_table, date=date_obj.strftime('%Y%m%d'), account=account)] ), params={}) def test_load_temp_staging_raw_table( mock_aws, mock_executor_and_params): """Test load_temp_staging_raw_table method.""" mock_executor, licensor = mock_executor_and_params mock_executor.load_temp_staging_raw_table( 'load_temp_staging_raw_table', 's3://somepath' ) assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.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' }, ) def test_clean_staging_raw_table(mock_executor_and_params): """Test clean_staging_raw_table method.""" mock_executor, licensor = mock_executor_and_params mock_executor.clean_staging_raw_table( 'staging_raw_youtube_claim', '2017-01-01') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=['test_db.test_schema.staging_raw_youtube_claim']), params={'date': '2017-01-01', 'licensor': licensor}) def test_load_staging_raw_table(mock_executor_and_params): """Test load_staging_raw_table method.""" mock_executor, licensor = mock_executor_and_params mock_executor.load_staging_raw_table( temp_staging_raw_table='temp_table', staging_raw_table='staging_raw_table', date='2017-11-16', filename='filename', filesize=111, processed_datetime='2017-01-01' ) 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_table', 'FROM test_db.test_schema.temp_table']), params={'download_date': '2017-11-16', 'filename': 'filename', 'processed_datetime': '2017-01-01', 'filesize': 111, 'licensor': licensor}) def test_load_staging_fact_table(mock_executor_and_params): """Test load_staging_fact_table method.""" mock_executor, licensor = mock_executor_and_params mock_executor.load_staging_fact_table('2017-11-16') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.' f'staging_fact_analytics_youtube_claim_{licensor}_20171116', 'test_db.test_schema.staging_raw_youtube_claim']), params={'reportdate': '2017-11-16', 'storeid': 453, 'feedid': 14}) def test_load_fact_error_data(mock_executor_and_params): """Test load_fact_error_data method.""" mock_executor, licensor = mock_executor_and_params mock_executor.load_fact_error_data('2017-11-16') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.fact_analytics_error', 'test_db.test_schema.staging_raw_youtube_claim', 'test_db.test_schema.' f'staging_fact_analytics_youtube_claim_{licensor}_20171116']), params={'reportdate': '2017-11-16', 'storeid': 453, 'feedid': 14, 'licensor': licensor}) def test_is_assets_available(mock_executor_and_params): """Test is_assets_available_method.""" mock_executor, licensor = mock_executor_and_params mock_executor.is_assets_available() assert mock_executor.fetchone.call_count == 1 mock_executor.fetchone.assert_any_call( SubstringMatcher( containing=[ 'SELECT COUNT(*) FROM', 'test_db.test_schema.staging_raw_youtube_asset_report']), params={'licensor': licensor}) def test_update_dim_claim(mock_executor_and_params): """Test update_dim_claim method.""" mock_executor, licensor = mock_executor_and_params mock_executor.update_dim_claim('2020-01-01') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=['test_db.test_schema.dim_claim']), params={'download_date': '2020-01-01', 'licensor': licensor})