"""Unit tests for Amazon Prime Marketshare Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.amazon_prime_marketshare. \ snowflake_executor import AmazonPrimeMKTSFExecutor 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 = AmazonPrimeMKTSFExecutor(sf_config_mock) aws_params = { 'aws_key_id': 'test_id', 'aws_secret_key': 'test_secret', 'aws_token': 'test_token'} with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock, patch.object(executor, 'get_aws_params', return_value=aws_params): yield executor def test_create_temp_staging_raw_table(mock_executor): """Test create_temp_staging_raw_table method.""" mock_executor.create_temp_staging_raw_table( 'temp_staging_raw_table', date_for_sqlloader='2018-05-01') assert mock_executor.ex_mock.call_count == 1 mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema.temp_staging_raw_table', 'CREATE OR REPLACE TRANSIENT TABLE']), params={}) def test_load_temp_staging_raw_table(mock_executor, aws_config_mock): """Test load_temp_staging_raw_table method.""" mock_executor.load_temp_staging_raw_table( temp_staging_raw_table='temp_staging_raw_table', aws=aws_config_mock, key_dir='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.temp_staging_raw_table']), params={ 'aws_key_id': 'test_id', 'aws_secret_key': 'test_secret', 'aws_token': 'test_token', 's3_path': 's3://somepath'}) def test_clean_staging_raw_table(mock_executor): """Test clean_staging_raw_table method.""" mock_executor.clean_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=[ 'DELETE FROM', 'test_db.test_schema.staging_raw_table', 'WHERE date =']), params={'date': '2018-05-01'}) 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.temp_staging_raw_table']), params={'date': '2018-05-01', 'storeid': 187}) def test_load_marketshare_data(mock_executor): """Test load_marketshare_data method.""" mock_executor.load_marketshare_data('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.fact_market_share']), params={'date': '2018-05-01', 'storeid': 187})