-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rule_engine.py
More file actions
332 lines (276 loc) · 10 KB
/
test_rule_engine.py
File metadata and controls
332 lines (276 loc) · 10 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import asyncio
import pytest
from openscada_lite.common.bus.event_bus import EventBus
from openscada_lite.modules.rule.manager.rule_manager import RuleEngine
from openscada_lite.common.bus.event_types import EventType
from openscada_lite.common.models.entities import Rule
from openscada_lite.common.models.dtos import (
SendCommandMsg,
RaiseAlarmMsg,
LowerAlarmMsg,
TagUpdateMsg,
)
@pytest.fixture(autouse=True)
def clear_test():
EventBus.get_instance().clear_subscribers()
RuleEngine.reset_instance()
@pytest.mark.asyncio
async def test_send_command_triggered():
test_bus = EventBus.get_instance()
engine = RuleEngine.get_instance()
engine.rules = [
Rule(
rule_id="test_command",
on_condition="WaterTank@pressure > 50",
on_actions=["send_command('WaterTank@VALVE1_POS', 0)"],
)
]
engine.build_tag_to_rules_index()
received = []
async def capture(msg: SendCommandMsg):
received.append(msg)
test_bus.subscribe(EventType.SEND_COMMAND, capture)
# Trigger condition
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(datapoint_identifier="WaterTank@pressure", value=60),
)
await asyncio.sleep(0.01)
assert len(received) == 1
assert received[0].datapoint_identifier == "WaterTank@VALVE1_POS"
assert received[0].value == 0
assert received[0].command_id is not None
@pytest.mark.asyncio
async def test_alarm_active_inactive():
test_bus = EventBus.get_instance()
engine = RuleEngine.get_instance()
engine.rules = [
Rule(
rule_id="test_alarm",
on_condition="WaterTank@temperature > 80",
on_actions=["raise_alarm()"],
off_condition="WaterTank@temperature <= 70",
off_actions=["lower_alarm()"],
)
]
engine.build_tag_to_rules_index()
alarms_active = []
alarms_inactive = []
async def capture_alarm_active(msg: RaiseAlarmMsg):
alarms_active.append(msg)
async def capture_alarm_inactive(msg: LowerAlarmMsg):
alarms_inactive.append(msg)
test_bus.subscribe(EventType.RAISE_ALARM, capture_alarm_active)
test_bus.subscribe(EventType.LOWER_ALARM, capture_alarm_inactive)
# Condition true → alarm_active
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(datapoint_identifier="WaterTank@temperature", value=90),
)
await asyncio.sleep(0.01)
assert len(alarms_active) == 1
assert alarms_active[0].datapoint_identifier == "WaterTank@temperature"
# Condition false → alarm_inactive
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(datapoint_identifier="WaterTank@temperature", value=70),
)
await asyncio.sleep(0.01)
assert len(alarms_inactive) == 1
assert alarms_inactive[0].datapoint_identifier == "WaterTank@temperature"
@pytest.mark.asyncio
async def test_multiple_actions():
test_bus = EventBus.get_instance()
engine = RuleEngine.get_instance()
engine.rules = [
Rule(
rule_id="multi_action_rule",
on_condition="WaterTank@pressure > 100",
on_actions=[
"send_command('AuxServer@VALVE1_POS', 0)",
"raise_alarm('WaterTank@pressure')",
],
)
]
engine.build_tag_to_rules_index()
commands = []
alarms = []
async def capture_command(msg: SendCommandMsg):
commands.append(msg)
async def capture_alarm(msg: RaiseAlarmMsg):
alarms.append(msg)
test_bus.subscribe(EventType.SEND_COMMAND, capture_command)
test_bus.subscribe(EventType.RAISE_ALARM, capture_alarm)
# Trigger condition
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(datapoint_identifier="WaterTank@pressure", value=120),
)
await asyncio.sleep(0.01)
assert len(commands) == 1
assert commands[0].datapoint_identifier == "AuxServer@VALVE1_POS"
assert commands[0].value == 0
assert len(alarms) == 1
assert alarms[0].datapoint_identifier == "WaterTank@pressure"
@pytest.mark.asyncio
async def test_on_action_triggers_twice_only_onc_alarm():
test_bus = EventBus.get_instance()
engine = RuleEngine.get_instance()
engine.rules = [
Rule(
rule_id="repeat_on_rule",
on_condition="WaterTank@level > 10",
on_actions=["raise_alarm()"],
# No off_condition
)
]
engine.build_tag_to_rules_index()
alarms = []
async def capture_alarm(msg: RaiseAlarmMsg):
alarms.append(msg)
test_bus.subscribe(EventType.RAISE_ALARM, capture_alarm)
# First trigger
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(datapoint_identifier="WaterTank@level", value=15),
)
await asyncio.sleep(0.01)
# Second trigger (should trigger again)
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(datapoint_identifier="WaterTank@level", value=20),
)
await asyncio.sleep(0.01)
assert len(alarms) == 1
assert alarms[0].datapoint_identifier == "WaterTank@level"
@pytest.mark.asyncio
async def test_switch_error_rules_toggle():
"""
Verify that switch_error_straight and switch_error_turn rules correctly
activate and deactivate depending on LEFT/RIGHT switch values.
"""
test_bus = EventBus.get_instance()
engine = RuleEngine.get_instance()
engine.rules = [
Rule(
rule_id="switch_error_straight",
on_condition="TrainTestDriver@RIGHT_SWITCH_CONTROL == 'STRAIGHT' "
"and TrainTestDriver@LEFT_SWITCH_CONTROL == 'TURN'",
on_actions=["raise_alarm()"],
),
Rule(
rule_id="switch_error_turn",
on_condition="TrainTestDriver@RIGHT_SWITCH_CONTROL == 'TURN' "
"and TrainTestDriver@LEFT_SWITCH_CONTROL == 'STRAIGHT'",
on_actions=["raise_alarm()"],
),
]
engine.build_tag_to_rules_index()
raised = []
lowered = []
async def capture_raise(msg: RaiseAlarmMsg):
raised.append((msg.datapoint_identifier, msg.rule_id))
async def capture_lower(msg: LowerAlarmMsg):
lowered.append((msg.datapoint_identifier, msg.rule_id))
test_bus.subscribe(EventType.RAISE_ALARM, capture_raise)
test_bus.subscribe(EventType.LOWER_ALARM, capture_lower)
# --- 1. Both STRAIGHT initially -> no alarms
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(
datapoint_identifier="TrainTestDriver@LEFT_SWITCH_CONTROL", value="STRAIGHT"
),
)
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(
datapoint_identifier="TrainTestDriver@RIGHT_SWITCH_CONTROL",
value="STRAIGHT",
),
)
await asyncio.sleep(0.01)
assert raised == []
assert lowered == []
# --- 2. Right TURN, Left STRAIGHT -> switch_error_turn should activate
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(
datapoint_identifier="TrainTestDriver@RIGHT_SWITCH_CONTROL", value="TURN"
),
)
await asyncio.sleep(0.01)
assert any(r[1] == "switch_error_turn" for r in raised)
# --- 3. Now Left TURN, Right TURN -> error_turn should deactivate,
# error_straight should NOT activate
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(
datapoint_identifier="TrainTestDriver@LEFT_SWITCH_CONTROL", value="TURN"
),
)
await asyncio.sleep(0.01)
# should deactivate switch_error_turn
assert any(lowered_alarm[1] == "switch_error_turn" for lowered_alarm in lowered)
# and not raise straight alarm
assert not any(
r[1] == "switch_error_straight" for r in raised if r[1] != "switch_error_turn"
)
# --- 4. Right STRAIGHT, Left TURN -> now switch_error_straight should activate
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(
datapoint_identifier="TrainTestDriver@RIGHT_SWITCH_CONTROL",
value="STRAIGHT",
),
)
await asyncio.sleep(0.01)
assert any(r[1] == "switch_error_straight" for r in raised)
# --- 5. Both STRAIGHT again -> switch_error_straight should deactivate
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(
datapoint_identifier="TrainTestDriver@LEFT_SWITCH_CONTROL", value="STRAIGHT"
),
)
await asyncio.sleep(0.01)
assert any(lowered_alarm[1] == "switch_error_straight" for lowered_alarm in lowered)
@pytest.mark.asyncio
async def test_boolean_symbols():
"""
Test that rules using TRUE and FALSE symbols are correctly evaluated.
"""
test_bus = EventBus.get_instance()
engine = RuleEngine.get_instance()
engine.rules = [
Rule(
rule_id="test_boolean_true",
on_condition="CameraDriver@CAMERA_1_ALARM == TRUE",
on_actions=["raise_alarm('CameraDriver@CAMERA_1_ALARM')"],
)
]
engine.build_tag_to_rules_index()
alarms_raised = []
alarms_lowered = []
async def capture_alarm_raised(msg: RaiseAlarmMsg):
alarms_raised.append(msg)
# Add debugging output to capture which rule is triggering the action
async def capture_alarm_lowered(msg: LowerAlarmMsg):
alarms_lowered.append(msg)
test_bus.subscribe(EventType.RAISE_ALARM, capture_alarm_raised)
test_bus.subscribe(EventType.LOWER_ALARM, capture_alarm_lowered)
# Trigger TRUE condition
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(datapoint_identifier="CameraDriver@CAMERA_1_ALARM", value="TRUE"),
)
await asyncio.sleep(0.01)
assert len(alarms_raised) == 1
assert alarms_raised[0].datapoint_identifier == "CameraDriver@CAMERA_1_ALARM"
# Trigger FALSE condition
await test_bus.publish(
EventType.TAG_UPDATE,
TagUpdateMsg(datapoint_identifier="CameraDriver@CAMERA_1_ALARM", value="FALSE"),
)
await asyncio.sleep(0.01)
assert len(alarms_lowered) == 1
assert alarms_lowered[0].datapoint_identifier == "CameraDriver@CAMERA_1_ALARM"