def test_check_any_condition_with_no_items_fails(self): conditions = {'any': []} variables = BaseVariables() rule = {'conditions': conditions, 'actions': []} with self.assertRaises(AssertionError): engine.check_conditions_recursively(conditions, variables, rule)
def test_nested_all_and_any(self, *args): conditions = { 'all': [{ 'any': [{ 'name': 1 }, { 'name': 2 }] }, { 'name': 3 }] } bv = BaseVariables() def side_effect(condition, _, **kwargs): condition_check = condition['name'] in [2, 3] return condition_check, {} 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, override_params=OVERRIDE_NONE) engine.check_condition.assert_any_call({'name': 2}, bv, override_params=OVERRIDE_NONE) engine.check_condition.assert_any_call({'name': 3}, bv, override_params=OVERRIDE_NONE)
def test_nested_all_and_any(self, *args): conditions = { 'all': [{ 'any': [{ 'name': 1 }, { 'name': 2 }] }, { 'name': 3 }] } rule = {'conditions': conditions, 'actions': {}} bv = BaseVariables() def side_effect(condition, _, rule): return ConditionResult(result=condition['name'] in [2, 3], name=condition['name'], operator='', value='', parameters='') engine.check_condition.side_effect = side_effect engine.check_conditions_recursively(conditions, bv, rule) self.assertEqual(engine.check_condition.call_count, 3) engine.check_condition.assert_any_call({'name': 1}, bv, rule) engine.check_condition.assert_any_call({'name': 2}, bv, rule) engine.check_condition.assert_any_call({'name': 3}, bv, rule)
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_nested_all_and_any(self, *args): conditions = {'all': [ {'any': [{'name': 1}, {'name': 2}]}, {'name': 3}]} bv = BaseVariables() def side_effect(condition, s, _): 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, None) engine.check_condition.assert_any_call({'name': 2}, bv, None) engine.check_condition.assert_any_call({'name': 3}, bv, None)
def test_case12(self): """ cond1: true or cond2: true => [cond1] """ conditions = { 'any': [ { 'name': 'true_variable', 'operator': 'is_true', 'value': '1' }, { 'name': 'true_variable', 'operator': 'is_true', 'value': '2' }, ] } variables = TrueVariables() rule = {'conditions': conditions, 'actions': []} result = engine.check_conditions_recursively(conditions, variables, rule) self.assertEqual(result, (True, [ ConditionResult(result=True, name='true_variable', operator='is_true', value='1', parameters={}), ]))
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, None)
def test_case5(self): """ cond1: false and (cond2: false or cond3: true) => [] """ conditions = { 'all': [{ 'name': 'true_variable', 'operator': 'is_false', 'value': '1' }, { 'any': [{ 'name': 'true_variable', 'operator': 'is_false', 'value': '2' }, { 'name': 'true_variable', 'operator': 'is_true', 'value': '3' }] }] } variables = TrueVariables() rule = {'conditions': conditions, 'actions': []} result = engine.check_conditions_recursively(conditions, variables, rule) self.assertEqual(result, (False, []))
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_kwargs_passed_into_variable_method(self): class TestVariables(BaseVariables): @boolean_rule_variable def test_eq_one(self, arg1=0): return arg1 == 1 conditions = { 'all': [{ 'name': 'test_eq_one', 'operator': 'is_false', 'value': True }, { 'name': 'test_eq_one', 'operator': 'is_false', 'value': True, 'kwargs': { 'arg1': 2 } }, { 'name': 'test_eq_one', 'operator': 'is_true', 'value': True, 'kwargs': { 'arg1': 1 } }] } tv = TestVariables() result = engine.check_conditions_recursively(conditions, tv) self.assertTrue(result)
def test_check_any_conditions_with_all_true(self, *args): conditions = {'any': [{'thing1': ''}, {'thing2': ''}]} variables = BaseVariables() result = engine.check_conditions_recursively(conditions, variables) self.assertEqual(result, T_NO_OVERRIDES) engine.check_condition.assert_called_once_with( {'thing1': ''}, variables, override_params=OVERRIDE_NONE)
def test_check_any_conditions_with_all_true(self, *args): conditions = {'any': [{'thing1': ''}, {'thing2': ''}]} variables = BaseVariables() rule = {'conditions': conditions, 'actions': []} result = engine.check_conditions_recursively(conditions, variables, rule) self.assertEqual(result, (True, [(True,)])) engine.check_condition.assert_called_once_with({'thing1': ''}, variables, rule)
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, None)
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_check_any_conditions_with_all_false(self, *args): conditions = {'any': [{'thing1': ''}, {'thing2': ''}]} variables = BaseVariables() rule = {'conditions': conditions, 'actions': []} result = engine.check_conditions_recursively(conditions, variables, rule) self.assertEqual(result, (False, [])) # assert call count and most recent call are as expected self.assertEqual(engine.check_condition.call_count, 2) engine.check_condition.assert_called_with(conditions['any'][1], variables, rule)
def test_check_any_conditions_with_all_false(self, *args): conditions = {'any': [{'thing1': ''}, {'thing2': ''}]} variables = BaseVariables() result = engine.check_conditions_recursively(conditions, variables) self.assertEqual(result, F_NO_OVERRIDES) # 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, override_params=OVERRIDE_NONE)
def test_recursive_overrides_collect_repeated(self, *args): conditions = {'all': [{'name': 1}, {'name': 2}]} bv = BaseVariables() def side_effect(condition, _, **kwargs): if condition['name'] == 1: return True, OVERRIDE_C3 elif condition['name'] == 2: return True, OVERRIDE_C4 engine.check_condition.side_effect = side_effect result, overrides = engine.check_conditions_recursively(conditions, bv) self.assertEqual(engine.check_condition.call_count, 2) engine.check_condition.assert_any_call({'name': 1}, bv, override_params=OVERRIDE_NONE) engine.check_condition.assert_any_call({'name': 2}, bv, override_params=OVERRIDE_C3) self.assertTrue(result) self.assertDictEqual(overrides, OVERRIDE_C4)
def test_case8(self): """ cond1: false or (cond2: true and cond3: true) => [cond2, cond3] """ conditions = { 'any': [{ 'name': 'true_variable', 'operator': 'is_false', 'value': '1' }, { 'all': [{ 'name': 'true_variable', 'operator': 'is_true', 'value': '2' }, { 'name': 'true_variable', 'operator': 'is_true', 'value': '3' }] }] } variables = TrueVariables() rule = {'conditions': conditions, 'actions': []} result = engine.check_conditions_recursively(conditions, variables, rule) self.assertEqual(result, (True, [ ConditionResult(result=True, name='true_variable', operator='is_true', value='2', parameters={}), ConditionResult(result=True, name='true_variable', operator='is_true', value='3', parameters={}), ]))
def test_check_any_condition_with_no_items_fails(self): with self.assertRaises(AssertionError): engine.check_conditions_recursively({'any': []}, BaseVariables())
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_all_and_any_together(self): conditions = {'any': [], 'all': []} variables = BaseVariables() rule = {"conditions": conditions, "actions": []} with self.assertRaises(AssertionError): engine.check_conditions_recursively(conditions, variables, rule)
def test_check_all_condition_with_no_items_fails(self): conditions = {'all': []} rule = {"conditions": conditions, "actions": []} variables = BaseVariables() with self.assertRaises(AssertionError): engine.check_conditions_recursively(conditions, variables, rule)