"""Unit tests for tasks of Ticketek Workflow.""" from datetime import datetime from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_ingestion.flows.ticketek import config from feed_ingestion.flows.ticketek import tasks _date = '2024-01-01' _report_name = 'event_booking' class TestBootstrap(object): """Test bootstrap.""" @pytest.fixture def expected_bootstrap_response(self): """Response for bootstrap task.""" _date_dt = datetime.strptime(_date, '%Y-%m-%d').date() filename = config.reports[_report_name]['filename_template'].format( date=_date_dt ) file_pattern = rf'.*\/{filename}' return { 'feed_name': '_'.join([config.feed_name, _report_name]), 'date': _date, 'report_name': _report_name, 'secrets_path': config.secrets_path, 'drop_bucket': config.drop_bucket, 'drop_path': config.drop_path, 's3_dir_path': f's3://{config.drop_bucket}/{config.drop_path}', 'file_pattern': file_pattern, 'snowflake_pattern': filename, 'staging_raw_table': config.reports[_report_name][ 'staging_raw_table'], 'is_set_overall_ingested': _date != datetime.now().strftime('%Y-%m-%d') } @patch('feed_ingestion.flows.ticketek.tasks.garcon_feed_status') def test_bootstrap(self, feed_status_mock, expected_bootstrap_response): """Test bootstrap task.""" context = { 'activity': MagicMock(), 'date': _date, 'report': _report_name, 'reload': None } result = tasks.bootstrap(**context) assert result == expected_bootstrap_response @patch('feed_ingestion.flows.ticketek.tasks.TicketekSnowflakeExecutor') def test_load_staging_raw_table_reports_typical(mock_executor): make_activity = MagicMock() feed_name = 'feed1' date = '2024-01-01' report_name = 'event_booking' s3_dir_path = 's3://bucket/path' staging_raw_table_name = 'staging_table' file_names = {'files': ['file1.csv', 'file2.csv']} snowflake_pattern = '.*Ticketek_20240101.*\\.gz$' mock_exec = mock_executor.return_value.__enter__.return_value tasks.load_staging_raw_table_reports( make_activity, feed_name, date, report_name, s3_dir_path, staging_raw_table_name, file_names, snowflake_pattern) mock_exec.create_temp_staging_raw_table.assert_called_once_with( report_name=report_name, temp_staging_raw_table='temp_staging_table_20240101') mock_exec.assign_masking_on_temp_staging_raw_table.assert_called_once_with( temp_staging_raw_table='temp_staging_table_20240101') mock_exec.create_stage.assert_called_once_with( s3_dir_path=s3_dir_path, stage_name='stage_ticketek_20240101') mock_exec.load_temp_staging_raw_table.assert_called_once_with( temp_staging_raw_table='temp_staging_table_20240101', file_name=snowflake_pattern, stage_name='stage_ticketek_20240101') mock_exec.clean_staging_raw_table.assert_called_once_with( staging_raw_table=staging_raw_table_name, file_name=snowflake_pattern, date=date) mock_exec.load_staging_raw_table.assert_called_once_with( staging_raw_table=staging_raw_table_name, temp_staging_raw_table='temp_staging_table_20240101', report_name=report_name, date=date, file_name=snowflake_pattern) mock_exec.drop_table.assert_called_once_with( table='temp_staging_table_20240101') mock_exec.drop_stage.assert_called_once_with( stage_name='stage_ticketek_20240101')