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_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_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): _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_in(self): _in = In(["dog", "cat"]) str_token = StringToken("my_token", _in) if not str_token.evaluate("dog"): self.fail()
def test_string_type_list(self): with pytest.raises(ValueError): _in = In([35, 45, 67])
def test_string_type(self): with pytest.raises(ValueError): _in = In(35)