Merge ~cjwatson/launchpad:remove-zope.app.testing.ztapi into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: acba60284e806f924bfa423a6cb0b16d8bf0e25b
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:remove-zope.app.testing.ztapi
Merge into: launchpad:master
Prerequisite: ~cjwatson/launchpad:remove-test-event-listener
Diff against target: 352 lines (+73/-48)
7 files modified
lib/lp/app/stories/basics/xx-developerexceptions.txt (+10/-3)
lib/lp/app/stories/basics/xx-opstats.txt (+9/-2)
lib/lp/services/webapp/doc/menus.txt (+9/-2)
lib/lp/services/webapp/tests/test_authorization.py (+17/-25)
lib/lp/services/webapp/tests/test_authutility.py (+11/-11)
lib/lp/services/webapp/tests/test_error.py (+6/-2)
lib/lp/testing/__init__.py (+11/-3)
Reviewer Review Type Date Requested Status
Ioana Lasc (community) Approve
Review via email: mp+374630@code.launchpad.net

Commit message

Remove all uses of zope.app.testing.ztapi

We want to move away from using zope.app.testing. Some parts of this
are complicated, but ztapi can be replaced using the various
Zope*Fixture classes with just some rearrangement of arguments, and the
fixtures are easier to use in Launchpad anyway.

