##// END OF EJS Templates
Adding a test for rollback in case of rejection
Sylvain Corlay -
Show More
@@ -1333,11 +1333,20 b' def test_pickle_hastraits():'
1333
1333
1334 def test_hold_trait_notifications():
1334 def test_hold_trait_notifications():
1335 changes = []
1335 changes = []
1336
1336 class Test(HasTraits):
1337 class Test(HasTraits):
1337 a = Integer(0)
1338 a = Integer(0)
1339 b = Integer(0)
1340
1338 def _a_changed(self, name, old, new):
1341 def _a_changed(self, name, old, new):
1339 changes.append((old, new))
1342 changes.append((old, new))
1340
1343
1344 def _b_validate(self, value, trait):
1345 if value != 0:
1346 raise TraitError('Only 0 is a valid value')
1347 return value
1348
1349 # Test context manager and nesting
1341 t = Test()
1350 t = Test()
1342 with t.hold_trait_notifications():
1351 with t.hold_trait_notifications():
1343 with t.hold_trait_notifications():
1352 with t.hold_trait_notifications():
@@ -1356,8 +1365,16 b' def test_hold_trait_notifications():'
1356 t.a = 4
1365 t.a = 4
1357 nt.assert_equal(t.a, 4)
1366 nt.assert_equal(t.a, 4)
1358 nt.assert_equal(changes, [])
1367 nt.assert_equal(changes, [])
1359 nt.assert_equal(changes, [(3,4)])
1360
1368
1369 nt.assert_equal(changes, [(3,4)])
1370 # Test roll-back
1371 try:
1372 with t.hold_trait_notifications():
1373 t.b = 1 # raises a Trait error
1374 except:
1375 pass
1376 nt.assert_equal(t.b, 0)
1377
1361
1378
1362 class OrderTraits(HasTraits):
1379 class OrderTraits(HasTraits):
1363 notified = Dict()
1380 notified = Dict()
@@ -594,6 +594,7 b' class HasTraits(py3compat.with_metaclass(MetaHasTraits, object)):'
594 """
594 """
595 if self._cross_validation_lock is True:
595 if self._cross_validation_lock is True:
596 yield
596 yield
597 return
597 else:
598 else:
598 self._cross_validation_lock = True
599 self._cross_validation_lock = True
599 cache = {}
600 cache = {}
@@ -628,8 +629,8 b' class HasTraits(py3compat.with_metaclass(MetaHasTraits, object)):'
628 notifications = {}
629 notifications = {}
629 raise e
630 raise e
630 finally:
631 finally:
631 self._cross_validation_lock = False
632 self._notify_trait = _notify_trait
632 self._notify_trait = _notify_trait
633 self._cross_validation_lock = False
633 if isinstance(_notify_trait, types.MethodType):
634 if isinstance(_notify_trait, types.MethodType):
634 # FIXME: remove when support is bumped to 3.4.
635 # FIXME: remove when support is bumped to 3.4.
635 # when original method is restored,
636 # when original method is restored,
General Comments 0
You need to be logged in to leave comments. Login now