Пример #1
0
    def test_do_actions_with_injected_parameters(self):
        function_params_mock = MagicMock()
        function_params_mock.keywords = True
        with patch('inspect.getargspec', return_value=function_params_mock):
            rule_actions = [
                {
                    'name': 'action1'
                },
                {
                    'name': 'action2',
                    'params': {'param1': 'foo', 'param2': 10}
                }
            ]

            rule = {
                'conditions': {

                },
                'actions': rule_actions
            }

            defined_actions = BaseActions()
            defined_actions.action1 = MagicMock()
            defined_actions.action2 = MagicMock()
            defined_actions.action2.params = {
                'param1': fields.FIELD_TEXT,
                'param2': fields.FIELD_NUMERIC
            }

            payload = [(True, 'condition_name', 'operator_name', 'condition_value')]

            engine.do_actions(rule_actions, defined_actions, payload, rule)

            defined_actions.action1.assert_called_once_with(conditions=payload, rule=rule)
            defined_actions.action2.assert_called_once_with(param1='foo', param2=10, conditions=payload, rule=rule)
Пример #2
0
    def test_do_actions_checks_invalid_params(self):
        @rule_action(params=[{'name': 'param1', 'fieldType': FIELD_NUMERIC}])
        def action1(self, param1):
            pass
        actions = [{'name': 'action1', 'params': {'param1': 1, 'param2': 2}}]
        defined_actions = BaseActions()
        defined_actions.action1 = action1

        err_string = 'Invalid parameters param2 for action action1'
        with self.assertRaisesRegexp(AssertionError, err_string):
            engine.do_actions(actions, defined_actions)
Пример #3
0
    def test_do_actions(self):
        actions = [ {'name': 'action1'},
                    {'name': 'action2',
                     'params': {'param1': 'foo', 'param2': 10}}]
        defined_actions = BaseActions()
        defined_actions.action1 = MagicMock()
        defined_actions.action2 = MagicMock()

        engine.do_actions(actions, defined_actions)

        defined_actions.action1.assert_called_once_with()
        defined_actions.action2.assert_called_once_with(param1='foo', param2=10)
Пример #4
0
    def test_do_actions(self):
        actions = [ {'name': 'action1'},
                    {'name': 'action2',
                     'params': {'param1': 'foo', 'param2': 10}}]
        defined_actions = BaseActions()
        defined_actions.action1 = MagicMock()
        defined_actions.action2 = MagicMock()

        engine.do_actions(actions, defined_actions)

        defined_actions.action1.assert_called_once_with()
        defined_actions.action2.assert_called_once_with(param1='foo', param2=10)
    def test_do_with_invalid_action(self):
        actions = [{'name': 'fakeone'}]
        err_string = "Action fakeone is not defined in class BaseActions"

        rule = {'conditions': {}, 'actions': {}}

        checked_conditions_results = [(True, 'condition_name', 'operator_name',
                                       'condition_value')]

        with self.assertRaisesRegexp(AssertionError, err_string):
            engine.do_actions(actions, BaseActions(),
                              checked_conditions_results, rule)
