-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtest_observe.py
More file actions
296 lines (214 loc) · 6.69 KB
/
test_observe.py
File metadata and controls
296 lines (214 loc) · 6.69 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
#------------------------------------------------------------------------------
# Copyright (c) 2013-2017, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
"""Test the notification mechanisms.
"""
import pytest
from atom.api import Atom, Int, List, Value, Event, Signal, observe
def test_static_observers(capsys):
"""Test using static observers.
"""
class Extended(Atom):
val = Int()
obs_decorator = observe('val2', 'ext.val')
class ObserverTest(Atom):
ext = Value()
val2 = Int(0)
changes = List()
@obs_decorator
def react(self, change):
self.changes.append(change['name'])
manual_obs = obs_decorator(react.func)
ot = ObserverTest()
ext1 = Extended()
ext2 = Extended()
# Test installing the extended observer
ot.ext = ext1
assert ext1.has_observer('val', ot.react)
assert not ext2.has_observer('val', ot.react)
ot.ext = ext2
assert ext2.has_observer('val', ot.react)
assert not ext1.has_observer('val', ot.react)
# Test notifications on value setting
ot.val2 = 1
assert 'val2' in ot.changes
ext1.val = 1
assert 'val' not in ot.changes
ext2.val = 1
assert 'val' in ot.changes
# Test manually managing static observers
ot.changes = []
# We have 2 static observers hence 2 removals
ObserverTest.val2.remove_static_observer('react')
ObserverTest.val2.remove_static_observer('manual_obs')
ot.val2 += 1
assert not ot.changes
ObserverTest.val2.add_static_observer('react')
ot.val2 += 1
assert ot.changes
# Test removing the extended observer upon deletion
del ot.ext
assert not ext2.has_observer('val', ot.react)
assert not capsys.readouterr()[1]
ot.ext = 12
assert capsys.readouterr()[1]
def test_dynamic_observers():
"""Test using dynamic observers.
"""
class Observer(object):
def __init__(self):
self.count = 0
def react(self, change):
self.count += 1
class DynamicTest(Atom):
val = Int()
val2 = Int()
ob = Observer()
dt1 = DynamicTest()
dt2 = DynamicTest()
# Test observing a single instance
dt1.observe('val', ob.react)
dt1.val = 1
assert ob.count == 1
dt2.val = 1
assert ob.count == 1
# Test unobserving (no args)
dt1.observe('val2', ob.react)
dt1.unobserve()
for m in dt1.members():
assert not dt1.has_observers(m)
# Test unobserving (single arg)
dt1.observe('val', ob.react)
dt1.observe('val2', ob.react)
assert dt1.has_observers('val')
assert dt1.has_observers('val2')
dt1.unobserve('val')
assert not dt1.has_observers('val')
assert dt1.has_observers('val2')
# Test unobserving (two args)
ob2 = Observer()
dt2.observe('val', ob.react)
dt2.observe('val', ob2.react)
assert dt2.has_observer('val', ob.react)
assert dt2.has_observer('val', ob2.react)
dt2.unobserve('val', ob2.react)
assert dt2.has_observer('val', ob.react)
assert not dt2.has_observer('val', ob2.react)
def test_binding_event_signals():
"""Test directly binding events and signals.
"""
class EventSignalTest(Atom):
e = Event()
s = Signal()
counter = Int()
def event_handler(change):
change['object'].counter += 1
def signal_handler(obj):
obj.counter += 2
est = EventSignalTest()
est.e.bind(event_handler)
est.s.connect(signal_handler)
est.e = 1
est.s(est)
assert est.counter == 3
est.e(1)
est.s.emit(est)
assert est.counter == 6
est.e.unbind(event_handler)
est.s.disconnect(signal_handler)
def test_manually_notifying():
"""Test manual notifications
"""
class Observer(object):
def __init__(self):
self.count = 0
def react(self, change):
self.count += 1
class NotifTest(Atom):
val = Int()
count = Int()
def _observe_val(self, change):
self.count += 1
ob = Observer()
nt = NotifTest()
nt.observe('val', ob.react)
# Check both static and dynamic notifiers are called
nt.val = 1
assert ob.count == 1
assert nt.count == 1
# Check only dynamic notifiers are called
nt.notify('val', dict(name='val'))
assert ob.count == 2
assert nt.count == 1
# Check that only static notifiers are called
NotifTest.val.notify(nt, dict())
assert ob.count == 2
assert nt.count == 2
# Check that notification suppression does work
ob.count = 0
nt.count = 0
with nt.suppress_notifications():
nt.val += 1
assert not nt.count and not ob.count
def test_observe_decorators():
"""Test checking observe decorator handling.
"""
def react(self, change):
pass
handler = observe(('val',))
handler(react)
handler_clone = handler.clone()
assert handler is not handler_clone
assert handler.pairs == handler_clone.pairs
assert handler.func is handler_clone.func
with pytest.raises(TypeError):
observe(12)
with pytest.raises(TypeError):
observe(['a.b.c'])
def test_handling_of_exceptions_in_observers(capsys):
"""Test that we properly print exception occuring in observers.
"""
class Observer(object):
def __init__(self):
self.count = 0
self.exc_cls = ValueError
def react(self, change):
self.count += 1
raise self.exc_cls()
class NotifTest(Atom):
val = Int()
count = Int()
exc_cls = Value(ValueError)
def _observe_val(self, change):
self.count += 1
raise self.exc_cls()
ob = Observer()
nt = NotifTest()
nt.observe('val', ob.react)
assert not capsys.readouterr()[1]
# Check both static and dynamic notifiers are called even when raising
nt.val = 1
assert ob.count == 1
assert nt.count == 1
# Check we did print to stderr
assert capsys.readouterr()[1]
class MyException(Exception):
def __str__(self):
raise ValueError()
ob.exc_cls = MyException
nt.exc_cls = MyException
assert not capsys.readouterr()[1]
# Check both static and dynamic notifiers are called even when raising
nt.val += 1
assert ob.count == 2
assert nt.count == 2
# Check we did print to stderr
err = capsys.readouterr()[1]
print(err)
assert err
# XXX add a test catching the SystemError of Python 3
# For the time being I known how to write one only using enaml