"""Unit tests for TikTok Fraudulent Streams Report tasks workflow.""" from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_ingestion.flows.tiktok_fraudulent_streams_report import tasks _date = '2023-01-01' @pytest.fixture def expected_bootstrap_response(): """Response for bootstrap task.""" return { 'feed_name': 'tiktok_fraudulent_streams_report', 'date': '2023-01-01', 'drop_path': 'ftp/data_feed/tiktok/fraud_activity', 'archive_path': 'tiktok_fraudulent_streams_report/archives/202301', 'source_file_name': 'TikTok_FraudulentUsage_report_202301.txt.zip', 'new_file_name': 'TikTok_FraudulentUsage_report_202301.txt.gz', 's3_archive_path': 's3://dev-cucumbers/tiktok_fraudulent_streams_report/' + 'archives/202301/', 'key_dir': 's3://dev-cucumbers/tiktok_fraudulent_streams_report/' + 'archives/202301/TikTok_FraudulentUsage_report_202301.txt.gz', 'secrets_path': 'swf-tiktok-fraudulent-streams-report', 'staging_raw_table': 'STAGING_RAW_TIKTOK_FRAUDULENT_USAGE_REPORT', 'temp_staging_raw_table': 'temp_staging_raw_tiktok_fraudulent_streams_report', 's3_archive_file_path': 's3://dev-cucumbers/tiktok_fraudulent_streams_report/archives/' + '202301/TikTok_FraudulentUsage_report_202301.txt.gz', 's3_drop_file_path': 's3://dev-feed-drop/ftp/data_feed/tiktok/fraud_activity/' + 'TikTok_FraudulentUsage_report_202301.txt.zip', 'reload': None, } def test_bootstrap(expected_bootstrap_response): """Test bootstrap task with last day of a week.""" result = tasks.bootstrap(MagicMock(), _date, reload=None) assert result == expected_bootstrap_response def test_bootstrap_context_date_pre_june(): """Test bootstrap task.""" # test with post-June 2023 date (filename change) result = tasks.bootstrap(MagicMock(), '2023-01-01', reload=False) assert result['new_file_name'] == \ 'TikTok_FraudulentUsage_report_202301.txt.gz' assert result['source_file_name'] == \ 'TikTok_FraudulentUsage_report_202301.txt.zip' def test_bootstrap_context_date_post_june(): """Test bootstrap task.""" # test with post-June 2023 date (filename change) result = tasks.bootstrap(MagicMock(), '2023-06-01', reload=False) assert result['new_file_name'] ==\ 'TikTok_UsageAdjustment_report_202306.txt.gz' assert result['source_file_name'] ==\ 'TikTok_UsageAdjustment_report_202306.txt.zip' @pytest.fixture def mock_task_status(): """Yield task status.""" task_status_path = 'feed_ingestion.tasks.task_status' with patch(task_status_path) as task_status: task_status.is_completed_task.return_value = False task_status.mark_completed_task = MagicMock() task_status.get_values = MagicMock() yield task_status @pytest.fixture def mock_set_overall_status(): """Yield overall status.""" overall_status_path = ( 'feed_ingestion.flows.tiktok_fraudulent_streams_report.tasks.' + 'garcon_feed_status.set_overall_status') with patch(overall_status_path) as overall_status: yield overall_status def test_set_status_to_ingested( mock_set_overall_status): """Test set_status_to_ingested.""" tasks.set_status_to_ingested( MagicMock(), 'feed_name', _date, 'TikTok_FraudulentUsage_report_202301.txt.gz') mock_set_overall_status.assert_called_with( _date, 'feed_name', 'INGESTED') @patch('feed_ingestion.flows.tiktok_fraudulent_streams_report.tasks.s3') def test_grab_drop_files_no_new_file(s3_mock, mock_task_status): """Test grab_drop_files.""" s3_mock.get_list_of_files_and_directories.return_value = [] tasks.grab_drop_files( MagicMock(), _date, 's3://drop_path', 's3://archive_path') s3_mock.get_list_of_files_and_directories.assert_called_with( 's3://drop_path') s3_mock.convert_zip_to_gzip_on_s3.assert_not_called() @patch('feed_ingestion.flows.tiktok_fraudulent_streams_report.tasks.s3') def test_grab_drop_files_new_file(s3_mock, mock_task_status): """Test grab_drop_files.""" activity_mock = MagicMock() s3_mock.get_list_of_files_and_directories.return_value = ['s3://drop_path'] tasks.grab_drop_files( activity_mock, _date, 's3://drop_path', 's3://archive_path') s3_mock.get_list_of_files_and_directories.assert_called_with( 's3://drop_path') s3_mock.convert_zip_to_gzip_on_s3.assert_called_with( activity_mock, zip_s3_path='s3://drop_path', gz_s3_path='s3://archive_path', local_temp_dir='./', extract_original_filename=True)