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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
# Copyright 2010 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
__metaclass__ = type
__all__ = [
'BrowsesWithQueryLimit',
'Contains',
'DocTestMatches',
'DoesNotCorrectlyProvide',
'DoesNotProvide',
'EqualsIgnoringWhitespace',
'HasQueryCount',
'IsNotProxied',
'IsProxied',
'MatchesPickerText',
'MatchesTagText',
'MissingElement',
'MultipleElements',
'Provides',
'ProvidesAndIsProxied',
]
from lazr.lifecycle.snapshot import Snapshot
from testtools import matchers
from testtools.content import Content
from testtools.content_type import UTF8_TEXT
from testtools.matchers import (
DocTestMatches as OriginalDocTestMatches,
Equals,
LessThan,
Matcher,
Mismatch,
MismatchesAll,
)
from zope.interface.exceptions import (
BrokenImplementation,
BrokenMethodImplementation,
DoesNotImplement,
)
from zope.interface.verify import verifyObject
from zope.security.proxy import (
builtin_isinstance,
Proxy,
)
from lp.services.webapp import canonical_url
from lp.services.webapp.batching import BatchNavigator
from lp.testing import normalize_whitespace
from lp.testing._login import person_logged_in
from lp.testing._webservice import QueryCollector
class BrowsesWithQueryLimit(Matcher):
"""Matches the rendering of an objects default view with a query limit.
This is a wrapper for HasQueryCount which does the heavy lifting on the
query comparison - BrowsesWithQueryLimit simply provides convenient
glue to use a userbrowser and view an object.
"""
def __init__(self, query_limit, user, view_name="+index", **options):
"""Create a BrowsesWithQueryLimit checking for limit query_limit.
:param query_limit: The number of queries permited for the page.
:param user: The user to use to render the page.
:param view_name: The name of the view to use to render tha page.
:param options: Additional options for view generation eg rootsite.
"""
self.query_limit = query_limit
self.user = user
self.view_name = view_name
self.options = options
def match(self, context):
# circular dependencies.
from lp.testing.pages import setupBrowserForUser
with person_logged_in(self.user):
context_url = canonical_url(
context, view_name=self.view_name, **self.options)
browser = setupBrowserForUser(self.user)
collector = QueryCollector()
collector.register()
try:
browser.open(context_url)
counter = HasQueryCount(LessThan(self.query_limit))
# When bug 724691 is fixed, this can become an AnnotateMismatch to
# describe the object being rendered.
return counter.match(collector)
finally:
# Unregister now in case this method is called multiple
# times in a single test.
collector.unregister()
def __str__(self):
return "BrowsesWithQueryLimit(%s, %s)" % (self.query_limit, self.user)
class DoesNotProvide(Mismatch):
"""An object does not provide an interface."""
def __init__(self, obj, interface):
"""Create a DoesNotProvide Mismatch.
:param obj: the object that does not match.
:param interface: the Interface that the object was supposed to match.
"""
self.obj = obj
self.interface = interface
def describe(self):
return "%r does not provide %r." % (self.obj, self.interface)
class DoesNotCorrectlyProvide(DoesNotProvide):
"""An object does not correctly provide an interface."""
def __init__(self, obj, interface, extra=None):
"""Create a DoesNotCorrectlyProvide Mismatch.
:param obj: the object that does not match.
:param interface: the Interface that the object was supposed to match.
:param extra: any extra information about the mismatch as a string,
or None
"""
super(DoesNotCorrectlyProvide, self).__init__(obj, interface)
self.extra = extra
def describe(self):
if self.extra is not None:
extra = ": %s" % self.extra
else:
extra = "."
return ("%r claims to provide %r, but does not do so correctly%s"
% (self.obj, self.interface, extra))
class Provides(Matcher):
"""Test that an object provides a certain interface."""
def __init__(self, interface):
"""Create a Provides Matcher.
:param interface: the Interface that the object should provide.
"""
self.interface = interface
def __str__(self):
return "provides %r." % self.interface
def match(self, matchee):
if not self.interface.providedBy(matchee):
return DoesNotProvide(matchee, self.interface)
passed = True
extra = None
try:
if not verifyObject(self.interface, matchee):
passed = False
except (BrokenImplementation, BrokenMethodImplementation,
DoesNotImplement), e:
passed = False
extra = str(e)
if not passed:
return DoesNotCorrectlyProvide(
matchee, self.interface, extra=extra)
return None
class HasQueryCount(Matcher):
"""Adapt a Binary Matcher to the query count on a QueryCollector.
If there is a mismatch, the queries from the collector are provided as a
test attachment.
"""
def __init__(self, count_matcher):
"""Create a HasQueryCount that will match using count_matcher."""
self.count_matcher = count_matcher
def __str__(self):
return "HasQueryCount(%s)" % self.count_matcher
def match(self, something):
mismatch = self.count_matcher.match(something.count)
if mismatch is None:
return None
return _MismatchedQueryCount(mismatch, something)
class _MismatchedQueryCount(Mismatch):
"""The Mismatch for a HasQueryCount matcher."""
def __init__(self, mismatch, query_collector):
self.count_mismatch = mismatch
self.query_collector = query_collector
def describe(self):
return "queries do not match: %s" % (self.count_mismatch.describe(),)
def get_details(self):
result = []
for query in self.query_collector.queries:
result.append(unicode(query).encode('utf8'))
return {'queries': Content(UTF8_TEXT, lambda: ['\n'.join(result)])}
class IsNotProxied(Mismatch):
"""An object is not proxied."""
def __init__(self, obj):
"""Create an IsNotProxied Mismatch.
:param obj: the object that is not proxied.
"""
self.obj = obj
def describe(self):
return "%r is not proxied." % self.obj
class IsProxied(Matcher):
"""Check that an object is proxied."""
def __str__(self):
return "Is proxied."
def match(self, matchee):
if not builtin_isinstance(matchee, Proxy):
return IsNotProxied(matchee)
return None
class ProvidesAndIsProxied(Matcher):
"""Test that an object implements an interface, and is proxied."""
def __init__(self, interface):
"""Create a ProvidesAndIsProxied matcher.
:param interface: the Interface the object must provide.
"""
self.interface = interface
def __str__(self):
return "Provides %r and is proxied." % self.interface
def match(self, matchee):
mismatch = Provides(self.interface).match(matchee)
if mismatch is not None:
return mismatch
return IsProxied().match(matchee)
class DoesNotContain(Mismatch):
def __init__(self, matchee, expected):
"""Create a DoesNotContain Mismatch.
:param matchee: the string that did not match.
:param expected: the string that `matchee` was expected to contain.
"""
self.matchee = matchee
self.expected = expected
def describe(self):
return "'%s' does not contain '%s'." % (
self.matchee, self.expected)
class Contains(Matcher):
"""Checks whether one string contains another."""
def __init__(self, expected):
"""Create a Contains Matcher.
:param expected: the string that matchees should contain.
"""
self.expected = expected
def __str__(self):
return "Contains '%s'." % self.expected
def match(self, matchee):
if self.expected not in matchee:
return DoesNotContain(matchee, self.expected)
return None
class IsConfiguredBatchNavigator(Matcher):
"""Check that an object is a batch navigator."""
def __init__(self, singular, plural, batch_size=None):
"""Create a ConfiguredBatchNavigator.
:param singular: The singular header the batch should be using.
:param plural: The plural header the batch should be using.
:param batch_size: The batch size that should be configured by
default.
"""
self._single = Equals(singular)
self._plural = Equals(plural)
self._batch = None
if batch_size:
self._batch = Equals(batch_size)
self.matchers = dict(
_singular_heading=self._single, _plural_heading=self._plural)
if self._batch:
self.matchers['default_size'] = self._batch
def __str__(self):
if self._batch:
batch = ", %r" % self._batch.expected
else:
batch = ''
return "ConfiguredBatchNavigator(%r, %r%s)" % (
self._single.expected, self._plural.expected, batch)
def match(self, matchee):
if not isinstance(matchee, BatchNavigator):
# Testtools doesn't have an IsInstanceMismatch yet.
return matchers._BinaryMismatch(
BatchNavigator, 'isinstance', matchee)
mismatches = []
for attrname, matcher in self.matchers.items():
mismatch = matcher.match(getattr(matchee, attrname))
if mismatch is not None:
mismatches.append(mismatch)
if mismatches:
return MismatchesAll(mismatches)
class WasSnapshotted(Mismatch):
def __init__(self, matchee, attribute):
self.matchee = matchee
self.attribute = attribute
def describe(self):
return "Snapshot of %s should not include %s" % (
self.matchee, self.attribute)
class DoesNotSnapshot(Matcher):
"""Checks that certain fields are skipped on Snapshots."""
def __init__(self, attr_list, interface, error_msg=None):
self.attr_list = attr_list
self.interface = interface
self.error_msg = error_msg
def __str__(self):
return "Does not include %s when Snapshot is provided %s." % (
', '.join(self.attr_list), self.interface)
def match(self, matchee):
snapshot = Snapshot(matchee, providing=self.interface)
mismatches = []
for attribute in self.attr_list:
if hasattr(snapshot, attribute):
mismatches.append(WasSnapshotted(matchee, attribute))
if len(mismatches) == 0:
return None
else:
return MismatchesAll(mismatches)
def DocTestMatches(example):
"""See if a string matches a doctest example.
Uses the default doctest flags used across Launchpad.
"""
from lp.testing.systemdocs import default_optionflags
return OriginalDocTestMatches(example, default_optionflags)
class SoupMismatch(Mismatch):
def __init__(self, widget_id, soup_content):
self.widget_id = widget_id
self.soup_content = soup_content
def get_details(self):
return {'content': self.soup_content}
class MissingElement(SoupMismatch):
def describe(self):
return 'No HTML element found with id %r' % self.widget_id
class MultipleElements(SoupMismatch):
def describe(self):
return 'HTML id %r found multiple times in document' % self.widget_id
class MatchesTagText(Matcher):
"""Match against the extracted text of the tag."""
def __init__(self, soup_content, tag_id):
"""Construct the matcher with the soup content."""
self.soup_content = soup_content
self.tag_id = tag_id
def __str__(self):
return "matches widget %r text" % self.tag_id
def match(self, matchee):
# Here to avoid circular dependancies.
from lp.testing.pages import extract_text
widgets = self.soup_content.findAll(id=self.tag_id)
if len(widgets) == 0:
return MissingElement(self.tag_id, self.soup_content)
elif len(widgets) > 1:
return MultipleElements(self.tag_id, self.soup_content)
widget = widgets[0]
text_matcher = DocTestMatches(extract_text(widget))
return text_matcher.match(matchee)
class MatchesPickerText(Matcher):
"""Match against the text in a widget."""
def __init__(self, soup_content, widget_id):
"""Construct the matcher with the soup content."""
self.soup_content = soup_content
self.widget_id = widget_id
def __str__(self):
return "matches widget %r text" % self.widget_id
def match(self, matchee):
# Here to avoid circular dependancies.
from lp.testing.pages import extract_text
widgets = self.soup_content.findAll(id=self.widget_id)
if len(widgets) == 0:
return MissingElement(self.widget_id, self.soup_content)
elif len(widgets) > 1:
return MultipleElements(self.widget_id, self.soup_content)
widget = widgets[0]
text = widget.findAll(attrs={'class': 'yui3-activator-data-box'})[0]
text_matcher = DocTestMatches(extract_text(text))
return text_matcher.match(matchee)
class EqualsIgnoringWhitespace(Equals):
"""Compare equality, ignoring whitespace in strings.
Whitespace in strings is normalized before comparison. All other objects
are compared as they come.
"""
def __init__(self, expected):
if isinstance(expected, (str, unicode)):
expected = normalize_whitespace(expected)
super(EqualsIgnoringWhitespace, self).__init__(expected)
def match(self, observed):
if isinstance(observed, (str, unicode)):
observed = normalize_whitespace(observed)
return super(EqualsIgnoringWhitespace, self).match(observed)
|