To post a comment you must log in.
Revision history for this message
Ioana Lasc (ilasc) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/app/stories/basics/xx-developerexceptions.txt b/lib/lp/app/stories/basics/xx-developerexceptions.txt
2index d0ec3e2..a63c448 100644
3--- a/lib/lp/app/stories/basics/xx-developerexceptions.txt
4+++ b/lib/lp/app/stories/basics/xx-developerexceptions.txt
5@@ -6,13 +6,18 @@ occurs. Other users get no traceback and only the OOPS ID.
6 To be able to test, a page that generates HTTP 500 errors is registered.
7 Accessing that page gives us the OOPS error page.
8
9- >>> from zope.app.testing import ztapi
10+ >>> from zope.interface import Interface
11+ >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
12+ >>> from lp.testing.fixture import ZopeAdapterFixture
13+
14 >>> class ErrorView(object):
15 ... """A broken view"""
16 ... def __call__(self, *args, **kw):
17 ... raise Exception('Oops')
18 ...
19- >>> ztapi.browserView(None, "error-test", ErrorView)
20+ >>> error_view_fixture = ZopeAdapterFixture(
21+ ... ErrorView, (None, IDefaultBrowserLayer), Interface, "error-test")
22+ >>> error_view_fixture.setUp()
23
24 As our test runner runs in 'always show tracebacks' mode, we need to
25 switch this off for these tests to work
26@@ -82,11 +87,13 @@ And the OOPS ID is displayed but not linkified.
27 <code class="oopsid">OOPS-...</code>)
28 ...
29
30-To avoid affecting other tests, reset the show_tracebacks config item.
31+To avoid affecting other tests, reset the show_tracebacks config item and
32+unregister the adapter.
33
34 >>> test_config_data = config.pop('test_data')
35 >>> config.canonical.show_tracebacks
36 True
37+ >>> error_view_fixture.cleanUp()
38
39
40 = HTTPCaller handle_errors =
41diff --git a/lib/lp/app/stories/basics/xx-opstats.txt b/lib/lp/app/stories/basics/xx-opstats.txt
42index 897ae19..47e7780 100644
43--- a/lib/lp/app/stories/basics/xx-opstats.txt
44+++ b/lib/lp/app/stories/basics/xx-opstats.txt
45@@ -98,13 +98,18 @@ may also include the odd case where the OOPS system has failed and a
46 fallback error page is rendered by Zope3. There doesn't seem to be any
47 particular need to differentiate these cases though:
48
49- >>> from zope.app.testing import ztapi
50+ >>> from zope.interface import Interface
51+ >>> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
52+ >>> from lp.testing.fixture import ZopeAdapterFixture
53+
54 >>> class ErrorView(object):
55 ... """A broken view"""
56 ... def __call__(self, *args, **kw):
57 ... raise Exception('Oops')
58 ...
59- >>> ztapi.browserView(None, "error-test", ErrorView)
60+ >>> error_view_fixture = ZopeAdapterFixture(
61+ ... ErrorView, (None, IDefaultBrowserLayer), Interface, "error-test")
62+ >>> error_view_fixture.setUp()
63 >>> output = http("GET /error-test HTTP/1.1\nHost: launchpad.test\n")
64 >>> output.getStatus()
65 500
66@@ -134,6 +139,8 @@ the site.
67 http requests: 1
68 requests: 1
69
70+ >>> error_view_fixture.cleanUp()
71+
72 Number of XML-RPC Faults
73 ------------------------
74
75diff --git a/lib/lp/services/webapp/doc/menus.txt b/lib/lp/services/webapp/doc/menus.txt
76index d79969c..d29f08e 100644
77--- a/lib/lp/services/webapp/doc/menus.txt
78+++ b/lib/lp/services/webapp/doc/menus.txt
79@@ -768,11 +768,16 @@ object. Let's say that the default view name for an IStreet is '+baz'.
80 This is the common case where the overview link is the default view
81 name.
82
83+ >>> from zope.publisher.interfaces import IDefaultViewName
84+ >>> from zope.publisher.interfaces.browser import IBrowserRequest
85+ >>> from lp.testing.fixture import ZopeAdapterFixture
86+
87 >>> class IStreet(Interface):
88 ... """A street."""
89 >>> directlyProvides(street, IStreet, IThingHavingFacets)
90- >>> from zope.app.testing import ztapi
91- >>> ztapi.setDefaultViewName(IStreet, '+baz', None)
92+ >>> street_default_view_fixture = ZopeAdapterFixture(
93+ ... '+baz', (IStreet, IBrowserRequest), IDefaultViewName)
94+ >>> street_default_view_fixture.setUp()
95
96 >>> request = FakeRequest(
97 ... 'http://launchpad.test/sesamestreet/+baz',
98@@ -795,6 +800,8 @@ infrastructure actually calculates a shortened URL for this case.
99 http://launchpad.test/sesamestreet/+bar True
100 http://launchpad.test/sesamestreet False
101
102+ >>> street_default_view_fixture.cleanUp()
103+
104 You can traverse to an individual menu item from the facet menu:
105
106 >>> view = LaunchpadView(house, request)
107diff --git a/lib/lp/services/webapp/tests/test_authorization.py b/lib/lp/services/webapp/tests/test_authorization.py
108index ee63806..5f5db72 100644
109--- a/lib/lp/services/webapp/tests/test_authorization.py
110+++ b/lib/lp/services/webapp/tests/test_authorization.py
111@@ -10,18 +10,12 @@ from random import getrandbits
112 import StringIO
113
114 import transaction
115-from zope.app.testing import ztapi
116-from zope.component import (
117- provideAdapter,
118- provideUtility,
119- )
120 from zope.interface import (
121 implementer,
122 Interface,
123 provider,
124 )
125 from zope.security.interfaces import Unauthorized
126-import zope.testing.cleanup
127
128 from lp.app.interfaces.security import IAuthorization
129 from lp.app.security import AuthorizationBase
130@@ -61,7 +55,10 @@ from lp.testing import (
131 TestCase,
132 )
133 from lp.testing.factory import ObjectFactory
134-from lp.testing.fixture import ZopeAdapterFixture
135+from lp.testing.fixture import (
136+ ZopeAdapterFixture,
137+ ZopeUtilityFixture,
138+ )
139 from lp.testing.layers import (
140 DatabaseFunctionalLayer,
141 ZopelessLayer,
142@@ -226,11 +223,9 @@ class TestCheckPermissionCaching(TestCase):
143
144 def setUp(self):
145 """Register a new permission and a fake store selector."""
146- zope.testing.cleanup.cleanUp()
147 super(TestCheckPermissionCaching, self).setUp()
148 self.factory = ObjectFactory()
149- provideUtility(FakeStoreSelector, IStoreSelector)
150- self.addCleanup(zope.testing.cleanup.cleanUp)
151+ self.useFixture(ZopeUtilityFixture(FakeStoreSelector, IStoreSelector))
152
153 def makeRequest(self):
154 """Construct an arbitrary `LaunchpadBrowserRequest` object."""
155@@ -246,11 +241,11 @@ class TestCheckPermissionCaching(TestCase):
156 `Checker` created by ``checker_factory``.
157 """
158 permission = self.factory.getUniqueString()
159- provideUtility(
160- PermissionAccessLevel(), ILaunchpadPermission, permission)
161+ self.useFixture(ZopeUtilityFixture(
162+ PermissionAccessLevel(), ILaunchpadPermission, permission))
163 checker_factory = CheckerFactory()
164- provideAdapter(
165- checker_factory, [Object], IAuthorization, name=permission)
166+ self.useFixture(ZopeAdapterFixture(
167+ checker_factory, [Object], IAuthorization, name=permission))
168 return Object(), permission, checker_factory
169
170 def test_checkPermission_cache_unauthenticated(self):
171@@ -386,9 +381,8 @@ class TestCheckPermissionCaching(TestCase):
172 # checkUnauthenticatedPermission caches the result of
173 # checkUnauthenticated for a particular object and permission.
174 # We set a principal to ensure that it is not used even if set.
175- provideUtility(PlacelessAuthUtility(), IPlacelessAuthUtility)
176- zope.testing.cleanup.addCleanUp(
177- ztapi.unprovideUtility, (IPlacelessAuthUtility,))
178+ self.useFixture(ZopeUtilityFixture(
179+ PlacelessAuthUtility(), IPlacelessAuthUtility))
180 principal = FakeLaunchpadPrincipal()
181 request = self.makeRequest()
182 request.setPrincipal(principal)
183@@ -409,9 +403,8 @@ class TestCheckPermissionCaching(TestCase):
184 def test_checkUnauthenticatedPermission_commit_clears_cache(self):
185 # Committing a transaction clears the cache.
186 # We set a principal to ensure that it is not used even if set.
187- provideUtility(PlacelessAuthUtility(), IPlacelessAuthUtility)
188- zope.testing.cleanup.addCleanUp(
189- ztapi.unprovideUtility, (IPlacelessAuthUtility,))
190+ self.useFixture(ZopeUtilityFixture(
191+ PlacelessAuthUtility(), IPlacelessAuthUtility))
192 principal = FakeLaunchpadPrincipal()
193 request = self.makeRequest()
194 request.setPrincipal(principal)
195@@ -435,16 +428,15 @@ class TestCheckPermissionCaching(TestCase):
196 class TestLaunchpadSecurityPolicy_getPrincipalsAccessLevel(TestCase):
197
198 def setUp(self):
199- zope.testing.cleanup.cleanUp()
200 cls = TestLaunchpadSecurityPolicy_getPrincipalsAccessLevel
201 super(cls, self).setUp()
202 self.principal = LaunchpadPrincipal(
203 'foo.bar@canonical.com', 'foo', 'foo', object())
204 self.security = LaunchpadSecurityPolicy()
205- provideAdapter(
206- adapt_loneobject_to_container, [ILoneObject], ILaunchpadContainer)
207- provideAdapter(LoneObjectURL, [ILoneObject], ICanonicalUrlData)
208- self.addCleanup(zope.testing.cleanup.cleanUp)
209+ self.useFixture(ZopeAdapterFixture(
210+ adapt_loneobject_to_container, [ILoneObject], ILaunchpadContainer))
211+ self.useFixture(ZopeAdapterFixture(
212+ LoneObjectURL, [ILoneObject], ICanonicalUrlData))
213
214 def test_no_scope(self):
215 """Principal's access level is used when no scope is given."""
216diff --git a/lib/lp/services/webapp/tests/test_authutility.py b/lib/lp/services/webapp/tests/test_authutility.py
217index c6b5b91..d2dfcbc 100644
218--- a/lib/lp/services/webapp/tests/test_authutility.py
219+++ b/lib/lp/services/webapp/tests/test_authutility.py
220@@ -6,14 +6,9 @@ __metaclass__ = type
221 import base64
222
223 import testtools
224-from zope.app.testing import ztapi
225 from zope.app.testing.placelesssetup import PlacelessSetup
226 from zope.authentication.interfaces import ILoginPassword
227-from zope.component import (
228- getUtility,
229- provideAdapter,
230- provideUtility,
231- )
232+from zope.component import getUtility
233 from zope.interface import implementer
234 from zope.principalregistry.principalregistry import UnauthenticatedPrincipal
235 from zope.publisher.browser import TestRequest
236@@ -31,6 +26,10 @@ from lp.services.webapp.interfaces import (
237 IPlacelessAuthUtility,
238 IPlacelessLoginSource,
239 )
240+from lp.testing.fixture import (
241+ ZopeAdapterFixture,
242+ ZopeUtilityFixture,
243+ )
244
245
246 @implementer(IPerson)
247@@ -64,13 +63,14 @@ class TestPlacelessAuth(PlacelessSetup, testtools.TestCase):
248 def setUp(self):
249 testtools.TestCase.setUp(self)
250 PlacelessSetup.setUp(self)
251- provideUtility(DummyPlacelessLoginSource(), IPlacelessLoginSource)
252- provideUtility(PlacelessAuthUtility(), IPlacelessAuthUtility)
253- provideAdapter(BasicAuthAdapter, (IHTTPCredentials,), ILoginPassword)
254+ self.useFixture(ZopeUtilityFixture(
255+ DummyPlacelessLoginSource(), IPlacelessLoginSource))
256+ self.useFixture(ZopeUtilityFixture(
257+ PlacelessAuthUtility(), IPlacelessAuthUtility))
258+ self.useFixture(ZopeAdapterFixture(
259+ BasicAuthAdapter, (IHTTPCredentials,), ILoginPassword))
260
261 def tearDown(self):
262- ztapi.unprovideUtility(IPlacelessLoginSource)
263- ztapi.unprovideUtility(IPlacelessAuthUtility)
264 PlacelessSetup.tearDown(self)
265 testtools.TestCase.tearDown(self)
266
267diff --git a/lib/lp/services/webapp/tests/test_error.py b/lib/lp/services/webapp/tests/test_error.py
268index b9b9f28..7ad4e83 100644
269--- a/lib/lp/services/webapp/tests/test_error.py
270+++ b/lib/lp/services/webapp/tests/test_error.py
271@@ -24,7 +24,8 @@ from testtools.matchers import (
272 MatchesListwise,
273 )
274 import transaction
275-from zope.app.testing import ztapi
276+from zope.interface import Interface
277+from zope.publisher.interfaces.browser import IDefaultBrowserLayer
278
279 from lp.services.webapp.error import (
280 DisconnectionErrorView,
281@@ -37,6 +38,7 @@ from lp.testing.fixture import (
282 CaptureOops,
283 PGBouncerFixture,
284 Urllib2Fixture,
285+ ZopeAdapterFixture,
286 )
287 from lp.testing.layers import (
288 DatabaseLayer,
289@@ -234,7 +236,9 @@ class TestDatabaseErrorViews(TestCase):
290 """A view that raises an OperationalError"""
291 def __call__(self, *args, **kw):
292 raise OperationalError()
293- ztapi.browserView(None, "error-test", BrokenView())
294+ self.useFixture(ZopeAdapterFixture(
295+ BrokenView(), (None, IDefaultBrowserLayer), Interface,
296+ "error-test"))
297
298 url = 'http://launchpad.test/error-test'
299 error = self.getHTTPError(url)
300diff --git a/lib/lp/testing/__init__.py b/lib/lp/testing/__init__.py
301index 1aa11ea..6a0d9b2 100644
302--- a/lib/lp/testing/__init__.py
303+++ b/lib/lp/testing/__init__.py
304@@ -107,7 +107,6 @@ from testtools.matchers import (
305 )
306 from testtools.testcase import ExpectedException as TTExpectedException
307 import transaction
308-from zope.app.testing import ztapi
309 from zope.component import (
310 ComponentLookupError,
311 getMultiAdapter,
312@@ -185,7 +184,10 @@ from lp.testing._webservice import (
313 oauth_access_token_for,
314 )
315 from lp.testing.dbuser import switch_dbuser
316-from lp.testing.fixture import CaptureOops
317+from lp.testing.fixture import (
318+ CaptureOops,
319+ ZopeEventHandlerFixture,
320+ )
321 from lp.testing.karma import KarmaRecorder
322 from lp.testing.mail_helpers import pop_notifications
323
324@@ -390,6 +392,7 @@ class RequestTimelineCollector:
325 self._active = False
326 self.count = None
327 self.queries = None
328+ self._event_fixture = None
329
330 def register(self):
331 """Start counting queries.
332@@ -398,7 +401,9 @@ class RequestTimelineCollector:
333
334 After each web request the count and queries attributes are updated.
335 """
336- ztapi.subscribe((IEndRequestEvent, ), None, self)
337+ self._event_fixture = ZopeEventHandlerFixture(
338+ self, (IEndRequestEvent, ))
339+ self._event_fixture.setUp()
340 self._active = True
341
342 def __enter__(self):
343@@ -412,6 +417,9 @@ class RequestTimelineCollector:
344
345 def unregister(self):
346 self._active = False
347+ if self._event_fixture is not None:
348+ self._event_fixture.cleanUp()
349+ self._event_fixture = None
350
351 def __exit__(self, exc_type, exc_value, traceback):
352 self.unregister()

Subscribers

People subscribed via source and target branches

to status/vote changes: