-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_rule_engine_core.py
90 lines (75 loc) · 3.07 KB
/
test_rule_engine_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import unittest
from agenda import Agenda
from rule_engine import Condition, Expression, Action, Rule
from rule_engine_core import RuleEngineCore, WorkingMemory
class TestRule(unittest.TestCase):
def test_evaluate_true(self):
condition = Condition(Expression('>', 'age', 18))
action = Action(Expression('=', 'is_eligible', True))
rule = Rule(condition, action)
facts = {'age': 20, 'is_eligible': False}
rule.evaluate(facts)
self.assertEqual(facts['is_eligible'], True)
def test_evaluate_false(self):
condition = Condition(Expression('>', 'age', 18))
action = Action(Expression('=', 'is_eligible', True))
rule = Rule(condition, action)
facts = {'age': 16, 'is_eligible': False}
rule.evaluate(facts)
self.assertEqual(facts['is_eligible'], False)
class TestAgenda(unittest.TestCase):
def test_add_rule(self):
rule1 = Rule(None, None)
rule2 = Rule(None, None)
agenda = Agenda()
agenda.add_rule(rule1, 1)
agenda.add_rule(rule2, 2)
expected_rules = [(1, rule1), (2, rule2)]
self.assertEqual(agenda.rules, expected_rules)
def test_remove_rule(self):
rule1 = Rule(None, None)
rule2 = Rule(None, None)
agenda = Agenda()
agenda.add_rule(rule1, 1)
agenda.add_rule(rule2, 2)
agenda.remove_rule(rule1)
expected_rules = [(2, rule2)]
self.assertEqual(agenda.rules, expected_rules)
def test_update_rule(self):
rule1 = Rule(None, None)
rule2 = Rule(None, None)
agenda = Agenda()
agenda.add_rule(rule1, 1)
agenda.add_rule(rule2, 2)
agenda.update_rule(rule1, 3)
expected_rules = [(2, rule2), (3, rule1)]
self.assertEqual(agenda.rules, expected_rules)
def test_get_next_rule(self):
rule1 = Rule(None, None)
rule2 = Rule(None, None)
agenda = Agenda()
agenda.add_rule(rule1, 1)
agenda.add_rule(rule2, 2)
next_rule = agenda.get_next_rule()
self.assertEqual(next_rule, rule1)
next_rule = agenda.get_next_rule()
self.assertEqual(next_rule, rule2)
next_rule = agenda.get_next_rule()
self.assertEqual(next_rule, None)
class TestRuleEngineCore(unittest.TestCase):
def test_execute(self):
condition1 = Condition(Expression('>', 'age', 18))
action1 = Action(Expression('=', 'is_eligible', True))
rule1 = Rule(condition1, action1)
rule1.priority = 1
condition2 = Condition(Expression('==', 'is_eligible', True))
action2 = Action(Expression('=', 'discount', 0.1))
rule2 = Rule(condition2, action2)
rule2.priority = 2
rules = [rule1, rule2]
working_memory = WorkingMemory({'age': 20})
rule_engine_core = RuleEngineCore(rules)
rule_engine_core.working_memory = working_memory
rule_engine_core.execute()
expected_facts = {'age': 20, 'is_eligible': True, 'discount': 0.1}
self.assertEqual(rule_engine_core.working_memory.facts, expected_facts)