Пример #6
0
    def test_do_actions_with_override(self):
        actions = [{
            'name': 'action',
            'params': {
                'param': 'foo',
            }
        }]
        override_params = {'param': 'bar'}
        defined_actions = BaseActions()
        defined_actions.action = MagicMock()

        engine.do_actions(actions,
                          defined_actions,
                          override_params=override_params)
        defined_actions.action.assert_called_once_with(param='bar')
    def test_do_with_parameter_with_default_value(self):
        function_params_mock = MagicMock()
        function_params_mock.varkw = None
        with patch('business_rules.engine.getfullargspec',
                   return_value=function_params_mock):
            # param2 is not set in rule, but there is a default parameter for it in action which will be used instead
            rule_actions = [{
                'name': 'some_action',
                'params': {
                    'param1': 'foo'
                }
            }]

            rule = {'conditions': {}, 'actions': rule_actions}

            action_param_with_default_value = ActionParam(
                field_type=fields.FIELD_NUMERIC, default_value=42)

            action_mock = MagicMock()

            class SomeActions(BaseActions):
                @rule_action(
                    params={
                        'param1': FIELD_TEXT,
                        'param2': action_param_with_default_value
                    })
                def some_action(self, param1, param2):
                    return action_mock(param1=param1, param2=param2)

            defined_actions = SomeActions()

            defined_actions.action = MagicMock()
            defined_actions.action.params = {
                'param1': fields.FIELD_TEXT,
                'param2': action_param_with_default_value
            }

            payload = [(True, 'condition_name', 'operator_name',
                        'condition_value')]

            engine.do_actions(rule_actions, defined_actions, payload, rule)

            action_mock.assert_called_once_with(param1='foo', param2=42)
    def test_do_actions_with_injected_parameters(self):
        function_params_mock = MagicMock()
        function_params_mock.varkw = True
        with patch('business_rules.engine.getfullargspec',
                   return_value=function_params_mock):
            rule_actions = [{
                'name': 'action1'
            }, {
                'name': 'action2',
                'params': {
                    'param1': 'foo',
                    'param2': 10
                }
            }]

            rule = {'conditions': {}, 'actions': rule_actions}

            defined_actions = BaseActions()
            defined_actions.action1 = MagicMock()
            defined_actions.action1.params = []
            defined_actions.action2 = MagicMock()
            defined_actions.action2.params = [{
                'label': 'action2',
                'name': 'param1',
                'fieldType': fields.FIELD_TEXT,
                'defaultValue': None
            }, {
                'label': 'action2',
                'name': 'param2',
                'fieldType': fields.FIELD_NUMERIC,
                'defaultValue': None
            }]
            payload = [(True, 'condition_name', 'operator_name',
                        'condition_value')]

            engine.do_actions(rule_actions, defined_actions, payload, rule)

            defined_actions.action1.assert_called_once_with(conditions=payload,
                                                            rule=rule)
            defined_actions.action2.assert_called_once_with(param1='foo',
                                                            param2=10,
                                                            conditions=payload,
                                                            rule=rule)
    def test_do_actions(self):
        function_params_mock = MagicMock()
        function_params_mock.varkw = None
        with patch('business_rules.engine.getfullargspec',
                   return_value=function_params_mock):
            rule_actions = [{
                'name': 'action1'
            }, {
                'name': 'action2',
                'params': {
                    'param1': 'foo',
                    'param2': 10
                }
            }]

            rule = {'conditions': {}, 'actions': rule_actions}

            action1_mock = MagicMock()
            action2_mock = MagicMock()

            class SomeActions(BaseActions):
                @rule_action()
                def action1(self):
                    return action1_mock()

                @rule_action(params={
                    'param1': FIELD_TEXT,
                    "param2": FIELD_NUMERIC
                })
                def action2(self, param1, param2):
                    return action2_mock(param1=param1, param2=param2)

            defined_actions = SomeActions()

            payload = [(True, 'condition_name', 'operator_name',
                        'condition_value')]

            engine.do_actions(rule_actions, defined_actions, payload, rule)

            action1_mock.assert_called_once_with()
            action2_mock.assert_called_once_with(param1='foo', param2=10)
    def test_default_param_overrides_action_param(self):

        function_params_mock = MagicMock()
        function_params_mock.varkw = None
        with patch('business_rules.engine.getfullargspec',
                   return_value=function_params_mock):
            rule_actions = [{
                'name': 'some_action',
                'params': {
                    'param1': False
                }
            }]

            rule = {'conditions': {}, 'actions': rule_actions}

            action_param_with_default_value = ActionParam(
                field_type=fields.FIELD_TEXT, default_value='bar')

            action_mock = MagicMock()

            class SomeActions(BaseActions):
                @rule_action(
                    params={'param1': action_param_with_default_value})
                def some_action(self, param1):
                    return action_mock(param1=param1)

            defined_actions = SomeActions()

            defined_actions.action = MagicMock()
            defined_actions.action.params = {
                'param1': action_param_with_default_value,
            }

            payload = [(True, 'condition_name', 'operator_name',
                        'condition_value')]

            engine.do_actions(rule_actions, defined_actions, payload, rule)

            action_mock.assert_called_once_with(param1=False)
Пример #11
0
    def test_do_actions_with_returned_values(self):
        actions = [{
            'name': 'action1'
        }, {
            'name': 'action2',
            'params': {
                'param1': 'foo',
                'param2': 10
            }
        }, {
            'name': 'action3',
            'params': {
                'param1': 'baz'
            }
        }, {
            'name': 'action4',
            'params': {
                'param1': 'old'
            }
        }]
        defined_actions = BaseActions()
        defined_actions.action1 = MagicMock(return_value={'param3': 'bar'})
        defined_actions.action2 = MagicMock(return_value=[1, 2, 3])
        defined_actions.action3 = MagicMock(return_value={'param1': 'new'})
        defined_actions.action4 = MagicMock()

        engine.do_actions(actions, defined_actions)

        defined_actions.action1.assert_called_once_with()
        # action result of dict type gets merged into params
        defined_actions.action2.assert_called_once_with(param1='foo',
                                                        param2=10,
                                                        param3='bar')
        # action result of non-dict type doesn't get merged into params
        defined_actions.action3.assert_called_once_with(param1='baz')
        # action result overrides params of the following action
        defined_actions.action4.assert_called_once_with(param1='new')
Пример #12
0
 def test_do_with_invalid_action(self):
     actions = [{'name': 'fakeone'}]
     err_string = "Action fakeone is not defined in class BaseActions"
     with self.assertRaisesRegexp(AssertionError, err_string):
         engine.do_actions(actions, BaseActions())
Пример #13
0
 def test_do_with_invalid_action(self):
     actions = [{'name': 'fakeone'}]
     err_string = "Action fakeone is not defined in class BaseActions"
     with self.assertRaisesRegexp(AssertionError, err_string):
         engine.do_actions(actions, BaseActions())