"""Unit tests for TikTok and Douyin SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows.tiktok import config from feed_ingestion.flows.tiktok.snowflake_executor import TikTokTheOrchard from tests.conftest import SubstringMatcher CONSUMER_DB = config.consumer_sf['db'] CONSUMER_SCHEMA = config.consumer_sf['schema'] # Temp staging raw tables live in the producer db/schema (sf_config_mock). PRODUCER_DB = 'test_db' PRODUCER_SCHEMA = 'test_schema' @pytest.fixture def mock_executor(sf_config_mock, monkeypatch): """Yield executor context.""" connect_mock = MagicMock() monkeypatch.setattr(connector, 'connect', connect_mock) executor = TikTokTheOrchard(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, 'download_date': '2017-11-16'} mock_executor.create_temp_staging_raw_table( 'temp_table', **kwargs) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ f'{PRODUCER_DB}.{PRODUCER_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, 'download_date': '2017-11-16' } mock_executor.load_temp_staging_raw_table( 'temp_table', None, 's3://somepath', **kwargs ) mock_executor.fetchall.assert_any_call( SubstringMatcher( containing=[ 'COPY INTO', f'{PRODUCER_DB}.{PRODUCER_SCHEMA}', 'temp_table' ] ), params=dict( aws_key_id='FOOBARKEY', aws_secret_key='FOOBARSECRET', aws_token='FOOBARTOKEN', s3_path='s3://somepath', ) ) def test_drop_table_uses_producer_db_schema(mock_executor): """Test drop_table uses producer db and schema.""" mock_executor.drop_table('temp_table') mock_executor.ex_mock.assert_called_once_with( SubstringMatcher( containing=[ 'DROP TABLE IF EXISTS', f'{PRODUCER_DB}.{PRODUCER_SCHEMA}', 'temp_table', ])) def test_load_staging_raw_table(mock_executor): """Test load_staging_raw_table method.""" for report in config.reports: kwargs = {'report': report, 'download_date': '2017-11-16'} 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=[ f'FROM {PRODUCER_DB}.{PRODUCER_SCHEMA}' '.temp_staging_raw_table', f'INSERT INTO {CONSUMER_DB}.{CONSUMER_SCHEMA}' '.staging_raw_table']), params={'download_date': '2017-11-16', 'licensor': 'theorchard'})