~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/translations/utilities/tests/test_file_importer.py

Merge with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009 Canonical Ltd.  This software is licensed under the
 
1
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
"""Translation File Importer tests."""
5
5
 
6
6
__metaclass__ = type
7
7
 
8
 
import unittest
9
 
 
10
 
import transaction
11
8
from zope.component import getUtility
12
9
from zope.security.proxy import removeSecurityProxy
13
10
 
14
 
from canonical.testing import LaunchpadZopelessLayer
 
11
from canonical.testing import ZopelessDatabaseLayer
15
12
from lp.registry.interfaces.person import IPersonSet
16
13
from lp.testing import TestCaseWithFactory
 
14
from lp.testing.fakelibrarian import FakeLibrarian
17
15
from lp.translations.interfaces.translationgroup import TranslationPermission
18
16
from lp.translations.interfaces.translationimporter import (
19
17
    OutdatedTranslationError,
82
80
#, c-format
83
81
msgid "%s"
84
82
msgstr ""
85
 
''' % (TEST_MSGID_ERROR)
 
83
''' % TEST_MSGID_ERROR
 
84
 
86
85
 
87
86
TEST_TRANSLATION_FILE_WITH_ERROR = r'''
88
87
msgid ""
95
94
#, c-format
96
95
msgid "%s"
97
96
msgstr "format specifier changes %%s"
98
 
'''  % (TEST_MSGID_ERROR)
 
97
''' % TEST_MSGID_ERROR
 
98
 
99
99
 
100
100
class FileImporterTestCase(TestCaseWithFactory):
101
101
    """Class test for translation importer component"""
102
 
    layer = LaunchpadZopelessLayer
 
102
    layer = ZopelessDatabaseLayer
103
103
 
104
104
    def _createFileImporters(self, pot_content, po_content, is_published):
105
105
        """Create queue entries from POT and PO content strings.
119
119
            is_published, self.importer_person,
120
120
            productseries=potemplate.productseries,
121
121
            potemplate=potemplate)
122
 
        transaction.commit()
123
 
        return POTFileImporter(
124
 
            template_entry, GettextPOImporter(), None )
 
122
        self.fake_librarian.pretendCommit()
 
123
        return POTFileImporter(template_entry, GettextPOImporter(), None)
125
124
 
126
125
    def _createPOFileImporter(self,
127
126
            pot_importer, po_content, is_published, existing_pofile=None,
138
137
        translation_entry = self.translation_import_queue.addOrUpdateEntry(
139
138
            pofile.path, po_content, is_published, person,
140
139
            productseries=potemplate.productseries, pofile=pofile)
141
 
        transaction.commit()
142
 
        return POFileImporter(
143
 
            translation_entry, GettextPOImporter(), None )
 
140
        self.fake_librarian.pretendCommit()
 
141
        return POFileImporter(translation_entry, GettextPOImporter(), None)
144
142
 
145
143
    def _createImporterForExportedEntries(self):
146
144
        """Set up entries that where exported from LP, i.e. that contain the
164
162
            False, self.importer_person,
165
163
            productseries=potemplate.productseries,
166
164
            potemplate=potemplate)
167
 
        transaction.commit()
168
 
        return FileImporter(
169
 
            template_entry, GettextPOImporter(), None )
 
165
        self.fake_librarian.pretendCommit()
 
166
        return FileImporter(template_entry, GettextPOImporter(), None)
170
167
 
171
168
    def setUp(self):
172
169
        super(FileImporterTestCase, self).setUp()
 
170
        self.fake_librarian = FakeLibrarian()
 
171
        self.fake_librarian.installAsLibrarian()
173
172
        self.translation_import_queue = getUtility(ITranslationImportQueue)
174
173
        self.importer_person = self.factory.makePerson()
175
174
 
 
175
    def tearDown(self):
 
176
        self.fake_librarian.uninstall()
 
177
        super(FileImporterTestCase, self).tearDown()
 
178
 
176
179
    def test_FileImporter_importMessage_NotImplemented(self):
177
180
        importer = self._createFileImporter()
178
181
        self.failUnlessRaises(NotImplementedError,
241
244
        product.translationpermission = TranslationPermission.CLOSED
242
245
        product.translationgroup = self.factory.makeTranslationGroup(
243
246
            self.importer_person)
244
 
        transaction.commit()
 
247
        self.fake_librarian.pretendCommit()
245
248
 
246
249
        # Get one POTMsgSet to do storeTranslationsInDatabase on.
247
250
        message = pot_importer.translation_file.messages[0]
280
283
            "POTFileImporter had no reference to an IPOTemplate.")
281
284
        self.failUnless(pot_importer.pofile is None or
282
285
            pot_importer.pofile.language == "en",
283
 
            "POTFileImporter referenced an IPOFile which was not English." )
 
286
            "POTFileImporter referenced an IPOFile which was not English.")
284
287
        # Test if POFileImporter gets initialised correctly.
285
288
        self.failUnless(po_importer.potemplate is not None,
286
289
            "POTFileImporter had no reference to an IPOTemplate.")
340
343
        # Test correct import operation for both
341
344
        # exported and published files.
342
345
        importers = (
343
 
                     self._createImporterForExportedEntries(),
344
 
                     self._createImporterForPublishedEntries()
345
 
                     )
 
346
            self._createImporterForExportedEntries(),
 
347
            self._createImporterForPublishedEntries(),
 
348
            )
346
349
        for (pot_importer, po_importer) in importers:
347
350
            # Run the import and see if PotMsgSet and TranslationMessage
348
351
            # entries are correctly created in the DB.
385
388
        self.failUnlessEqual(len(errors), 0,
386
389
            "POFileImporter.importFile returned errors where there should "
387
390
            "be none.")
388
 
        transaction.commit()
 
391
        self.fake_librarian.pretendCommit()
389
392
 
390
393
        # Create new POFileImporter with an earlier timestamp and
391
394
        # a different translation (msgstr).
397
400
        self.failUnlessEqual(len(errors), 1,
398
401
            "No error detected when importing a pofile with an earlier "
399
402
            "export timestamp (update conflict).")
400
 
        self.failUnless( errors[0]['error-message'].find(
 
403
        self.failUnless(
 
404
            errors[0]['error-message'].find(
401
405
                u"updated by someone else after you") != -1,
402
406
            "importFile() failed to detect a message update conflict.")
403
407
 
441
445
                TEST_TRANSLATION_FILE_WITH_ERROR, False)
442
446
        pot_importer.importFile()
443
447
        po_importer.importFile()
444
 
        transaction.commit()
 
448
        self.fake_librarian.pretendCommit()
445
449
 
446
450
        po_importer2 = self._createPOFileImporter(
447
451
            pot_importer, TEST_TRANSLATION_EXPORTED_EARLIER, False,
477
481
            "Last-Translator: Hector Atlas <??@??.??>\\n"
478
482
            "Content-Type: text/plain; charset=UTF-8\\n"
479
483
            "X-Launchpad-Export-Date: 2008-11-05 13:31+0000\\n"
480
 
            
 
484
 
481
485
            msgid "%s"
482
486
            msgstr "Dankuwel"
483
487
            """ % TEST_MSGID
493
497
 
494
498
class CreateFileImporterTestCase(TestCaseWithFactory):
495
499
    """Class test for translation importer creation."""
496
 
    layer = LaunchpadZopelessLayer
 
500
    layer = ZopelessDatabaseLayer
497
501
 
498
502
    def setUp(self):
499
503
        super(CreateFileImporterTestCase, self).setUp()
 
504
        self.fake_librarian = FakeLibrarian()
 
505
        self.fake_librarian.installAsLibrarian()
500
506
        self.translation_import_queue = getUtility(ITranslationImportQueue)
501
507
        self.importer_person = self.factory.makePerson()
502
508
 
 
509
    def tearDown(self):
 
510
        self.fake_librarian.uninstall()
 
511
        super(CreateFileImporterTestCase, self).tearDown()
 
512
 
503
513
    def _make_queue_entry(self, is_published):
504
514
        pofile = self.factory.makePOFile('eo')
505
515
        # Create a header with a newer date than what is found in
510
520
        queue_entry = self.translation_import_queue.addOrUpdateEntry(
511
521
            pofile.path, po_content, is_published, self.importer_person,
512
522
            productseries=pofile.potemplate.productseries, pofile=pofile)
513
 
        transaction.commit()
 
523
        self.fake_librarian.pretendCommit()
514
524
        return queue_entry
515
525
 
516
526
    def test_raises_OutdatedTranslationError_on_user_uploads(self):
517
527
        queue_entry = self._make_queue_entry(False)
518
 
        self.assertRaises(OutdatedTranslationError, POFileImporter,
519
 
            queue_entry, GettextPOImporter(), None )
 
528
        self.assertRaises(
 
529
            OutdatedTranslationError,
 
530
            POFileImporter, queue_entry, GettextPOImporter(), None)
520
531
 
521
532
    def test_not_raises_OutdatedTranslationError_on_published_uploads(self):
522
533
        queue_entry = self._make_queue_entry(True)
523
534
        try:
524
 
            importer = POFileImporter(queue_entry, GettextPOImporter(), None )
 
535
            importer = POFileImporter(queue_entry, GettextPOImporter(), None)
525
536
        except OutdatedTranslationError:
526
537
            self.fail("OutdatedTranslationError raised.")
527
538
 
529
540
        queue_entry = self._make_queue_entry(True)
530
541
        pofile = queue_entry.pofile
531
542
        old_raw_header = pofile.header
532
 
        importer = POFileImporter(queue_entry, GettextPOImporter(), None )
 
543
        importer = POFileImporter(queue_entry, GettextPOImporter(), None)
533
544
        self.assertEqual(old_raw_header, pofile.header)
534
 
 
535
 
 
536
 
def test_suite():
537
 
    return unittest.TestLoader().loadTestsFromName(__name__)