~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Launchpad Patch Queue Manager
  • Date: 2010-08-05 05:54:59 UTC
  • mfrom: (11277.3.3 registry)
  • Revision ID: launchpad@pqm.canonical.com-20100805055459-ogz4pioghjrkog1a
[r=mwhudson][ui=none] Factor out QueryCounter from the rosetta tests
        for reuse and cleanup a couple of registry tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
 
15
15
import transaction
 
16
from zope.app.publication.interfaces import IEndRequestEvent
 
17
from zope.app.testing import ztapi
16
18
from zope.component import getUtility
 
19
 
17
20
from launchpadlib.credentials import AccessToken, Credentials
18
21
from launchpadlib.launchpad import Launchpad
19
22
 
 
23
from canonical.launchpad.webapp.adapter import get_request_statements
20
24
from canonical.launchpad.webapp.interaction import ANONYMOUS
21
25
from canonical.launchpad.webapp.interfaces import OAuthPermission
22
26
from canonical.launchpad.interfaces import (
122
126
    transaction.commit()
123
127
    version = version or Launchpad.DEFAULT_VERSION
124
128
    return Launchpad(credentials, service_root, version=version)
 
129
 
 
130
 
 
131
class QueryCollector:
 
132
    """Collect database calls made in web requests.
 
133
 
 
134
    These are only retrievable at the end of a request, and for tests it is
 
135
    useful to be able to make aassertions about the calls made during a request
 
136
    : this class provides a tool to gather them in a simple fashion.
 
137
 
 
138
    :ivar count: The count of db queries the last web request made.
 
139
    :ivar queries: The list of queries made. See
 
140
        canonical.launchpad.webapp.adapter.get_request_statements for more
 
141
        information.
 
142
    """
 
143
 
 
144
    def __init__(self):
 
145
        self._active = False
 
146
        self.count = None
 
147
        self.queries = None
 
148
 
 
149
    def register(self):
 
150
        """Start counting queries.
 
151
        
 
152
        Be sure to call unregister when finished with the collector.
 
153
 
 
154
        After each web request the count and queries attributes are updated.
 
155
        """
 
156
        ztapi.subscribe((IEndRequestEvent, ), None, self)
 
157
        self._active = True
 
158
 
 
159
    def __call__(self, event):
 
160
        if self._active:
 
161
            self.queries = get_request_statements()
 
162
            self.count = len(self.queries)
 
163
 
 
164
    def unregister(self):
 
165
        self._active = False
 
166
 
 
167