"""Unit tests for iTunes Tickets Snowflake SQL executor.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from snowflake import connector from feed_ingestion.flows import ITunesTicketsSF from feed_ingestion.flows.itunes_tickets import config 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 = ITunesTicketsSF(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.""" for table_type in config.table_types: table_name = 'temp_itunes_{table_type}'.format(table_type=table_type) 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 == len(config.table_types) def test_load_temp_staging_raw_table(mock_aws, mock_executor, aws_config_mock): """Test load_temp_staging_raw_table method.""" for table_type in config.table_types: table_name = 'temp_itunes_{}'. format(table_type) 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 == len(config.table_types) 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 == len(config.table_types) params = {'date': '2018-05-01'} mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'test_db.test_schema.itunes_tickets', 'FROM test_db.test_schema.{}'.format( 'temp_itunes_tickets_20180501' )]), params=params) mock_executor.ex_mock.assert_any_call( SubstringMatcher( containing=[ 'INSERT INTO', 'test_db.test_schema.itunes_ticket_notes', 'FROM test_db.test_schema.{}'.format( 'temp_itunes_ticket_notes_20180501' )]), params=params)