-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_manager.py
executable file
·163 lines (149 loc) · 5.25 KB
/
test_manager.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
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
import pytest
import logging
import serial
import manager
from constants import *
from datetime import datetime, timedelta
from unittest.mock import MagicMock
from manager import Effector, EffectorManager, SensorValues
log = logging.getLogger()
log.setLevel(logging.DEBUG)
class TestState():
def __init__(self, name,
air_hum, air_temp,
soil_hum, soil_temp,
blower_state_1,
water_pump_state_1,
radiator_valve_state_1,
air_renew_valve_state_1,
expected_handshakes):
self.name = name,
self.air_hum = air_hum,
self.air_temp = air_temp,
self.soil_hum = soil_hum,
self.soil_temp = soil_temp
self.sensors = SensorValues("file")
self.sensors.air_hum = air_hum
self.sensors.air_temp = air_temp
self.sensors.soil_hum = soil_hum
self.sensors.soil_temp = soil_temp
self.sensors.air_O2 = None
self.expected_handshakes = expected_handshakes
self.effectors = EffectorManager(
"file",
water_pump=Effector(
name="water pump",
curr_state=water_pump_state_1,
prev_time=datetime.now(),
on_interval=WATER_PUMP_ON_INTERVAL,
off_interval=WATER_PUMP_OFF_INTERVAL,
on_msg=WATER_PUMP_ON_MSG,
off_msg=WATER_PUMP_OFF_MSG,
),
blower=Effector(
name="blower",
curr_state=blower_state_1,
prev_time=datetime.now() - DRYING_OFF_INTERVAL -
DRYING_ON_INTERVAL - timedelta(seconds=1),
on_interval=BLOWER_ON_INTERVAL,
off_interval=BLOWER_OFF_INTERVAL,
on_msg=BLOWER_ON_MSG,
off_msg=BLOWER_OFF_MSG,
),
radiator_valve=Effector(
name="radiator valve",
curr_state=radiator_valve_state_1,
prev_time=datetime.now(),
on_interval=RADIATOR_VALVE_ON_INTERVAL,
on_msg=RADIATOR_ON_MSG,
off_msg=RADIATOR_OFF_MSG,
),
air_renew_valve=Effector(
name="air renewal valve",
curr_state=air_renew_valve_state_1,
prev_time=datetime.now(),
on_interval=AIR_RENEW_ON_INTERVAL,
off_interval=AIR_RENEW_OFF_INTERVAL,
on_msg=AIR_RENEW_ON_MSG,
off_msg=AIR_RENEW_OFF_MSG,
),
)
state_list = [
TestState(
name="nominal",
air_hum=40,
air_temp=22,
soil_hum=SOIL_H2O_MIN,
soil_temp=SOIL_TEMP_MAX - 10,
blower_state_1=State.OFF,
water_pump_state_1=State.OFF,
radiator_valve_state_1=State.OFF,
air_renew_valve_state_1=State.OFF,
expected_handshakes=[]
),
TestState(
name="soil temp too high",
air_hum=40,
air_temp=22,
soil_hum=SOIL_H2O_MIN,
soil_temp=SOIL_TEMP_MAX + 1,
blower_state_1=State.OFF,
water_pump_state_1=State.OFF,
radiator_valve_state_1=State.OFF,
air_renew_valve_state_1=State.OFF,
expected_handshakes=[RADIATOR_ON_MSG, BLOWER_ON_MSG]
),
TestState(
name="soil temp too high, blower already on",
air_hum=40,
air_temp=22,
soil_hum=SOIL_H2O_MIN,
soil_temp=SOIL_TEMP_MAX + 1,
blower_state_1=State.ON,
water_pump_state_1=State.OFF,
radiator_valve_state_1=State.OFF,
air_renew_valve_state_1=State.OFF,
expected_handshakes=[RADIATOR_ON_MSG]
),
TestState(
name="soil humidity too high",
air_hum=40,
air_temp=22,
soil_hum=SOIL_H2O_MAX + 1,
soil_temp=40,
blower_state_1=State.OFF,
water_pump_state_1=State.OFF,
radiator_valve_state_1=State.OFF,
air_renew_valve_state_1=State.OFF,
expected_handshakes=[BLOWER_ON_MSG, RADIATOR_ON_MSG, AIR_RENEW_ON_MSG]
),
TestState(
name="soil humidity too high with water pump to turn off",
air_hum=40,
air_temp=22,
soil_hum=SOIL_H2O_MAX + 1,
soil_temp=SOIL_TEMP_MAX + 1,
blower_state_1=State.OFF,
water_pump_state_1=State.ON,
radiator_valve_state_1=State.OFF,
air_renew_valve_state_1=State.OFF,
expected_handshakes=[
BLOWER_ON_MSG, WATER_PUMP_OFF_MSG, RADIATOR_ON_MSG, AIR_RENEW_ON_MSG]
)
]
@pytest.mark.parametrize("case", state_list)
def test_state(case):
logging.info(case.name)
# Do not emit any real messages
ser = serial.Serial()
ser.write = MagicMock()
manager.current_time_is_at_night = MagicMock(return_value=False)
case.effectors.manage(ser, case.sensors)
for handshake in case.expected_handshakes:
logging.info(f"Expected handshake: {MSG_TO_TEXT[handshake]}")
for handshake in case.effectors.expected_handshakes.keys():
logging.info(f"Received handshake: {MSG_TO_TEXT[handshake]}")
for handshake in case.expected_handshakes:
assert(handshake in case.effectors.expected_handshakes.keys())
assert(len(case.expected_handshakes) == len(
case.effectors.expected_handshakes))