"""CLI tests.""" from unittest.mock import patch import pytest from dim_refresh_etl import cli @pytest.fixture def import_flow_class_mock(mocker): """Mock flow class import function.""" return mocker.patch( 'dim_refresh_etl.cli.import_utils.import_workflow_class') @pytest.fixture def sys_exit_mock(mocker): """Mock sys.exit response.""" sys_exit = mocker.patch('dim_refresh_etl.cli.sys.exit') sys_exit.return_value = 0 @pytest.fixture def cli_start_mock(mocker): """Mock start function in cli.""" return mocker.patch('dim_refresh_etl.cli.start') @pytest.fixture def mock_flow(import_flow_class_mock): """Mock imported flow object.""" return import_flow_class_mock.return_value.return_value @pytest.mark.parametrize('flow_name', ['refresh', 'geocoding']) def test_import_flow( flow_name, cli_start_mock, sys_exit_mock, import_flow_class_mock): """Test correct import flow class.""" cli.main([flow_name, 'exec', 'artist']) import_flow_class_mock.assert_called_with(flow_name) @patch('dim_refresh_etl.cli.activity.ActivityWorker') def test_incorrect_flow(mock_activity_worker, mock_flow): """Test behaviour.""" with pytest.raises(SystemExit): cli.main(['unknown_flow']) def test_exec(cli_start_mock, sys_exit_mock, mock_flow): """Test refresh exec.""" cli.main(['refresh', 'exec', 'artist']) cli_start_mock.assert_called_with(mock_flow, {'dim_type': 'artist'}) def test_exec_wait(cli_start_mock, sys_exit_mock, mock_flow): """Test refresh exec with --wait-until-complete option.""" cli.main(['refresh', 'exec', 'artist', '--wait-until-complete']) cli_start_mock.assert_called_with( mock_flow, {'dim_type': 'artist', 'wait_until_complete': True}) @patch('dim_refresh_etl.cli.activity.ActivityWorker') def test_activity_worker(mock_activity_worker, mock_flow): """Test refresh worker.""" cli.main(['refresh', 'worker']) mock_activity_worker.assert_called_with(mock_flow, None) mock_activity_worker.return_value.run.assert_called_with() @patch('dim_refresh_etl.cli.decider.DeciderWorker') @patch('dim_refresh_etl.cli.run_decider') def test_run_decider( mock_run_decider, mock_decider_worker, mock_flow): """Test refresh decider.""" cli.main(['refresh', 'decider']) mock_decider_worker.assert_called_with(mock_flow) mock_run_decider.assert_called_with(mock_decider_worker.return_value)