Пример #1
0
def test_cancel_task_without_id():
    task_id = None
    request = CancelTaskRequest(task_id)

    assert request.task_id == task_id
    assert not request.is_valid()
    assert len(request.errors) == 1
    error = request.errors[0]
    assert error.parameter == 'task_id'
    assert error.message == 'is required'
Пример #2
0
def test_cancel_task_bad_request():
    task_id = 'abc'
    request = CancelTaskRequest(task_id)

    assert request.task_id == task_id
    assert not request.is_valid()
    assert len(request.errors) == 1
    error = request.errors[0]
    assert error.parameter == 'task_id'
    assert error.message == 'expected int, got str(abc)'
Пример #3
0
def cancel_task(id):
    request = CancelTaskRequest(id)
    action = CancelTaskAction(tasks_repository,
                              status_repo=task_statuses_repository)
    action.add_before_execution_hook(authorization_manager.authorize)
    response = action.execute(request)
    status = get_status(response)

    return json_response(response.value, TaskEncoder, status)
Пример #4
0
def test_cancel_task_action():
    task_repo = mock.Mock()
    status_repo = mock.Mock()
    action = CancelTaskAction(task_repo=task_repo, status_repo=status_repo)
    request = CancelTaskRequest(task_id=1)
    response = action.execute(request)

    assert bool(response) is True
    assert task_repo.update.called
Пример #5
0
def test_cancel_task_action_handles_bad_request():
    task_repo = mock.Mock()
    status_repo = mock.Mock()
    action = CancelTaskAction(task_repo=task_repo, status_repo=status_repo)
    request = CancelTaskRequest(task_id=None)
    response = action.execute(request)

    assert bool(response) is False
    assert not task_repo.update.called
    assert response.value == {
        'type': ResponseFailure.PARAMETER_ERROR,
        'message': 'task_id: is required'
    }
Пример #6
0
def test_cancel_task_action_handles_generic_error():
    error_message = 'Error!!!'
    task_repo = mock.Mock()
    task_repo.update.side_effect = Exception(error_message)
    status_repo = mock.Mock()
    action = CancelTaskAction(task_repo=task_repo, status_repo=status_repo)
    request = CancelTaskRequest(task_id=1)
    response = action.execute(request)

    assert bool(response) is False
    assert task_repo.update.called
    assert response.value == {
        'type': ResponseFailure.SYSTEM_ERROR,
        'message': 'Exception: {}'.format(error_message)
    }
Пример #7
0
def test_cancel_task_action_with_hooks():
    task_repo = mock.Mock()
    status_repo = mock.Mock()
    action = CancelTaskAction(task_repo=task_repo, status_repo=status_repo)
    request = CancelTaskRequest(task_id=1)

    before = mock.MagicMock()
    after = mock.MagicMock()

    action.add_before_execution_hook(before)
    action.add_after_execution_hook(after)

    response = action.execute(request)

    assert before.called
    assert after.called
    assert bool(response) is True
    assert task_repo.update.called
Пример #8
0
def test_cancel_task_request():
    task_id = 1
    request = CancelTaskRequest(task_id)

    assert request.task_id == task_id
    assert request.is_valid()