From 7c8aa5647b85c4c2691e072350aa3639273b1229 Mon Sep 17 00:00:00 2001 From: Piotr Kaznowski Date: Fri, 29 Jan 2021 23:00:19 +0100 Subject: [PATCH] #5 adds warning instead of exception raising to pa schedule set. by: Piotr --- cli/schedule.py | 7 +++++-- tests/test_cli_schedule.py | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cli/schedule.py b/cli/schedule.py index 2737586..ebf938b 100644 --- a/cli/schedule.py +++ b/cli/schedule.py @@ -59,12 +59,15 @@ def set( `pa schedule update` or deleted with `pa schedule delete` commands.""" - get_logger(set_info=True) + logger = get_logger(set_info=True) task = Task.to_be_created( command=command, hour=hour, minute=minute, disabled=disabled ) - task.create_schedule() + try: + task.create_schedule() + except Exception as e: + logger.warning(snakesay(str(e))) delete_app = typer.Typer() diff --git a/tests/test_cli_schedule.py b/tests/test_cli_schedule.py index bd1bfa6..feb109f 100644 --- a/tests/test_cli_schedule.py +++ b/tests/test_cli_schedule.py @@ -77,6 +77,21 @@ def test_validates_hours(self): assert "Invalid value" in result.stdout assert "66 is not in the valid range of 0 to 23" in result.stdout + def test_logs_warning_when_create_schedule_raises(self, mocker): + mock_logger = mocker.patch("cli.schedule.get_logger").return_value + mock_snakesay = mocker.patch("cli.schedule.snakesay") + mock_task_to_be_created = mocker.patch("cli.schedule.Task.to_be_created") + error_msg = ( + "POST to set new task via API failed, got : " + '{"detail":"You have reached your maximum number of scheduled tasks"}' + ) + mock_task_to_be_created.return_value.create_schedule.side_effect = Exception(error_msg) + + runner.invoke(app, ["set", "--command", "echo foo", "--minute", "13"]) + + assert mock_snakesay.call_args == call(error_msg) + assert mock_logger.warning.call_args == call(mock_snakesay.return_value) + @pytest.mark.clischeduledeleteall class TestDeleteAllTasks: @@ -240,6 +255,7 @@ def test_complains_when_no_id_provided(self): result = runner.invoke(app, ["get", "--command"]) assert "Missing argument 'id'" in result.stdout + @pytest.mark.clischedulelist class TestList: def test_logs_table_with_correct_headers_and_values(self, mocker, task_list):