Exemplo n.º 1
0
    def test_validation_rules_printing(self, get_validation_rule_test_object):
        """Testing propagation of data down through child nodes."""
        test_object = get_validation_rule_test_object
        # Create some rules
        # True Test Rule
        rule1 = Rule().on(test_object)\
                          .when('i')\
                          .is_equal(5)
        # Create another True rule
        rule2 = Rule().on(test_object)\
                          .when('a_ge')\
                          .is_equal('a_g')

        # Create a false rule
        rule3 = Rule().on(test_object)\
                          .when('a_g')\
                          .is_equal(50)
        # Create another false rule
        rule4 = Rule().on(test_object)\
                          .when('a_g')\
                          .is_greater(50)
        # Add 1 and 2 to a new Rule Set
        rs1 = RuleSet()
        rs1.add_rule(rule1).add_rule(rule2)
        # Add RS1 and rules 3 and 4 to new RuleSet
        rs2 = RuleSet()
        rs2.add_rule(rs1).add_rule(rule3).add_rule(rule4)
        rs2.when_no_rules_are_true
        # Print to see how it looks in the hierarchy
        rs2.print_rule_set
Exemplo n.º 2
0
    def test_validation_rules_error_handling(self,
                                             get_validation_rule_test_object):
        """Testing propagation of data down through child nodes."""
        test_object = get_validation_rule_test_object
        # Indicate action on error
        rule = Rule().on(test_object)\
                          .when('i')\
                          .is_equal(5)\
                          .on_fail_report_error

        assert rule._action_on_fail == "report", "Action assignment failed"
        # Report Error Message ForRule
        rule.error_message = "Dangerous for i to be 5."
        assert "Dangerous" in rule._error_message, "Error message assignment didn't work"
Exemplo n.º 3
0
 def test_validation_rules_match(self, get_validation_rule_test_object):
     test_object = get_validation_rule_test_object
     rule = Rule()
     # Evaluates to true with constant
     answer = rule.on(test_object)\
                       .when('a_s')\
                       .is_match("[a-zA-Z]+")\
                       .evaluate.is_valid
     assert answer is True, "Invalid evaluation of match rule"
     # Evaluates to false with constant
     answer = rule.on(test_object)\
                       .when('a_s')\
                       .is_match("[0-9]+")\
                       .evaluate.is_valid
     assert answer is False, "Invalid evaluation of match rule"
Exemplo n.º 4
0
 def test_validation_rules_not_empty(self, get_validation_rule_test_object):
     test_object = get_validation_rule_test_object
     rule = Rule()
     # Evaluates to true
     answer = rule.on(test_object)\
                       .when('e')\
                       .is_not_empty\
                       .evaluate\
                       .is_valid
     assert answer is False, "Invalid evaluation of not empty rule"
     # Evaluates to false
     answer = rule.on(test_object)\
                       .when('s')\
                       .is_not_empty\
                       .evaluate\
                       .is_valid
     assert answer is True, "Invalid evaluation of not empty rule"
Exemplo n.º 5
0
 def test_validation_rules_not_none(self, get_validation_rule_test_object):
     test_object = get_validation_rule_test_object
     rule = Rule()
     # Evaluates to true
     answer = rule.on(test_object)\
                       .when('a_n')\
                       .is_not_none\
                       .evaluate\
                       .is_valid
     assert answer is False, "Invalid evaluation of not none rule"
     # Evaluates to false
     answer = rule.on(test_object)\
                       .when('a_xn')\
                       .is_not_none\
                       .evaluate\
                       .is_valid
     assert answer is True, "Invalid evaluation of not none rule"
Exemplo n.º 6
0
 def test_validation_rules_string(self, get_validation_rule_test_object):
     test_object = get_validation_rule_test_object
     rule = Rule()
     # Evaluates to true
     answer = rule.on(test_object)\
                       .when('a_s')\
                       .is_string\
                       .evaluate\
                       .is_valid
     assert answer is True, "Invalid evaluation of string rule"
     # Evaluates to false
     answer = rule.on(test_object)\
                       .when('a_l')\
                       .is_string\
                       .evaluate\
                       .is_valid
     assert answer is False, "Invalid evaluation of string rule"
