Merge lp:~lifeless/launchpad/fakelibrarian into lp:launchpad

Proposed by Robert Collins
Status: Merged
Approved by: Michael Hudson-Doyle
Approved revision: no longer in the source branch.
Merged at revision: 11575
Proposed branch: lp:~lifeless/launchpad/fakelibrarian
Merge into: lp:launchpad
Prerequisite: lp:~lifeless/launchpad/test
Diff against target: 132 lines (+16/-57)
3 files modified
lib/lp/testing/fakelibrarian.py (+13/-54)
lib/lp/testing/tests/test_fakelibrarian.py (+1/-1)
lib/lp/translations/utilities/tests/test_file_importer.py (+2/-2)
To merge this branch: bzr merge lp:~lifeless/launchpad/fakelibrarian
Reviewer Review Type Date Requested Status
Michael Hudson-Doyle Approve
Review via email: mp+35918@code.launchpad.net

Commit message

Port the fake librarian to fixtures.

Description of the change

Port the fake librarian to fixtures.

To post a comment you must log in.
Revision history for this message
Michael Hudson-Doyle (mwhudson) wrote :

Somehow I managed to convince myself that this branch was a bit complicated, but actually it seems to be a nice simplification. Cool :-)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/lp/testing/fakelibrarian.py'
--- lib/lp/testing/fakelibrarian.py 2010-09-09 10:40:17 +0000
+++ lib/lp/testing/fakelibrarian.py 2010-09-18 09:29:59 +0000
@@ -19,6 +19,7 @@
19from StringIO import StringIO19from StringIO import StringIO
20from urlparse import urljoin20from urlparse import urljoin
2121
22from fixtures import Fixture
22import transaction23import transaction
23from transaction.interfaces import ISynchronizer24from transaction.interfaces import ISynchronizer
24import zope.component25import zope.component
@@ -58,8 +59,8 @@
58 return self._datafile.read(chunksize)59 return self._datafile.read(chunksize)
5960
6061
61class FakeLibrarian(object):62class FakeLibrarian(Fixture):
62 """A fake, in-process Librarian.63 """A test double Librarian which works in-process.
6364
64 This takes the role of both the librarian client and the LibraryFileAlias65 This takes the role of both the librarian client and the LibraryFileAlias
65 utility.66 utility.
@@ -67,65 +68,23 @@
67 provided_utilities = [ILibrarianClient, ILibraryFileAliasSet]68 provided_utilities = [ILibrarianClient, ILibraryFileAliasSet]
68 implements(ISynchronizer, *provided_utilities)69 implements(ISynchronizer, *provided_utilities)
6970
70 installed_as_librarian = False71 def setUp(self):
7172 """Fixture API: install as the librarian."""
72 def installAsLibrarian(self):73 Fixture.setUp(self)
73 """Install this `FakeLibrarian` as the default Librarian."""74 self.aliases = {}
74 if self.installed_as_librarian:75 self.download_url = config.librarian.download_url
75 return
76
77 transaction.manager.registerSynch(self)76 transaction.manager.registerSynch(self)
7877 self.addCleanup(transaction.manager.unregisterSynch, self)
79 # Original utilities that need to be restored.
80 self.original_utilities = {}
8178
82 site_manager = zope.component.getGlobalSiteManager()79 site_manager = zope.component.getGlobalSiteManager()
83 for utility in self.provided_utilities:80 for utility in self.provided_utilities:
84 original = zope.component.getUtility(utility)81 original = zope.component.getUtility(utility)
85 if site_manager.unregisterUtility(original, utility):82 if site_manager.unregisterUtility(original, utility):
86 # We really disabled a utility, so remember to restore83 # We really disabled a utility, restore it later.
87 # it later. (Alternatively, the utility object might84 self.addCleanup(
88 # implement an interface that extends the utility one,85 zope.component.provideUtility, original, utility)
89 # in which case we should not restore it.)
90 self.original_utilities[utility] = original
91 zope.component.provideUtility(self, utility)86 zope.component.provideUtility(self, utility)
9287 self.addCleanup(site_manager.unregisterUtility, self, utility)
93 self.installed_as_librarian = True
94
95 def uninstall(self):
96 """Un-install this `FakeLibrarian` as the default Librarian."""
97 if not self.installed_as_librarian:
98 return
99
100 transaction.manager.unregisterSynch(self)
101
102 site_manager = zope.component.getGlobalSiteManager()
103 for utility in reversed(self.provided_utilities):
104 site_manager.unregisterUtility(self, utility)
105 original_utility = self.original_utilities.get(utility)
106 if original_utility is not None:
107 # We disabled a utility to get here; restore the
108 # original. We do not do this for utilities that were
109 # implemented through interface inheritance, because in
110 # that case we would never have unregistered anything in
111 # the first place. Re-registering would register the
112 # same object twice, for related but different
113 # interfaces.
114 zope.component.provideUtility(original_utility, utility)
115
116 self.installed_as_librarian = False
117
118 def setUp(self):
119 """Fixture API: install as the librarian."""
120 self.installAsLibrarian()
121
122 def tearDown(self):
123 """Fixture API: uninstall."""
124 self.uninstall()
125
126 def __init__(self):
127 self.aliases = {}
128 self.download_url = config.librarian.download_url
12988
130 def addFile(self, name, size, file, contentType, expires=None):89 def addFile(self, name, size, file, contentType, expires=None):
131 """See `IFileUploadClient`."""90 """See `IFileUploadClient`."""
13291
=== modified file 'lib/lp/testing/tests/test_fakelibrarian.py'
--- lib/lp/testing/tests/test_fakelibrarian.py 2010-09-09 10:04:28 +0000
+++ lib/lp/testing/tests/test_fakelibrarian.py 2010-09-18 09:29:59 +0000
@@ -143,7 +143,7 @@
143143
144 def setUp(self):144 def setUp(self):
145 super(TestFakeLibrarian, self).setUp()145 super(TestFakeLibrarian, self).setUp()
146 self.fake_librarian = self.installFixture(FakeLibrarian())146 self.fake_librarian = self.useFixture(FakeLibrarian())
147147
148 def test_fake(self):148 def test_fake(self):
149 self.assertTrue(verifyObject(ISynchronizer, self.fake_librarian))149 self.assertTrue(verifyObject(ISynchronizer, self.fake_librarian))
150150
=== modified file 'lib/lp/translations/utilities/tests/test_file_importer.py'
--- lib/lp/translations/utilities/tests/test_file_importer.py 2010-09-03 14:11:56 +0000
+++ lib/lp/translations/utilities/tests/test_file_importer.py 2010-09-18 09:29:59 +0000
@@ -169,7 +169,7 @@
169169
170 def setUp(self):170 def setUp(self):
171 super(FileImporterTestCase, self).setUp()171 super(FileImporterTestCase, self).setUp()
172 self.fake_librarian = self.installFixture(FakeLibrarian())172 self.fake_librarian = self.useFixture(FakeLibrarian())
173 self.translation_import_queue = getUtility(ITranslationImportQueue)173 self.translation_import_queue = getUtility(ITranslationImportQueue)
174 self.importer_person = self.factory.makePerson()174 self.importer_person = self.factory.makePerson()
175175
@@ -526,7 +526,7 @@
526526
527 def setUp(self):527 def setUp(self):
528 super(CreateFileImporterTestCase, self).setUp()528 super(CreateFileImporterTestCase, self).setUp()
529 self.fake_librarian = self.installFixture(FakeLibrarian())529 self.fake_librarian = self.useFixture(FakeLibrarian())
530 self.translation_import_queue = getUtility(ITranslationImportQueue)530 self.translation_import_queue = getUtility(ITranslationImportQueue)
531 self.importer_person = self.factory.makePerson()531 self.importer_person = self.factory.makePerson()
532532