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)
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_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_async_do_actions(self): actions = [{ 'name': 'action1' }, { 'name': 'action2', 'params': { 'param1': 'foo', 'param2': 10 } }] defined_actions = BaseActions() defined_actions.action1 = AsyncMock() defined_actions.action2 = AsyncMock() asyncio.get_event_loop().run_until_complete( engine.async_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_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')