Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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={}),
        ]))
Ejemplo n.º 7
0
    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, []))
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
    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={}),
        ]))
Ejemplo n.º 19
0
 def test_check_any_condition_with_no_items_fails(self):
     with self.assertRaises(AssertionError):
         engine.check_conditions_recursively({'any': []}, BaseVariables())
Ejemplo n.º 20
0
 def test_check_all_and_any_together(self):
     conditions = {'any': [], 'all': []}
     variables = BaseVariables()
     with self.assertRaises(AssertionError):
         engine.check_conditions_recursively(conditions, variables)
Ejemplo n.º 21
0
 def test_check_any_condition_with_no_items_fails(self):
     with self.assertRaises(AssertionError):
         engine.check_conditions_recursively({'any': []}, BaseVariables())
Ejemplo n.º 22
0
 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)
Ejemplo n.º 23
0
 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)