Merge lp:~jtv/launchpad/use-fakelibrarian into lp:launchpad

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Graham Binns
Approved revision: no longer in the source branch.
Merged at revision: 11427
Proposed branch: lp:~jtv/launchpad/use-fakelibrarian
Merge into: lp:launchpad
Prerequisite: lp:~jtv/launchpad/fakelibrarian-commit
Diff against target: 223 lines (+43/-36)
1 file modified
lib/lp/translations/utilities/tests/test_file_importer.py (+43/-36)
To merge this branch: bzr merge lp:~jtv/launchpad/use-fakelibrarian
Reviewer Review Type Date Requested Status
Graham Binns (community) code Approve
Robert Collins code Pending
Review via email: mp+33324@code.launchpad.net

Commit message

Convert a test to FakeLibrarian.

Description of the change

= Convert a test to FakeLibrarian =

Requires the lp:~jtv/launchpad/fakelibrarian-commit branch.

The Translations import engine annoyingly relies on the Librarian from the very first point where an upload is picked for processing all the way down into the per-format input parsers. I'm hoping to fix that someday but until that day, here's a branch that converts one of the most badly affected tests to use the FakeLibrarian.

I cleaned up a bunch of lint later, so that may be a little distracting. The meat of the matter is in the transaction.commit calls that have been replaced with self.fakelibrarian.pretendCommit calls. This means that a database commit is no longer needed to make each test pass.

The change speeds up the test by about 20% (based on the shortest times used by the second of two pairs of runs each, to eliminate cache variability as much as possible). The percentage is about equal for setup, which no longer requires the LibrarianLayer; test run time; and overall test time. All numbers as reported by "bin/test -vvv."

The test runs were done with LP_PERSISTENT_TEST_SERVICES enabled. The speedup would be much more dramatic without that. On the other hand, the speedup is obviously less dramatic as a part of a larger test run that's likely to set up LibrarianLayer anyway.

Jeroen

