def test_check_all_conditions_with_all_false(self, *args): conditions = {'all': [{'thing1': ''}, {'thing2': ''}]} variables = BaseVariables() result = engine.check_conditions_recursively(conditions, variables) self.assertEqual(result, False) engine.check_condition.assert_called_once_with({'thing1': ''}, variables)
def test_check_all_conditions_with_all_true(self, *args): conditions = {'all': [{'thing1': ''}, {'thing2': ''}]} variables = BaseVariables() result = engine.check_conditions_recursively(conditions, variables) self.assertEqual(result, True) # assert call count and most recent call are as expected self.assertEqual(engine.check_condition.call_count, 2) engine.check_condition.assert_called_with({'thing2': ''}, variables)
def test_run_that_doesnt_trigger_rule(self, *args): rule = {'conditions': 'blah', 'actions': 'blah2'} variables = BaseVariables() actions = BaseActions() result = engine.run(rule, variables, actions) self.assertEqual(result, False) engine.check_conditions_recursively.assert_called_once_with( rule['conditions'], variables) self.assertEqual(engine.do_actions.call_count, 0)
def test_run_all_stop_on_first(self, *args): rule1 = {'conditions': 'condition1', 'actions': 'action name 1'} rule2 = {'conditions': 'condition2', 'actions': 'action name 2'} variables = BaseVariables() actions = BaseActions() result = engine.run_all([rule1, rule2], variables, actions, stop_on_first_trigger=True) self.assertEqual(result, True) self.assertEqual(engine.run.call_count, 1) engine.run.assert_called_once_with(rule1, variables, actions)
def test_run_all_some_rule_triggered(self, *args): """ By default, does not stop on first triggered rule. Returns True if any rule was triggered, otherwise False """ rule1 = {'conditions': 'condition1', 'actions': 'action name 1'} rule2 = {'conditions': 'condition2', 'actions': 'action name 2'} variables = BaseVariables() actions = BaseActions() def return_action1(rule, *args, **kwargs): return rule['actions'] == 'action name 1' engine.run.side_effect = return_action1 result = engine.run_all([rule1, rule2], variables, actions) self.assertTrue(result) self.assertEqual(engine.run.call_count, 2) # switch order and try again engine.run.reset_mock() result = engine.run_all([rule2, rule1], variables, actions) self.assertTrue(result) self.assertEqual(engine.run.call_count, 2)
def test_nested_all_and_any(self, *args): conditions = { 'all': [{ 'any': [{ 'name': 1 }, { 'name': 2 }] }, { 'name': 3 }] } bv = BaseVariables() def side_effect(condition, _): return condition['name'] in [2, 3] engine.check_condition.side_effect = side_effect engine.check_conditions_recursively(conditions, bv) self.assertEqual(engine.check_condition.call_count, 3) engine.check_condition.assert_any_call({'name': 1}, bv) engine.check_condition.assert_any_call({'name': 2}, bv) engine.check_condition.assert_any_call({'name': 3}, bv)
def test_check_all_and_any_together(self): conditions = {'any': [], 'all': []} variables = BaseVariables() with self.assertRaises(AssertionError): engine.check_conditions_recursively(conditions, variables)
def test_check_any_condition_with_no_items_fails(self): with self.assertRaises(AssertionError): engine.check_conditions_recursively({'any': []}, BaseVariables())
def test_base_has_no_variables(self): self.assertEqual(len(BaseVariables.get_all_variables()), 0)