~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/testing/matchers.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-02-28 13:28:27 UTC
  • mfrom: (12457.2.10 bug-710685)
  • Revision ID: launchpad@pqm.canonical.com-20110228132827-xfl9awkbwyf46e14
[r=stevenk,
        stub][bug=710685] Various bits combining to reduce late evaluation
        for Product and Distro bugtasks on Branch:+index.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
__metaclass__ = type
5
5
__all__ = [
 
6
    'BrowsesWithQueryLimit',
6
7
    'Contains',
7
8
    'DocTestMatches',
8
9
    'DoesNotCorrectlyProvide',
24
25
from testtools.matchers import (
25
26
    Equals,
26
27
    DocTestMatches as OriginalDocTestMatches,
 
28
    LessThan,
27
29
    Matcher,
28
30
    Mismatch,
29
31
    MismatchesAll,
40
42
    Proxy,
41
43
    )
42
44
 
 
45
from canonical.launchpad.webapp import canonical_url
43
46
from canonical.launchpad.webapp.batching import BatchNavigator
 
47
from lp.testing._login import person_logged_in
 
48
from lp.testing._webservice import QueryCollector
 
49
 
 
50
 
 
51
class BrowsesWithQueryLimit(Matcher):
 
52
    """Matches the rendering of an objects default view with a query limit.
 
53
    
 
54
    This is a wrapper for HasQueryCount which does the heavy lifting on the
 
55
    query comparison - BrowsesWithQueryLimit simply provides convenient
 
56
    glue to use a userbrowser and view an object.
 
57
    """
 
58
 
 
59
    def __init__(self, query_limit, user):
 
60
        """Create a BrowsesWithQueryLimit checking for limit query_limit.
 
61
        
 
62
        :param query_limit: The number of queries permited for the page.
 
63
        :param user: The user to use to render the page.
 
64
        """    
 
65
        self.query_limit = query_limit
 
66
        self.user = user
 
67
 
 
68
    def match(self, context):
 
69
        # circular dependencies.
 
70
        from canonical.launchpad.testing.pages import setupBrowserForUser
 
71
        with person_logged_in(self.user):
 
72
            context_url = canonical_url(context)
 
73
        browser = setupBrowserForUser(self.user)
 
74
        collector = QueryCollector()
 
75
        collector.register()
 
76
        try:
 
77
            browser.open(context_url)
 
78
            counter = HasQueryCount(LessThan(self.query_limit))
 
79
            # When bug 724691 is fixed, this can become an AnnotateMismatch to
 
80
            # describe the object being rendered.
 
81
            return counter.match(collector)
 
82
        finally:
 
83
            # Unregister now in case this method is called multiple
 
84
            # times in a single test.
 
85
            collector.unregister()
 
86
 
 
87
    def __str__(self):
 
88
        return "BrowsesWithQueryLimit(%s, %s)" % (self.query_limit, self.user)
44
89
 
45
90
 
46
91
class DoesNotProvide(Mismatch):