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
|
# Copyright (C) 2006-2010 Canonical Ltd
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for decorator functions"""
import inspect
from bzrlib import decorators
from bzrlib.tests import TestCase
class SampleUnlockError(Exception):
pass
def create_decorator_sample(style, unlock_error=None):
"""Create a DecoratorSample object, using specific lock operators.
:param style: The type of lock decorators to use (fast/pretty/None)
:param unlock_error: If specified, an error to raise from unlock.
:return: An instantiated DecoratorSample object.
"""
if style is None:
# Default
needs_read_lock = decorators.needs_read_lock
needs_write_lock = decorators.needs_write_lock
elif style == 'pretty':
needs_read_lock = decorators._pretty_needs_read_lock
needs_write_lock = decorators._pretty_needs_write_lock
else:
needs_read_lock = decorators._fast_needs_read_lock
needs_write_lock = decorators._fast_needs_write_lock
class DecoratorSample(object):
"""Sample class that uses decorators.
Log when requests go through lock_read()/unlock() or
lock_write()/unlock.
"""
def __init__(self):
self.actions = []
def lock_read(self):
self.actions.append('lock_read')
def lock_write(self):
self.actions.append('lock_write')
@decorators.only_raises(SampleUnlockError)
def unlock(self):
if unlock_error:
self.actions.append('unlock_fail')
raise unlock_error
else:
self.actions.append('unlock')
@needs_read_lock
def frob(self):
"""Frob the sample object"""
self.actions.append('frob')
return 'newbie'
@needs_write_lock
def bank(self, bar, biz=None):
"""Bank the sample, but using bar and biz."""
self.actions.append(('bank', bar, biz))
return (bar, biz)
@needs_read_lock
def fail_during_read(self):
self.actions.append('fail_during_read')
raise TypeError('during read')
@needs_write_lock
def fail_during_write(self):
self.actions.append('fail_during_write')
raise TypeError('during write')
return DecoratorSample()
class TestDecoratorActions(TestCase):
_decorator_style = None # default
def test_read_lock_locks_and_unlocks(self):
sam = create_decorator_sample(self._decorator_style)
self.assertEqual('newbie', sam.frob())
self.assertEqual(['lock_read', 'frob', 'unlock'], sam.actions)
def test_write_lock_locks_and_unlocks(self):
sam = create_decorator_sample(self._decorator_style)
self.assertEqual(('bar', 'bing'), sam.bank('bar', biz='bing'))
self.assertEqual(['lock_write', ('bank', 'bar', 'bing'), 'unlock'],
sam.actions)
def test_read_lock_unlocks_during_failure(self):
sam = create_decorator_sample(self._decorator_style)
self.assertRaises(TypeError, sam.fail_during_read)
self.assertEqual(['lock_read', 'fail_during_read', 'unlock'],
sam.actions)
def test_write_lock_unlocks_during_failure(self):
sam = create_decorator_sample(self._decorator_style)
self.assertRaises(TypeError, sam.fail_during_write)
self.assertEqual(['lock_write', 'fail_during_write', 'unlock'],
sam.actions)
def test_read_lock_raises_original_error(self):
sam = create_decorator_sample(self._decorator_style,
unlock_error=SampleUnlockError())
self.assertRaises(TypeError, sam.fail_during_read)
self.assertEqual(['lock_read', 'fail_during_read', 'unlock_fail'],
sam.actions)
def test_write_lock_raises_original_error(self):
sam = create_decorator_sample(self._decorator_style,
unlock_error=SampleUnlockError())
self.assertRaises(TypeError, sam.fail_during_write)
self.assertEqual(['lock_write', 'fail_during_write', 'unlock_fail'],
sam.actions)
def test_read_lock_raises_unlock_error(self):
sam = create_decorator_sample(self._decorator_style,
unlock_error=SampleUnlockError())
self.assertRaises(SampleUnlockError, sam.frob)
self.assertEqual(['lock_read', 'frob', 'unlock_fail'], sam.actions)
def test_write_lock_raises_unlock_error(self):
sam = create_decorator_sample(self._decorator_style,
unlock_error=SampleUnlockError())
self.assertRaises(SampleUnlockError, sam.bank, 'bar', biz='bing')
self.assertEqual(['lock_write', ('bank', 'bar', 'bing'),
'unlock_fail'], sam.actions)
class TestFastDecoratorActions(TestDecoratorActions):
_decorator_style = 'fast'
class TestPrettyDecoratorActions(TestDecoratorActions):
_decorator_style = 'pretty'
class TestDecoratorDocs(TestCase):
"""Test method decorators"""
def test_read_lock_passthrough(self):
"""@needs_read_lock exposes underlying name and doc."""
sam = create_decorator_sample(None)
self.assertEqual('frob', sam.frob.__name__)
self.assertDocstring('Frob the sample object', sam.frob)
def test_write_lock_passthrough(self):
"""@needs_write_lock exposes underlying name and doc."""
sam = create_decorator_sample(None)
self.assertEqual('bank', sam.bank.__name__)
self.assertDocstring('Bank the sample, but using bar and biz.',
sam.bank)
def test_argument_passthrough(self):
"""Test that arguments get passed around properly."""
sam = create_decorator_sample(None)
sam.bank('1', biz='2')
self.assertEqual(['lock_write',
('bank', '1', '2'),
'unlock',
], sam.actions)
class TestPrettyDecorators(TestCase):
"""Test that pretty decorators generate nice looking wrappers."""
def get_formatted_args(self, func):
"""Return a nicely formatted string for the arguments to a function.
This generates something like "(foo, bar=None)".
"""
return inspect.formatargspec(*inspect.getargspec(func))
def test__pretty_needs_read_lock(self):
"""Test that _pretty_needs_read_lock generates a nice wrapper."""
@decorators._pretty_needs_read_lock
def my_function(foo, bar, baz=None, biz=1):
"""Just a function that supplies several arguments."""
self.assertEqual('my_function', my_function.__name__)
self.assertEqual('my_function_read_locked',
my_function.func_code.co_name)
self.assertEqual('(foo, bar, baz=None, biz=1)',
self.get_formatted_args(my_function))
self.assertDocstring(
'Just a function that supplies several arguments.', my_function)
def test__fast_needs_read_lock(self):
"""Test the output of _fast_needs_read_lock."""
@decorators._fast_needs_read_lock
def my_function(foo, bar, baz=None, biz=1):
"""Just a function that supplies several arguments."""
self.assertEqual('my_function', my_function.__name__)
self.assertEqual('read_locked', my_function.func_code.co_name)
self.assertEqual('(self, *args, **kwargs)',
self.get_formatted_args(my_function))
self.assertDocstring(
'Just a function that supplies several arguments.', my_function)
def test__pretty_needs_write_lock(self):
"""Test that _pretty_needs_write_lock generates a nice wrapper."""
@decorators._pretty_needs_write_lock
def my_function(foo, bar, baz=None, biz=1):
"""Just a function that supplies several arguments."""
self.assertEqual('my_function', my_function.__name__)
self.assertEqual('my_function_write_locked',
my_function.func_code.co_name)
self.assertEqual('(foo, bar, baz=None, biz=1)',
self.get_formatted_args(my_function))
self.assertDocstring(
'Just a function that supplies several arguments.', my_function)
def test__fast_needs_write_lock(self):
"""Test the output of _fast_needs_write_lock."""
@decorators._fast_needs_write_lock
def my_function(foo, bar, baz=None, biz=1):
"""Just a function that supplies several arguments."""
self.assertEqual('my_function', my_function.__name__)
self.assertEqual('write_locked', my_function.func_code.co_name)
self.assertEqual('(self, *args, **kwargs)',
self.get_formatted_args(my_function))
self.assertDocstring(
'Just a function that supplies several arguments.', my_function)
def test_use_decorators(self):
"""Test that you can switch the type of the decorators."""
cur_read = decorators.needs_read_lock
cur_write = decorators.needs_write_lock
try:
decorators.use_fast_decorators()
self.assertIs(decorators._fast_needs_read_lock,
decorators.needs_read_lock)
self.assertIs(decorators._fast_needs_write_lock,
decorators.needs_write_lock)
decorators.use_pretty_decorators()
self.assertIs(decorators._pretty_needs_read_lock,
decorators.needs_read_lock)
self.assertIs(decorators._pretty_needs_write_lock,
decorators.needs_write_lock)
# One more switch to make sure it wasn't just good luck that the
# functions pointed to the correct version
decorators.use_fast_decorators()
self.assertIs(decorators._fast_needs_read_lock,
decorators.needs_read_lock)
self.assertIs(decorators._fast_needs_write_lock,
decorators.needs_write_lock)
finally:
decorators.needs_read_lock = cur_read
decorators.needs_write_lock = cur_write
class TestOnlyRaisesDecorator(TestCase):
def raise_ZeroDivisionError(self):
1/0
def test_raises_approved_error(self):
decorator = decorators.only_raises(ZeroDivisionError)
decorated_meth = decorator(self.raise_ZeroDivisionError)
self.assertRaises(ZeroDivisionError, decorated_meth)
def test_quietly_logs_unapproved_errors(self):
decorator = decorators.only_raises(IOError)
decorated_meth = decorator(self.raise_ZeroDivisionError)
self.assertLogsError(ZeroDivisionError, decorated_meth)
|