23
23
from bzrlib.tests import TestCase
26
def create_decorator_sample(style, except_in_unlock=False):
26
class SampleUnlockError(Exception):
30
def create_decorator_sample(style, unlock_error=None):
27
31
"""Create a DecoratorSample object, using specific lock operators.
29
33
:param style: The type of lock decorators to use (fast/pretty/None)
30
:param except_in_unlock: If True, raise an exception during unlock
34
:param unlock_error: If specified, an error to raise from unlock.
31
35
:return: An instantiated DecoratorSample object.
58
62
def lock_write(self):
59
63
self.actions.append('lock_write')
65
@decorators.only_raises(SampleUnlockError)
63
68
self.actions.append('unlock_fail')
64
raise KeyError('during unlock')
66
71
self.actions.append('unlock')
120
125
def test_read_lock_raises_original_error(self):
121
126
sam = create_decorator_sample(self._decorator_style,
122
except_in_unlock=True)
127
unlock_error=SampleUnlockError())
123
128
self.assertRaises(TypeError, sam.fail_during_read)
124
129
self.assertEqual(['lock_read', 'fail_during_read', 'unlock_fail'],
127
132
def test_write_lock_raises_original_error(self):
128
133
sam = create_decorator_sample(self._decorator_style,
129
except_in_unlock=True)
134
unlock_error=SampleUnlockError())
130
135
self.assertRaises(TypeError, sam.fail_during_write)
131
136
self.assertEqual(['lock_write', 'fail_during_write', 'unlock_fail'],
134
139
def test_read_lock_raises_unlock_error(self):
135
140
sam = create_decorator_sample(self._decorator_style,
136
except_in_unlock=True)
137
self.assertRaises(KeyError, sam.frob)
141
unlock_error=SampleUnlockError())
142
self.assertRaises(SampleUnlockError, sam.frob)
138
143
self.assertEqual(['lock_read', 'frob', 'unlock_fail'], sam.actions)
140
145
def test_write_lock_raises_unlock_error(self):
141
146
sam = create_decorator_sample(self._decorator_style,
142
except_in_unlock=True)
143
self.assertRaises(KeyError, sam.bank, 'bar', biz='bing')
147
unlock_error=SampleUnlockError())
148
self.assertRaises(SampleUnlockError, sam.bank, 'bar', biz='bing')
144
149
self.assertEqual(['lock_write', ('bank', 'bar', 'bing'),
145
150
'unlock_fail'], sam.actions)
277
282
decorators.needs_read_lock = cur_read
278
283
decorators.needs_write_lock = cur_write
286
class TestOnlyRaisesDecorator(TestCase):
288
def raise_ZeroDivisionError(self):
291
def test_raises_approved_error(self):
292
decorator = decorators.only_raises(ZeroDivisionError)
293
decorated_meth = decorator(self.raise_ZeroDivisionError)
294
self.assertRaises(ZeroDivisionError, decorated_meth)
296
def test_quietly_logs_unapproved_errors(self):
297
decorator = decorators.only_raises(IOError)
298
decorated_meth = decorator(self.raise_ZeroDivisionError)
299
self.assertLogsError(ZeroDivisionError, decorated_meth)