"""Unit tests for Garcon CLI for backload_scheduler.py.""" from unittest import mock from unittest.mock import patch import pytest from feed_ingestion.bin import backload_scheduler @patch('feed_ingestion.bin.backload_scheduler.boto3') @patch('feed_ingestion.bin.backload_scheduler.BackloadTasksExecutor') @pytest.mark.parametrize('argv, is_error, result', [ (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-01-01'], False, 1), (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-01-03'], False, 3), (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-02-01'], False, 32), (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-02-31'], True, ValueError), (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', ], True, ValueError), ]) def test_add_tasks_period(mock_backload_executor, mock_boto3, argv, is_error, result): """Test different period arguments.""" mock_insert_backload_tasks = mock_backload_executor.return_value. \ __enter__.return_value.insert_backload_tasks mock_insert_backload_tasks.reset_mock() with mock.patch('sys.argv', [''] + argv): if is_error: with pytest.raises(result): backload_scheduler.main() else: backload_scheduler.main() assert mock_insert_backload_tasks.call_count == result @patch('feed_ingestion.bin.backload_scheduler.boto3') @patch('feed_ingestion.bin.backload_scheduler.BackloadTasksExecutor') @pytest.mark.parametrize('argv, context', [ (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-01-01', '--context', '{"foo": "BAR", "licensor": "sme"}'], '{"foo": "BAR", "licensor": "sme", "context_date": "2020-01-01"}'), (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-01-01'], '{"licensor": "sme", "context_date": "2020-01-01"}'), (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-01-01', '--reload', 'full'], '{"reload": "True", "licensor": "sme", "context_date": "2020-01-01"}'), (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-01-01', '--reload', 'None'], '{"licensor": "sme", "context_date": "2020-01-01"}'), (['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-01-01', '--reload', 'soft', '--context', '{"report_name": "prime"}'], '{"report_name": "prime", "soft_reload": "True", "licensor": "sme", ' '"context_date": "2020-01-01"}'), ]) def test_context(mock_backload_executor, mock_boto3, argv, context): """Test generated context value.""" mock_insert_backload_tasks = mock_backload_executor.return_value. \ __enter__.return_value.insert_backload_tasks mock_insert_backload_tasks.reset_mock() with mock.patch('sys.argv', [''] + argv): backload_scheduler.main() assert mock_insert_backload_tasks.called assert mock_insert_backload_tasks.call_args[1]['context'] == context @patch('feed_ingestion.bin.backload_scheduler.BackloadTasksExecutor') def test_cancel_tasks(mock_backload_executor): """Test cancel task arguments.""" mock_delete_backload_tasks = mock_backload_executor.return_value. \ __enter__.return_value.delete_backload_tasks argv = ['--licensor', 'sme', '--flow', 'amazon_music', '--period_start', '2020-01-01', '--period_end', '2020-01-01', '--cancel'] with mock.patch('sys.argv', [''] + argv): backload_scheduler.main() assert mock_delete_backload_tasks.called expected_args = { 'flow': 'amazon_music', 'licensor': 'sme', 'period_end': '2020-01-01', 'period_start': '2020-01-01' } assert mock_delete_backload_tasks.call_args[1] == expected_args @pytest.mark.parametrize('status_item, status_item_expected', [ ({ 'date': '2020-05-19', 'completed_tasks_status': 'set_overall_status_DOWNLOADED,staging_raw_table_tasks,set_overall_status_POPULATED_RAW_TABLE,load_aggregated_table,update_dim_tables,create_staging_fact,load_staging_fact,load_aggregated_skips_and_saves,load_fact_data,set_overall_status_INGESTED', # noqa:E501 'feed_name': 'amazon_music_sme_prime', 'status': 'INGESTED', }, { 'date': '2020-05-19', 'completed_tasks_status': 'set_overall_status_DOWNLOADED,staging_raw_table_tasks,set_overall_status_POPULATED_RAW_TABLE,load_aggregated_table,update_dim_tables', # noqa:E501 'feed_name': 'amazon_music_sme_prime', 'status': 'POPULATED_RAW_TABLE', }), ({ 'date': '2020-05-19', 'completed_tasks_status': 'set_overall_status_DOWNLOADED,staging_raw_table_tasks,set_overall_status_POPULATED_RAW_TABLE,load_aggregated_table,update_dim_tables,create_staging_fact,load_staging_fact,load_aggregated_skips_and_saves,load_fact_data,set_overall_status_INGESTED', # noqa:E501 'feed_name': 'some_other_feed_name', 'status': 'INGESTED', }, { 'date': '2020-05-19', 'completed_tasks_status': 'set_overall_status_DOWNLOADED,staging_raw_table_tasks,set_overall_status_POPULATED_RAW_TABLE', # noqa:E501 'feed_name': 'some_other_feed_name', 'status': 'POPULATED_RAW_TABLE', }), ({ 'date': '2020-05-19', 'completed_tasks_status': 'grab_drop_files,staging_raw_table_tasks,set_overall_status_POPULATED_RAW_TABLE,update_dim_tables,sns_publish_message,create_staging_fact,load_staging_fact,load_fact_data,set_overall_status_INGESTED', # noqa:E501 'feed_name': 'spotify_sme_streams', 'status': 'INGESTED', }, { 'date': '2020-05-19', 'completed_tasks_status': 'grab_drop_files,staging_raw_table_tasks,set_overall_status_POPULATED_RAW_TABLE,update_dim_tables,sns_publish_message', # noqa:E501 'feed_name': 'spotify_sme_streams', 'status': 'POPULATED_RAW_TABLE', }), ({ 'date': '2020-05-19', 'completed_tasks_status': '', 'feed_name': 'spotify', 'status': 'INGESTED', }, { 'date': '2020-05-19', 'completed_tasks_status': '', 'feed_name': 'spotify', 'status': 'POPULATED_RAW_TABLE', }), ]) def test_update_status_for_soft_reload(status_item, status_item_expected): """Test update_status_for_soft_reload.""" assert backload_scheduler.update_status_for_soft_reload( status_item ) == status_item_expected