Exemplo n.º 7
0
 def test_validation_rules_integer(self, get_validation_rule_test_object):
     test_object = get_validation_rule_test_object
     rule = Rule()
     # Evaluates to true
     answer = rule.on(test_object)\
                       .when('i')\
                       .is_integer\
                       .evaluate\
                       .is_valid
     assert answer is True, "Invalid evaluation of integer rule"
     # Evaluates to false
     answer = rule.on(test_object)\
                       .when('s')\
                       .is_integer\
                       .evaluate\
                       .is_valid
     assert answer is False, "Invalid evaluation of integer rule"
Exemplo n.º 8
0
    def test_validation_rules_propagation(self,
                                          get_validation_rule_test_object):
        """Testing propagation of data down through child nodes."""
        test_object = get_validation_rule_test_object
        # Create some rules
        # True Test Rule
        rule1 = Rule().on(test_object)\
                          .when('i')\
                          .is_equal(5)
        # Create another True rule
        rule2 = Rule().on(test_object)\
                          .when('a_ge')\
                          .is_equal('a_g')

        # Create a false rule
        rule3 = Rule().on(test_object)\
                          .when('a_g')\
                          .is_equal(50)
        # Create another false rule
        rule4 = Rule().on(test_object)\
                          .when('a_g')\
                          .is_greater(50)
        # Add 1 and 2 to a new Rule Set
        rs1 = RuleSet()
        rs1.add_rule(rule1).add_rule(rule2)
        # Add RS1 and rules 3 and 4 to new RuleSet
        rs2 = RuleSet()
        rs2.add_rule(rs1).add_rule(rule3).add_rule(rule4)
        rs2.when_no_rules_are_true
        # Set the target object and attribute
        rs2.on(test_object)
        rs2.attribute('a_g')

        # Traverse through confirming updates
        def traverse(rule):
            assert rule._evaluated_instance == test_object, "Test object not set"
            assert rule._evaluated_attribute == "a_g", "Attribute not set"
            if isinstance(rule, RuleSet):
                for _, rule in rule._rules.items():
                    return traverse(rule)
            else:
                assert rule._evaluated_instance == test_object, "Test object not set"
                assert rule._evaluated_attribute == "a_g", "Attribute not set"

        traverse(rs2)
Exemplo n.º 9
0
    def test_validation_rules_set_and(self, get_validation_rule_test_object):
        test_object = get_validation_rule_test_object
        # Create True
        rule1 = Rule().on(test_object)\
                          .when('i')\
                          .is_equal(5)
        assert rule1.evaluate.is_valid is True, "Failed Rule 1 before assigning to set."
        # Create another True rule
        rule2 = Rule().on(test_object)\
                          .when('a_ge')\
                          .is_equal('a_g')
        assert rule2.evaluate.is_valid is True, "Failed Rule 2 before assigning to set."

        # Create a false rule
        rule3 = Rule().on(test_object)\
                          .when('a_g')\
                          .is_equal(50)
        assert rule3.evaluate.is_valid is False, "Failed Rule 3 before assigning to set."
        # Create another false rule
        rule4 = Rule().on(test_object)\
                          .when('a_g')\
                          .is_greater(50)
        assert rule4.evaluate.is_valid is False, "Failed Rule 4 before assigning to set."
        # Create True/True rule set where all must be true
        rs = RuleSet()
        rs.when_all_rules_are_true
        rs.add_rule(rule1).add_rule(rule2)
        # Check evaluation
        answer = rs.evaluate.is_valid
        assert answer is True, "Invalid evaluation of rules 1 and 2"

        # Create True/False rule set where all must be true
        rs.remove_rule(rule2).add_rule(rule3)
        # Check evaluation
        answer = rs.evaluate.is_valid
        assert answer is False, "Invalid evaluation of rules 1 and 3"

        # Create False/False rule where all must be true
        rs.remove_rule(rule3).add_rule(rule4)
        # Check evaluation
        answer = rs.evaluate.is_valid
        assert answer is False, "Invalid evaluation of rules 1 and 4"
