~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_decorators.py

  • Committer: John Arbash Meinel
  • Date: 2009-10-20 19:46:46 UTC
  • mfrom: (4759 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4771.
  • Revision ID: john@arbash-meinel.com-20091020194646-wnqpd15qs19y28z7
Merge bzr.dev 4759, bringing in static_tuple and streaming improvements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
from bzrlib.tests import TestCase
24
24
 
25
25
 
26
 
def create_decorator_sample(style, except_in_unlock=False):
 
26
class SampleUnlockError(Exception):
 
27
    pass
 
28
 
 
29
 
 
30
def create_decorator_sample(style, unlock_error=None):
27
31
    """Create a DecoratorSample object, using specific lock operators.
28
32
 
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.
32
36
    """
33
37
 
58
62
        def lock_write(self):
59
63
            self.actions.append('lock_write')
60
64
 
 
65
        @decorators.only_raises(SampleUnlockError)
61
66
        def unlock(self):
62
 
            if except_in_unlock:
 
67
            if unlock_error:
63
68
                self.actions.append('unlock_fail')
64
 
                raise KeyError('during unlock')
 
69
                raise unlock_error
65
70
            else:
66
71
                self.actions.append('unlock')
67
72
 
119
124
 
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'],
125
130
                         sam.actions)
126
131
 
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'],
132
137
                         sam.actions)
133
138
 
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)
139
144
 
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)
146
151
 
276
281
        finally:
277
282
            decorators.needs_read_lock = cur_read
278
283
            decorators.needs_write_lock = cur_write
 
284
 
 
285
 
 
286
class TestOnlyRaisesDecorator(TestCase):
 
287
 
 
288
    def raise_ZeroDivisionError(self):
 
289
        1/0
 
290
        
 
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)
 
295
 
 
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)
 
300
        
 
301