"""Unit tests for tasks of Seated Workflow.""" from datetime import date, datetime, timedelta from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_ingestion.flows.seated import config from feed_ingestion.flows.seated import tasks _date = '2024-01-01' _report_name = 'opt_ins' class TestBootstrap(object): """Test bootstrap.""" @pytest.fixture def expected_bootstrap_response(self): """Response for a 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'], 'staging_raw_table_errors': config.reports[_report_name][ 'staging_raw_table_errors'], 'is_set_overall_ingested': _date_dt + timedelta(days=1) < date.today(), 'pii_columns': config.reports[_report_name][ 'pii_columns'], } @patch('feed_ingestion.flows.seated.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.seated.tasks.SeatedSnowflakeExecutor') def test_load_staging_raw_table_reports_typical(mock_executor): make_activity = MagicMock() feed_name = 'feed1' date = '2024-01-01' report_name = 'opt_ins' s3_dir_path = 's3://bucket/path' staging_raw_table_name = 'staging_table' staging_raw_errors_table_name = 'staging_table_errors' file_names = {'files': ['file1.csv', 'file2.csv']} snowflake_pattern = '.*_2024-01-01_.*_v1\\.0\\.csv$' pii_columns = ['FIRST_NAME', 'LAST_NAME', 'EMAIL', 'PHONE_NUMBER'] 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, staging_raw_errors_table_name, file_names, snowflake_pattern, pii_columns) 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', pii_columns=['FIRST_NAME', 'LAST_NAME', 'EMAIL', 'PHONE_NUMBER']) mock_exec.create_stage.assert_called_once_with( s3_dir_path=s3_dir_path, stage_name='stage_seated_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_seated_20240101') mock_exec.clean_staging_raw_table.assert_called_once_with( staging_raw_table=staging_raw_table_name, staging_raw_errors_table=staging_raw_errors_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, staging_raw_errors_table=staging_raw_errors_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_seated_20240101')