Esempio n. 1
0
def test_single_action():
    """Test a single action is executed."""

    action = TaskAction(
        'increment_call_count',
        name='action1'
    )

    partial = action.prepare(42, build_context=BuildContext())
    partial.delay().get()

    assert increment_call_count.call_count == 1
Esempio n. 2
0
def test_action_with_single_default_pre_hook():
    """Test executing an action having a pre hook."""
    hook = ActionHook(
        'increment_call_count',
    )
    action = TaskAction(
        'increment_call_count',
        name='action1',
        hooks=[hook]
    )

    partial = action.prepare(42, build_context=BuildContext())
    partial.delay().get()

    assert increment_call_count.call_count == 2
Esempio n. 3
0
    def test_simple_action_from_dict(self):
        """Test that TaskAction.from_dict() with a simple action."""
        @action(called=False, name='test_task_simple')
        def test_simple(self, source):
            self.called = True
            return 42

        dct = {
            "name": "some_action",
            "task": "test_task_simple",
        }
        task_action = TaskAction(dct['task'], name=dct['name'])
        partial = task_action.prepare(None, BuildContext())
        ret = partial.delay().get()

        assert task_action.task.called
        assert ret.results['some_action'] == 42
Esempio n. 4
0
def test_action_with_single_default_error_hook():
    """Test executing an action having an error hook."""
    hook = ActionHook(
        'increment_call_count',
        event='error'
    )
    action = TaskAction(
        'err',
        hooks=[hook]
    )

    partial = action.prepare(42, build_context=BuildContext())

    with pytest.raises(ValueError):
        partial.delay().get()

    assert increment_call_count.call_count == 1
Esempio n. 5
0
    def test_callback_action_from_dict_with_children(self):
        """Test that TaskAction.from_dict() with a simple action.
        TESTING NOTES: here we are capturing passed args as instance vars,
        so we can make assertions about the behaviro after the fact.
        """
        @action(called=False, verify_kwargs={})
        def test_task_false_with_children(self, some_attribute=None):
            self.called = True
            self.verify_kwargs['some_attribute'] = some_attribute

        @action(called=False)
        def test_task_errback_with_children(self):
            self.called = True

        @action(called=False)
        def test_task_callback_with_children(self):
            self.called = True


        dct = {
            "task": "test_task_false_with_children",
            "kwargs": {
                'some_attribute': 42,
            },
            "children": [
                {
                    "task": "test_task_errback_with_children",
                    "predicate": "not parent"
                },
                {
                    "task": "test_task_callback_with_children",
                }
            ]
        }
        task_action = TaskAction.from_dict(dct)

        source = 12345
        task_action.prepare(BuildContext(source))
        ret = task_action.delay().get()

        # assert that action was called properly
        assert 'some_attribute' in task_action.task.verify_kwargs
        assert task_action.task.verify_kwargs['some_attribute'] == 42

        # assert that action was called properly;
        # it should not have a parent, and the source should
        # be set.
        assert task_action.task.called

        # assert that failure handler was called
        assert task_action.children[0][0].task.called

        # assert that the success handler was not called
        assert not task_action.children[1][0].task.called

        # assert that the initial action's return value was preserved
        # (i.e. the failure handler's return value is not used)
        assert not ret.last
Esempio n. 6
0
    def test_callback_action_from_dict(self):
        """Test that TaskAction.from_dict() with a simple action.
        TESTING NOTES: here we are capturing passed args as instance vars,
        so we can make assertions about the behaviro after the fact.
        """
        @action(called=False, verify_kwargs={})
        def test_task_false(self, source, some_attribute=None):
            self.called = True
            self.verify_kwargs['some_attribute'] = some_attribute
            return False

        @action(called=False)
        def test_task_errback(self, source):
            self.called = True
            return 'BLAH'

        @action(called=False)
        def test_task_callback(self, source):
            self.called = True


        task_action = TaskAction('test_task_false', some_attribute=42)

        source = 12345
        partial = task_action.prepare(source, BuildContext())
        ret = partial.delay().get()

        # assert that action was called properly
        assert 'some_attribute' in task_action.task.verify_kwargs
        assert task_action.task.verify_kwargs['some_attribute'] == 42

        # assert that action was called properly;
        # it should not have a parent, and the source should
        # be set.
        assert task_action.task.called

        # assert that the initial action's return value was preserved
        # (i.e. the failure handler's return value is not used)
        assert isinstance(ret, BuildContext)
Esempio n. 7
0
def test_action_with_multiple_default_post_hooks():
    """Test executing an action having a post hook."""
    hooks = [
        ActionHook(
            'increment_call_count',
        ),
        ActionHook(
            'increment_call_count',
        ),
        ActionHook(
            'increment_call_count',
        )
    ]
    action = TaskAction(
        'increment_call_count',
        name='action1',
        hooks=hooks
    )

    partial = action.prepare(42, build_context=BuildContext())
    partial.delay().get()

    assert increment_call_count.call_count == 4
Esempio n. 8
0
def test_prepare():
    @action(name='task_test_prepare', called=False)
    def task_test_prepare(self, context, some_arg=None):
        """Task returns what was passed in positional arg 1"""
        self.called = True
        return some_arg

    dct = {
        "name": "task_test_from_dict_action",
        "task": "task_test_prepare",
    }
    task_action = TaskAction.from_dict(dct)

    assert not task_action.partial
    task_action.prepare(build_context=None)
    assert task_action.partial

    assert not task_test_prepare.called
    ret = task_action.delay(some_arg=42).get()

    assert task_test_prepare.called
    assert ret == 42