Exemplo n.º 10
0
 def test_validation_rules_greater_equal_strings(
         self, get_validation_rule_test_object):
     test_object = get_validation_rule_test_object
     rule = Rule()
     # Evaluates to true with constant
     answer = rule.on(test_object)\
                       .when('s')\
                       .is_greater_equal("hats")\
                       .evaluate.is_valid
     assert answer is True, "Invalid evaluation of greater_equal rule"
     # Evaluates to false with constant
     answer = rule.on(test_object)\
                       .when('s')\
                       .is_greater_equal('z')\
                       .evaluate.is_valid
     assert answer is False, "Invalid evaluation of greater_equal rule"
     # Evaluates to true with attribute
     answer = rule.on(test_object)\
                       .when('a_sg')\
                       .is_greater_equal('a_s')\
                       .evaluate.is_valid
     assert answer is True, "Invalid evaluation of greater_equal rule"
     # Evaluates to false with attribute
     answer = rule.on(test_object)\
                       .when('a_s')\
                       .is_greater_equal('a_sg')\
                       .evaluate.is_valid
     assert answer is False, "Invalid evaluation of greater_equal rule"
Exemplo n.º 11
0
 def test_validation_rules_greater_equal_numbers(
         self, get_validation_rule_test_object):
     test_object = get_validation_rule_test_object
     rule = Rule()
     # Evaluates to true with constant
     answer = rule.on(test_object)\
                       .when('i')\
                       .is_greater_equal(5)\
                       .evaluate.is_valid
     assert answer is True, "Invalid evaluation of greater_equal rule"
     # Evaluates to false with constant
     answer = rule.on(test_object)\
                       .when('a_g')\
                       .is_greater_equal(50)\
                       .evaluate.is_valid
     assert answer is False, "Invalid evaluation of greater_equal rule"
     # Evaluates to true with attribute
     answer = rule.on(test_object)\
                       .when('a_g')\
                       .is_greater_equal('a_ge')\
                       .evaluate.is_valid
     assert answer is True, "Invalid evaluation of greater_equal rule"
     # Evaluates to false with attribute
     answer = rule.on(test_object)\
                       .when('a_le')\
                       .is_greater_equal('a_ge')\
                       .evaluate.is_valid
     assert answer is False, "Invalid evaluation of greater_equal rule"
Exemplo n.º 12
0
 def test_validation_rules_less_strings(self,
                                        get_validation_rule_test_object):
     test_object = get_validation_rule_test_object
     rule = Rule()
     # Evaluates to true with constant
     answer = rule.on(test_object)\
                       .when('s')\
                       .is_less('z')\
                       .evaluate\
                       .is_valid
     assert answer is True, "Invalid evaluation of less rule"
     # Evaluates to false with constant
     answer = rule.on(test_object)\
                       .when('s')\
                       .is_less('a')\
                       .evaluate\
                       .is_valid
     assert answer is False, "Invalid evaluation of less rule"
     # Evaluates to true with attribute
     answer = rule.on(test_object)\
                       .when('s')\
                       .is_less('z')\
                       .evaluate\
                       .is_valid
     assert answer is True, "Invalid evaluation of less rule"
     # Evaluates to false with attribute
     answer = rule.on(test_object)\
                       .when('s')\
                       .is_less('a_s')\
                       .evaluate\
                       .is_valid
     assert answer is False, "Invalid evaluation of less rule"
Exemplo n.º 13
0
    def test_validation_rules_set_none(self, get_validation_rule_test_object):
        test_object = get_validation_rule_test_object

        # Create True
        rule1 = Rule().on(test_object)\
                          .when('i')\
                          .is_equal(5)
        # Create another True rule
        rule2 = Rule().on(test_object)\
                          .when('a_ge')\
                          .is_equal('a_g')

        # Create a false rule
        rule3 = Rule().on(test_object)\
                          .when('a_g')\
                          .is_equal(50)
        # Create another false rule
        rule4 = Rule().on(test_object)\
                          .when('a_g')\
                          .is_greater(50)
        # Create True/True rule set where all must be true
        rs = RuleSet()
        rs.when_no_rules_are_true
        rs.add_rule(rule1)
        rs.add_rule(rule2)
        answer = rs.evaluate.is_valid
        assert answer is False, "Invalid evaluation of rules 1 and 2"
        # Create True/False rule set where all must be true
        rs.remove_rule(rule2)
        rs.add_rule(rule3)
        answer = rs.evaluate.is_valid
        rs.print_rule_set
        assert answer is False, "Invalid evaluation of rules 1 and 3"
        # Create False/False rule where all must be true
        rs.remove_rule(rule1)
        rs.add_rule(rule4)
        answer = rs.evaluate.is_valid
        rs.print_rule_set
        assert answer is True, "Invalid evaluation of rules 3 and 4"