-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_automate.py
304 lines (245 loc) · 8.42 KB
/
test_automate.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
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
# -*- coding: utf-8 -*-
# (c) 2015 Tuomas Airaksinen
#
# This file is part of Automate.
#
# Automate is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Automate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Automate. If not, see <http://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------------
#
# If you like Automate, please take a look at this page:
# http://evankelista.net/automate/
import pytest
from automate import *
from automate.program import Program
from automate.statusobject import AbstractSensor, AbstractActuator
import time
def test_namespace():
sys = System(exclude_services=['TextUIService'], name='test')
ns = sys.namespace # AutomateNamespace(sys)
# ns.set_system()
s = AbstractSensor()
ns['sens'] = s
assert s in ns.reverse
assert 'sens' in ns
#assert ns.sens is s
assert s in sys.sensors
ns['one'] = 1
assert 1 not in ns.reverse
a = AbstractActuator()
ns['act'] = a
with pytest.raises(ValueError):
ns['act'] = a
assert a in sys.actuators
assert a not in sys.sensors
p = Program()
ns['prg'] = p
assert p in sys.programs
#assert 'sensors' in ns
#assert 'programs' in ns
#assert 'actuators' in ns
del ns['prg']
assert not p in sys.programs
del ns['act']
assert not a in sys.actuators
sys.cleanup()
@pytest.fixture(params=[0, 1])
def mysys(request):
class sysclass(System):
mysensor = UserFloatSensor()
myactuator = FloatActuator()
prog = Program(active_condition=Value(mysensor if request.param else 'mysensor'),
on_update=Run(
SetStatus(myactuator if request.param else 'myactuator', 1),
Debug("Hello World!")),
)
s = sysclass(exclude_services=['TextUIService'], name='MySys')
yield s
s.cleanup()
def test_tags(mysys):
mysys.mysensor.tags = ['hep', 'hop']
assert mysys.mysensor.tags == {'hep', 'hop'}
mysys.mysensor.tags = 'hup,hip'
assert mysys.mysensor.tags == {'hup', 'hip'}
#['hep', 'hop']
def test_logging(caplog, mysys):
out = caplog.text()
s = 'Initializing services'
assert s in out
out = out.replace(s, '')
assert s not in out
def test_automatesystem(mysys):
s = mysys
assert s.mysensor.status == False
assert s.myactuator.status == False
s.mysensor.set_status(1)
s.flush()
assert s.myactuator.status == True
ns = s.namespace
def test_register_function(mysys):
def func():
return True
mysys.register_service_functions(func)
assert mysys.namespace['func']()
assert mysys.func()
class tst(object):
def hep(self):
return self.status
t = tst()
t.status = True
mysys.register_service_functions(t.hep)
assert mysys.hep()
def test_namechange(mysys):
assert mysys.mysensor.name == 'mysensor'
mysys.mysensor.name = 'newname'
assert mysys.mysensor.name == 'newname'
assert 'newname' in mysys.namespace
assert 'mysensor' not in mysys.namespace
def test_new_systemobj(mysys):
a1 = FloatActuator('somename', system=mysys)
assert 'somename' in mysys.namespace
mysys.prog.on_update = Run(SetStatus(mysys.myactuator, 1), SetStatus(a1, 2.0))
mysys.mysensor.set_status(1)
mysys.flush()
assert a1.status == 2.0
a2 = FloatActuator('somename', system=mysys)
assert a2.name == 'somename_00'
a3 = FloatActuator('somename', system=mysys)
assert a3.name == 'somename_01'
a4 = FloatActuator(system=mysys)
assert a4.name == 'Nameless_FloatActuator'
from automate.common import Lock
def test_lock(sysloader):
# TODO: check why this is not robust (see https://travis-ci.org/tuomas2/automate/jobs/245711227)
class mysys(System):
prg = UserIntSensor()
s = sysloader.new_system(mysys)
l = Lock('name')
flag = []
def locktst():
assert l.context
l.release()
flag.append(1)
c = s.prg.on_deactivate = Delay(0.2, Func(locktst))
c.call(s.prg)
l.acquire()
# now l is locked, but locktst will release it.
l.acquire() # This will wait until l is released.
assert flag
l.release() # final release.
def test_sysobject_callable(sysloader):
class mysys(System):
mysens = UserIntSensor(on_activate=Run('mycal'), priority=50)
mycal = Run(Run(Value(1), Shell('false')), Value(2))
s = sysloader.new_system(mysys)
def test_sysobject_callable2(sysloader):
class mysys(System):
mysens = UserIntSensor(on_activate=Run('mycal'), priority=50)
mycal = SetStatus('mysens', Add(Value('mysens'), 1))
s = sysloader.new_system(mysys)
# test some Sensors
def test_shellsensor_defaultfilter(sysloader):
class mysys(System):
p1 = ShellSensor(cmd='echo test\necho test2\necho test3')
s = sysloader.new_system(mysys)
time.sleep(0.1)
assert s.p1.status == 'test3\n'
def test_shellsensor_defaultfilter_simple(sysloader):
class mysys(System):
p1 = ShellSensor(cmd='echo test\necho test2\necho test3')
s = sysloader.new_system(mysys)
time.sleep(0.1)
assert s.p1.status == 'test3\n'
def test_shellsensor_simple(sysloader):
lines = []
def myfunc(line):
lines.append(line)
return line[:-1]
class mysys(System):
p1 = ShellSensor(cmd='echo test\necho test2\necho test3', filter=myfunc)
s = sysloader.new_system(mysys)
time.sleep(0.1)
assert s.p1.status == 'test3'
assert lines == ['test line', 'test\n', 'test2\n', 'test3\n']
def test_shellsensor(sysloader):
lines = []
def myfunc(q):
while True:
line = q.get()
if not line:
break
lines.append(line)
yield line[:-1]
class mysys(System):
p1 = ShellSensor(cmd='echo test\necho test2\necho test3', filter=myfunc)
s = sysloader.new_system(mysys)
time.sleep(0.1)
assert s.p1.status == 'test3'
assert lines == ['test\n', 'test2\n', 'test3\n']
def test_shellsensor_delayed(sysloader):
lines = []
def myfunc(q):
while True:
line = q.get()
if not line:
break
lines.append(line)
yield line[:-1]
class mysys(System):
p1 = ShellSensor(cmd='echo;echo test\necho test2\necho\n\n\nsleep 0.1\necho test3', filter=myfunc)
s = sysloader.new_system(mysys)
time.sleep(1)
assert s.p1.status == 'test3'
assert lines == ['\n', 'test\n', 'test2\n', '\n', 'test3\n']
def test_setup_system(sysloader):
class mysys(System):
lamppu1 = BoolActuator()
lamppu2 = BoolActuator()
lamppu3 = BoolActuator()
lamp_on_delay = UserFloatSensor()
lamp_off_delay = UserFloatSensor()
lamput = BoolActuator(
active_condition=Value(True),
on_update=Run(
SetStatus(lamppu1, 'lamput'),
Delay(IfElse('lamput', lamp_on_delay, lamp_off_delay),
SetStatus(lamppu3, 'lamput')),
Delay(IfElse('lamput', Value(2) * lamp_on_delay, Value(2) * lamp_off_delay),
If(Not('lamput'),
Empty(), # RemoteFunc(raspi2host, 'set_status', 'akvadimmer', 1)
),
Delay(5,
SetStatus(lamppu2, 'lamput')),
)),
)
s = sysloader.new_system(mysys)
a = s.lamput.on_update
assert a.system # run
assert a[0].system # setstatus
assert a[1].system # delay
assert a[1][0].system # ifelse
assert a[1][1].system # setstatus
assert a[2].system # delay
assert a[2][0].system # ifelse
assert a[2][0][1].system # mult
assert a[2][0][1][0].system # value
assert a[2][0][2].system # mult
assert a[2][0][2][0].system # value
#@mock.patch('traits_enaml.imports')
#@mock.patch('enaml.qt')
# def test_guithread(mock_qt, mock_traits):
# g = GuiThread()
# g.start()
# g.stop = True
# g.trigger()