"""Unit tests for iTunes Hides Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows import ITunesHidesSF 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 = ITunesHidesSF(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_name = 'temp_staging_raw_test' mock_executor.create_temp_staging_raw_table(table_name) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.{}'.format(table_name), 'CREATE OR REPLACE TRANSIENT TABLE']), params={}) assert mock_executor.ex_mock.call_count == 1 def test_load_temp_staging_raw_table(mock_aws, mock_executor, aws_config_mock): """Test load_temp_staging_raw_table method.""" table_name = 'temp_itunes_hides' mock_executor.load_temp_staging_raw_table( temp_staging_raw_table=table_name, aws=aws_config_mock, key_dir='s3://somepath') mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=['COPY INTO', 'test_db.test_schema.{}'.format(table_name)]), params={ 'aws_key_id': 'FOOBARKEY', 'aws_secret_key': 'FOOBARSECRET', 'aws_token': 'FOOBARTOKEN', 's3_path': 's3://somepath'}) assert mock_executor.ex_mock.call_count == 1 def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" mock_executor.load_staging_raw_table( 'temp_staging_raw_table', 'staging_raw_table', '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.staging_raw_table', 'FROM test_db.test_schema.{}'.format( 'temp_staging_raw_table' )]), params={}) def test_clean_snapshot_table(mock_executor): """Test clean_snapshot_table method.""" mock_executor.clean_snapshot_table( 'test_itunes_hidden_master', '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.test_itunes_hidden_master', 'WHERE INGEST_DATE =' ]), params={'ingest_date': '2018-05-01'}) def test_load_snapshot_table(mock_executor): """Test load_snapshot_table method.""" mock_executor.load_snapshot_table( 'temp_staging_raw_table', 'test_itunes_hidden_master') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'test_db.test_schema.test_itunes_hidden_master', 'FROM test_db.test_schema.temp_staging_raw_table' ]), params={}) def test_remove_previous_snapshots(mock_executor): """Test remove_previous_snapshots method.""" mock_executor.remove_previous_snapshots( 'test_itunes_hidden_master', 5) assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'DELETE FROM', 'test_db.test_schema.test_itunes_hidden_master', 'QUALIFY DENSE_RANK() OVER(ORDER BY INGEST_DATE DESC)', '::INT' ]), params={'snapshots_amount': '5'})