"""Unit tests for TikTok Weekly SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.tiktok_weekly import config from feed_ingestion.flows.tiktok_weekly.snowflake_executor import TikTokWeekly 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 = TikTokWeekly(sf_config_mock) with patch.object(executor, 'execute', wraps=executor.execute) as \ executor.ex_mock: executor.fetchall = MagicMock() yield executor def test_create_temp_staging_raw_table(mock_executor): """Test create_temp_staging_raw_table method.""" for report in config.reports: kwargs = {'report': report} mock_executor.create_temp_staging_raw_table( 'temp_table', **kwargs) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=['test_db.test_schema', 'temp_table']), params={}) def test_load_temp_staging_raw_table(mock_aws, mock_executor): """Test load_temp_staging_raw_table method.""" for report in config.reports: kwargs = {'report': report, 'error_limit': 10} mock_executor.load_temp_staging_raw_table( 'temp_table', None, 's3://somepath', **kwargs) mock_executor.fetchall.assert_any_call( SubstringMatcher( containing=['COPY INTO', 'test_db.test_schema', 'temp_table']), params={'aws_key_id': 'FOOBARKEY', 'aws_secret_key': 'FOOBARSECRET', 'aws_token': 'FOOBARTOKEN', 's3_path': 's3://somepath'}) def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" for report in config.reports: kwargs = {'report': report} mock_executor.load_staging_raw_table( 'temp_staging_raw_table', 'staging_raw_table', '2017-11-16', **kwargs) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'test_db.test_schema', 'temp_staging_raw_table', 'INSERT INTO test_db.test_schema.staging_raw_table']), params={'download_date': '2017-11-16'})