def test_recursive(self): _token_age_gt = NumericToken(token_name="age", operator=Gt(35)) _token_age_lt = NumericToken(token_name="age", operator=Lt(18)) _in = In(["dog", "cat"]) _token_pet = StringToken("pet", _in) _tokens = [_token_age_gt, _token_age_lt] _or = Or(_tokens) _and = And([_or, _token_pet]) _token_dict = {"age": 40, "pet": "parrot"} print("Result of Or is {}".format(_or.evaluate(_token_dict))) print("Result of And for {} is {}".format(_token_dict, _and.evaluate(_token_dict))) if _and.evaluate(_token_dict): self.fail() _token_dict = {"age": 10, "pet": "dog"} print("Result of And for {} is {}".format(_token_dict, _and.evaluate(_token_dict))) if not _and.evaluate(_token_dict): self.fail() _token_dict = {"age": 25, "pet": "dog"} print("Result of And for {} is {}".format(_token_dict, _and.evaluate(_token_dict))) if _and.evaluate(_token_dict): self.fail()
def test_recursive(self): _token_age_gt = NumericToken(token_name="age", operator=Gt(35)) _in = In(["dog", "cat"]) _token_pet = StringToken("pet", _in) _in = In(["owned", "leased"]) _token_ownership = StringToken("ownership", _in) _tokens = [_token_age_gt, _token_pet] _or = Or(_tokens) _and = And([_or, _token_ownership]) _token_dict = {"age": 40, "pet": "parrot", "ownership": "owned"} if not _and.evaluate(_token_dict): self.fail() _token_dict = {"age": 10, "pet": "dog", "ownership": "rented"} if _and.evaluate(_token_dict): self.fail() _token_dict = {"age": 25, "pet": "parrot", "ownership": "owned"} if _and.evaluate(_token_dict): self.fail()
def test_evaluate_negative(self): _gt_operator = Gt(2) _token = NumericToken("no_of_bl_paid_off_successfully", _gt_operator) _and = And([_token]) _score_row = RuleRowScore(antecedent=_and, consequent=70) _token_dict = {"no_of_bl_paid_off_successfully": 1} if _score_row.evaluate(_token_dict) != 0.0: self.fail()
def test_insufficient_values(self): with pytest.raises(ValueError): _token_age = NumericToken(token_name="age", operator=Gt(35)) _in = In(["dog", "cat"]) _token_pet = StringToken("pet", _in) _tokens = [_token_age, _token_pet] _and = And(_tokens) _token_dict = {"age": 40} _and.evaluate(_token_dict)
def test_evaluate_true(self): _token_age = NumericToken(token_name="age", operator=Gt(35)) _in = In(["dog", "cat"]) _token_pet = StringToken("pet", _in) _tokens = [_token_age, _token_pet] _and = And(_tokens) _token_dict = {"age": 40, "pet": "dog"} if not _and.evaluate(_token_dict): self.fail()
def test_evaluate_false(self): _token_age = NumericToken(token_name="age", operator=Gt(35)) _in = In(["dog", "cat"]) _token_pet = StringToken("pet", _in) _tokens = [_token_age, _token_pet] _or = Or(_tokens) _token_dict = {"age": 25, "pet": "parrot"} if _or.evaluate(_token_dict): self.fail()
def test_evaluate(self): _gt_operator = Gt(2) _token = NumericToken("no_of_bl_paid_off_successfully", _gt_operator) _and = And([_token]) _score_row = RuleRowScore(antecedent=_and, consequent=70) _score_set = RuleSetScore([_score_row], 0.6) _token_dict = {"no_of_bl_paid_off_successfully": 3} if _score_set.evaluate(_token_dict) != 42: self.fail()
def test_evaluate(self): _token_age = NumericToken(token_name="age", operator=Gt(35)) _in = In(["dog", "cat"]) _token_pet = StringToken("pet", _in) _tokens = [_token_age, _token_pet] _and = And(_tokens) _rule_row_decision = RuleRowDecision(_and, "GO") _token_dict = {"age": 40, "pet": "dog"} if _rule_row_decision.evaluate(_token_dict) != "GO": self.fail()
def test_evaluate_no_decision(self): _token_age = NumericToken(token_name="age", operator=Gt(35)) _in = In(["dog", "cat"]) _token_pet = StringToken("pet", _in) _tokens = [_token_age, _token_pet] _and = And(_tokens) _rule_row_decision_1 = RuleRowDecision(_and, "GO") _rule_set_decision = RuleSetDecision([_rule_row_decision_1]) _token_dict = {"age": 25, "pet": "parrot"} if _rule_set_decision.evaluate(_token_dict) != RuleSetDecision.NO_DECISION_ROW_EVALUATED: self.fail()
def test_evaluate(self): _token_age = NumericToken(token_name="age", operator=Gt(35)) _in = In(["dog", "cat"]) _token_pet = StringToken("pet", _in) _tokens = [_token_age, _token_pet] _and = And(_tokens) _rule_row_decision_1 = RuleRowDecision(_and, "GO") _token_age = NumericToken(token_name="age", operator=Lte(35)) _in = NotIn(["dog", "cat"]) _token_pet = StringToken("pet", _in) _tokens = [_token_age, _token_pet] _and = And(_tokens) _rule_row_decision_2 = RuleRowDecision(_and, "NO_GO") _token_dict = {"age": 25, "pet": "parrot"} _rule_set_decision = RuleSetDecision([_rule_row_decision_1, _rule_row_decision_2]) if _rule_set_decision.evaluate(_token_dict) != "NO_GO": self.fail()
def test_evaluate_gt(self): _gt = Gt(35) numeric_token = NumericToken("my_token", _gt) if not numeric_token.evaluate(40): self.fail()