To post a comment you must log in.
Revision history for this message
Graham Binns (gmb) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/translations/utilities/tests/test_file_importer.py'
2--- lib/lp/translations/utilities/tests/test_file_importer.py 2010-08-20 20:31:18 +0000
3+++ lib/lp/translations/utilities/tests/test_file_importer.py 2010-08-22 10:25:01 +0000
4@@ -1,19 +1,17 @@
5-# Copyright 2009 Canonical Ltd. This software is licensed under the
6+# Copyright 2009-2010 Canonical Ltd. This software is licensed under the
7 # GNU Affero General Public License version 3 (see the file LICENSE).
8
9 """Translation File Importer tests."""
10
11 __metaclass__ = type
12
13-import unittest
14-
15-import transaction
16 from zope.component import getUtility
17 from zope.security.proxy import removeSecurityProxy
18
19-from canonical.testing import LaunchpadZopelessLayer
20+from canonical.testing import ZopelessDatabaseLayer
21 from lp.registry.interfaces.person import IPersonSet
22 from lp.testing import TestCaseWithFactory
23+from lp.testing.fakelibrarian import FakeLibrarian
24 from lp.translations.interfaces.translationgroup import TranslationPermission
25 from lp.translations.interfaces.translationimporter import (
26 OutdatedTranslationError,
27@@ -82,7 +80,8 @@
28 #, c-format
29 msgid "%s"
30 msgstr ""
31-''' % (TEST_MSGID_ERROR)
32+''' % TEST_MSGID_ERROR
33+
34
35 TEST_TRANSLATION_FILE_WITH_ERROR = r'''
36 msgid ""
37@@ -95,11 +94,12 @@
38 #, c-format
39 msgid "%s"
40 msgstr "format specifier changes %%s"
41-''' % (TEST_MSGID_ERROR)
42+''' % TEST_MSGID_ERROR
43+
44
45 class FileImporterTestCase(TestCaseWithFactory):
46 """Class test for translation importer component"""
47- layer = LaunchpadZopelessLayer
48+ layer = ZopelessDatabaseLayer
49
50 def _createFileImporters(self, pot_content, po_content, is_published):
51 """Create queue entries from POT and PO content strings.
52@@ -119,9 +119,8 @@
53 is_published, self.importer_person,
54 productseries=potemplate.productseries,
55 potemplate=potemplate)
56- transaction.commit()
57- return POTFileImporter(
58- template_entry, GettextPOImporter(), None )
59+ self.fake_librarian.pretendCommit()
60+ return POTFileImporter(template_entry, GettextPOImporter(), None)
61
62 def _createPOFileImporter(self,
63 pot_importer, po_content, is_published, existing_pofile=None,
64@@ -138,9 +137,8 @@
65 translation_entry = self.translation_import_queue.addOrUpdateEntry(
66 pofile.path, po_content, is_published, person,
67 productseries=potemplate.productseries, pofile=pofile)
68- transaction.commit()
69- return POFileImporter(
70- translation_entry, GettextPOImporter(), None )
71+ self.fake_librarian.pretendCommit()
72+ return POFileImporter(translation_entry, GettextPOImporter(), None)
73
74 def _createImporterForExportedEntries(self):
75 """Set up entries that where exported from LP, i.e. that contain the
76@@ -164,15 +162,20 @@
77 False, self.importer_person,
78 productseries=potemplate.productseries,
79 potemplate=potemplate)
80- transaction.commit()
81- return FileImporter(
82- template_entry, GettextPOImporter(), None )
83+ self.fake_librarian.pretendCommit()
84+ return FileImporter(template_entry, GettextPOImporter(), None)
85
86 def setUp(self):
87 super(FileImporterTestCase, self).setUp()
88+ self.fake_librarian = FakeLibrarian()
89+ self.fake_librarian.installAsLibrarian()
90 self.translation_import_queue = getUtility(ITranslationImportQueue)
91 self.importer_person = self.factory.makePerson()
92
93+ def tearDown(self):
94+ self.fake_librarian.uninstall()
95+ super(FileImporterTestCase, self).tearDown()
96+
97 def test_FileImporter_importMessage_NotImplemented(self):
98 importer = self._createFileImporter()
99 self.failUnlessRaises(NotImplementedError,
100@@ -241,7 +244,7 @@
101 product.translationpermission = TranslationPermission.CLOSED
102 product.translationgroup = self.factory.makeTranslationGroup(
103 self.importer_person)
104- transaction.commit()
105+ self.fake_librarian.pretendCommit()
106
107 # Get one POTMsgSet to do storeTranslationsInDatabase on.
108 message = pot_importer.translation_file.messages[0]
109@@ -280,7 +283,7 @@
110 "POTFileImporter had no reference to an IPOTemplate.")
111 self.failUnless(pot_importer.pofile is None or
112 pot_importer.pofile.language == "en",
113- "POTFileImporter referenced an IPOFile which was not English." )
114+ "POTFileImporter referenced an IPOFile which was not English.")
115 # Test if POFileImporter gets initialised correctly.
116 self.failUnless(po_importer.potemplate is not None,
117 "POTFileImporter had no reference to an IPOTemplate.")
118@@ -340,9 +343,9 @@
119 # Test correct import operation for both
120 # exported and published files.
121 importers = (
122- self._createImporterForExportedEntries(),
123- self._createImporterForPublishedEntries()
124- )
125+ self._createImporterForExportedEntries(),
126+ self._createImporterForPublishedEntries(),
127+ )
128 for (pot_importer, po_importer) in importers:
129 # Run the import and see if PotMsgSet and TranslationMessage
130 # entries are correctly created in the DB.
131@@ -385,7 +388,7 @@
132 self.failUnlessEqual(len(errors), 0,
133 "POFileImporter.importFile returned errors where there should "
134 "be none.")
135- transaction.commit()
136+ self.fake_librarian.pretendCommit()
137
138 # Create new POFileImporter with an earlier timestamp and
139 # a different translation (msgstr).
140@@ -397,7 +400,8 @@
141 self.failUnlessEqual(len(errors), 1,
142 "No error detected when importing a pofile with an earlier "
143 "export timestamp (update conflict).")
144- self.failUnless( errors[0]['error-message'].find(
145+ self.failUnless(
146+ errors[0]['error-message'].find(
147 u"updated by someone else after you") != -1,
148 "importFile() failed to detect a message update conflict.")
149
150@@ -441,7 +445,7 @@
151 TEST_TRANSLATION_FILE_WITH_ERROR, False)
152 pot_importer.importFile()
153 po_importer.importFile()
154- transaction.commit()
155+ self.fake_librarian.pretendCommit()
156
157 po_importer2 = self._createPOFileImporter(
158 pot_importer, TEST_TRANSLATION_EXPORTED_EARLIER, False,
159@@ -477,7 +481,7 @@
160 "Last-Translator: Hector Atlas <??@??.??>\\n"
161 "Content-Type: text/plain; charset=UTF-8\\n"
162 "X-Launchpad-Export-Date: 2008-11-05 13:31+0000\\n"
163-
164+
165 msgid "%s"
166 msgstr "Dankuwel"
167 """ % TEST_MSGID
168@@ -493,13 +497,19 @@
169
170 class CreateFileImporterTestCase(TestCaseWithFactory):
171 """Class test for translation importer creation."""
172- layer = LaunchpadZopelessLayer
173+ layer = ZopelessDatabaseLayer
174
175 def setUp(self):
176 super(CreateFileImporterTestCase, self).setUp()
177+ self.fake_librarian = FakeLibrarian()
178+ self.fake_librarian.installAsLibrarian()
179 self.translation_import_queue = getUtility(ITranslationImportQueue)
180 self.importer_person = self.factory.makePerson()
181
182+ def tearDown(self):
183+ self.fake_librarian.uninstall()
184+ super(CreateFileImporterTestCase, self).tearDown()
185+
186 def _make_queue_entry(self, is_published):
187 pofile = self.factory.makePOFile('eo')
188 # Create a header with a newer date than what is found in
189@@ -510,18 +520,19 @@
190 queue_entry = self.translation_import_queue.addOrUpdateEntry(
191 pofile.path, po_content, is_published, self.importer_person,
192 productseries=pofile.potemplate.productseries, pofile=pofile)
193- transaction.commit()
194+ self.fake_librarian.pretendCommit()
195 return queue_entry
196
197 def test_raises_OutdatedTranslationError_on_user_uploads(self):
198 queue_entry = self._make_queue_entry(False)
199- self.assertRaises(OutdatedTranslationError, POFileImporter,
200- queue_entry, GettextPOImporter(), None )
201+ self.assertRaises(
202+ OutdatedTranslationError,
203+ POFileImporter, queue_entry, GettextPOImporter(), None)
204
205 def test_not_raises_OutdatedTranslationError_on_published_uploads(self):
206 queue_entry = self._make_queue_entry(True)
207 try:
208- importer = POFileImporter(queue_entry, GettextPOImporter(), None )
209+ importer = POFileImporter(queue_entry, GettextPOImporter(), None)
210 except OutdatedTranslationError:
211 self.fail("OutdatedTranslationError raised.")
212
213@@ -529,9 +540,5 @@
214 queue_entry = self._make_queue_entry(True)
215 pofile = queue_entry.pofile
216 old_raw_header = pofile.header
217- importer = POFileImporter(queue_entry, GettextPOImporter(), None )
218+ importer = POFileImporter(queue_entry, GettextPOImporter(), None)
219 self.assertEqual(old_raw_header, pofile.header)
220-
221-
222-def test_suite():
223- return unittest.TestLoader().loadTestsFromName(__name__)