← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~adeuring/launchpad/translation-fixing3 into lp:launchpad

 

Abel Deuring has proposed merging lp:~adeuring/launchpad/translation-fixing3 into lp:launchpad with lp:~adeuring/launchpad/translation-fixing2 as a prerequisite.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~adeuring/launchpad/translation-fixing3/+merge/47528

This branch replaces some calls of updateTranslation() by makeCurrentTranslationMessage()

The test setup is also slightly refactored, making the test methods quite short.

I did not see any reason why the translations created for the tests had to explicitly removed via try/finally -- the database is in a "fresh state" for each test anyway, and the tests do not do any possibly nasty things like creating temporaray files. So I removed this too.

test: ./bin/test translations -vvt test_remove_translations

no lint
-- 
https://code.launchpad.net/~adeuring/launchpad/translation-fixing3/+merge/47528
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~adeuring/launchpad/translation-fixing3 into lp:launchpad.
=== modified file 'lib/lp/translations/doc/rosetta-translation.txt'
--- lib/lp/translations/doc/rosetta-translation.txt	2011-01-24 15:51:18 +0000
+++ lib/lp/translations/doc/rosetta-translation.txt	2011-01-26 13:49:13 +0000
@@ -103,9 +103,9 @@
 this project from Pa.
 
     >>> translations = { 0: u'bar' }
-    >>> upstream_message = potmsgset.updateTranslation(
-    ...     pofile, Pa, translations, is_current_upstream=True,
-    ...     lock_timestamp=datetime.datetime.now(UTC))
+    >>> upstream_message = factory.makeCurrentTranslationMessage(
+    ...     pofile, potmsgset=potmsgset, translator=Pa,
+    ...     translations=translations, current_other=True)
     >>> flush_database_caches()
 
 Make sure that the new submission is in fact from Pa.
@@ -161,115 +161,3 @@
 
     >>> pofile.lasttranslator == Pb
     True
-
-Now, let's see what happens if Pc submits exactly the same translation that
-Pb just did. We don't want to record this, because we have decided not to
-record a new submission of the EXISTING active or upstream record. In other
-words, if the current active translation is 'baz', and someone else comes
-along and says it's 'baz', we don't record anything new.
-
-    >>> translations = { 0: u'baz' }
-    >>> dummy = potmsgset.updateTranslation(
-    ...     pofile, Pc, translations, is_current_upstream=False,
-    ...     lock_timestamp=datetime.datetime.now(UTC))
-    >>> repeat_submission = potmsgset.getCurrentTranslation(
-    ...     template, spanish, template.translation_side)
-
-Since that is exactly what Pb already said, let's see that the submission we
-got back was the one from Pb not a new one for Pc:
-
-    >>> repeat_submission.submitter == Pb
-    True
-
-In fact, it should be the same as before:
-
-    >>> repeat_submission == web_submission
-    True
-
-Now, let's test the NonEditorTranslations. These are translations submitted by
-people who do not have editorial rights on the pofile. We generally accept
-these but don't make them active.
-
-Here is our NonEditor:
-
-    >>> Pn = Person.get(51)
-    >>> Pn.name
-    u'kreutzm'
-
-Testing NoneEditorTranslations
-
-Let's see if there are any suggestions for this potmsgset. We are not
-expecting any.
-
-    >>> list(potmsgset.getLocalTranslationMessages(template, spanish))
-    []
-
-Let's make a submission from Pn  without editorial permissions, to the same
-msgset and plural form as above. First we will try and make it an upstream
-submission, and we expect to get an assertion error because we expect never
-to see a non-editor upstream submission.
-
-    >>> translations = { 0: u'bong' }
-    >>> dummy = potmsgset.updateTranslation(
-    ...     pofile, Pn, translations, is_current_upstream=True,
-    ...     lock_timestamp=datetime.datetime.now(UTC))
-    Traceback (most recent call last):
-    ...
-    AssertionError: Only an editor can submit is_current_upstream
-    translations.
-
-Now let's try again, but not as an imported translation.  This is
-accepted.
-
-    >>> noneditor_submission = potmsgset.updateTranslation(
-    ...    pofile, Pn, {0: u'bong'}, is_current_upstream=False,
-    ...    lock_timestamp=datetime.datetime.now(UTC))
-
-This submission should come from the new non-editor person:
-
-    >>> noneditor_submission.submitter == Pn
-    True
-
-And the lasttranslator for this POFile should not change, as this submission
-is not from an editor:
-
-    >>> pofile.lasttranslator == Pc
-    True
-
-And now we need to cheat, just a little bit.  We need to pretend that the
-noneditor submission was made a few seconds AFTER the other one. Since both
-use the DEFAULT datecreated, which is NOW, and both are inside the same
-transaction, they APPEAR to have happened simultaneously.  We need to change
-that, so we bump up the noneditor_submission.datecreated by a whole 2
-seconds.
-
-    >>> import datetime
-    >>> noneditor_submission.date_created += datetime.timedelta(0,2,0)
-    >>> from canonical.database.sqlbase import flush_database_updates
-    >>> flush_database_updates()
-
-Let's make sure:
-
-    >>> (noneditor_submission.date_created >
-    ...  potmsgset.getCurrentTranslation(
-    ...      template, spanish, template.translation_side).date_created)
-    True
-
-This new submission should not be active:
-
-    >>> current = potmsgset.getCurrentTranslation(
-    ...     template, spanish, template.translation_side)
-    >>> current == noneditor_submission
-    False
-
-And it should definitely not be current upstream:
-
-    >>> (potmsgset.getImportedTranslationMessage(template, spanish) ==
-    ...  noneditor_submission)
-    False
-
-However, given NonEditorTranslations, this should be available as
-a suggested translation.
-
-    >>> potmsgset.getLocalTranslationMessages(template, spanish).count()
-    1

=== modified file 'lib/lp/translations/scripts/tests/test_remove_translations.py'
--- lib/lp/translations/scripts/tests/test_remove_translations.py	2010-12-02 16:13:51 +0000
+++ lib/lp/translations/scripts/tests/test_remove_translations.py	2011-01-26 13:49:13 +0000
@@ -7,7 +7,6 @@
 
 __metaclass__ = type
 
-from datetime import datetime
 import logging
 from optparse import (
     OptionParser,
@@ -15,15 +14,18 @@
     )
 from unittest import TestLoader
 
-from pytz import timezone
 from storm.store import Store
 from zope.component import getUtility
+from zope.security.proxy import removeSecurityProxy
 
 from canonical.launchpad.ftests import sync
 from canonical.testing.layers import LaunchpadZopelessLayer
 from lp.registry.interfaces.person import IPersonSet
 from lp.services.scripts.base import LaunchpadScriptFailure
-from lp.testing import TestCase
+from lp.testing import (
+    TestCase,
+    TestCaseWithFactory,
+    )
 from lp.testing.factory import LaunchpadObjectFactory
 from lp.translations.interfaces.translationmessage import (
     RosettaTranslationOrigin,
@@ -265,10 +267,9 @@
         """Set translation for potmsgset in pofile to text."""
         if submitter is None:
             submitter = self.potemplate.owner
-        return potmsgset.updateTranslation(
-            pofile, submitter, {0: text},
-            is_current_upstream=is_current_upstream,
-            lock_timestamp=datetime.now(timezone('UTC')))
+        return self.factory.makeCurrentTranslationMessage(
+            pofile, potmsgset, translator=submitter,
+            translations={0: text}, current_other=is_current_upstream)
 
     def _makeMessages(self, template_text, nl_text, de_text,
                       submitter=None, is_current_upstream=False):
@@ -387,10 +388,10 @@
         potmsgset = self.factory.makePOTMsgSet(
             unrelated_nl_pofile.potemplate, 'Foo',
             sequence=0)
-        unrelated_nl_message = potmsgset.updateTranslation(
-            unrelated_nl_pofile, unrelated_nl_pofile.potemplate.owner,
-            {0: "Foe"}, is_current_upstream=False,
-            lock_timestamp=datetime.now(timezone('UTC')))
+        unrelated_nl_message = self.factory.makeCurrentTranslationMessage(
+            unrelated_nl_pofile, potmsgset,
+            translator=unrelated_nl_pofile.potemplate.owner,
+            translations={0: "Foe"})
 
         ids = [new_nl_message.id, new_de_message.id, unrelated_nl_message.id]
         rowcount = self._removeMessages(
@@ -429,23 +430,23 @@
         # Remove current messages, but not non-current messages.
         (new_nl_message, new_de_message) = self._makeMessages(
             "translate", "vertalen", "uebersetzen")
-        self.nl_message.is_current_ubuntu = False
+        self.nl_message.is_current_upstream = False
 
         ids = [self.nl_message.id, new_nl_message.id, new_de_message.id]
-        self._removeMessages(ids=ids, is_current_ubuntu=True)
+        self._removeMessages(ids=ids, is_current_upstream=True)
 
-        self.nl_message.is_current_ubuntu = True
+        self.nl_message.is_current_upstream = True
         self._checkInvariant()
 
     def test_RemoveNotCurrent(self):
         # Remove current messages, but not non-current messages.
         (new_nl_message, new_de_message) = self._makeMessages(
             "write", "schrijven", "schreiben")
-        new_nl_message.is_current_ubuntu = False
-        new_de_message.is_current_ubuntu = False
+        new_nl_message.is_current_upstream = False
+        new_de_message.is_current_upstream = False
 
         ids = [self.nl_message.id, new_nl_message.id, new_de_message.id]
-        self._removeMessages(ids=ids, is_current_ubuntu=False)
+        self._removeMessages(ids=ids, is_current_upstream=False)
 
         self._checkInvariant()
 
@@ -453,11 +454,11 @@
         # Remove current messages, but not non-current messages.
         (new_nl_message, new_de_message) = self._makeMessages(
             "book", "boek", "Buch")
-        new_nl_message.is_current_upstream = True
-        new_de_message.is_current_upstream = True
+        new_nl_message.is_current_ubuntu = True
+        new_de_message.is_current_ubuntu = True
 
         ids = [self.nl_message.id, new_nl_message.id, new_de_message.id]
-        self._removeMessages(ids=ids, is_current_upstream=True)
+        self._removeMessages(ids=ids, is_current_ubuntu=True)
 
         self._checkInvariant()
 
@@ -465,12 +466,12 @@
         # Remove current messages, but not non-current messages.
         (new_nl_message, new_de_message) = self._makeMessages(
             "helicopter", "helikopter", "Hubschauber")
-        self.nl_message.is_current_upstream = True
+        self.nl_message.is_current_ubuntu = True
 
         ids = [self.nl_message.id, new_nl_message.id, new_de_message.id]
-        self._removeMessages(ids=ids, is_current_upstream=False)
+        self._removeMessages(ids=ids, is_current_ubuntu=False)
 
-        self.nl_message.is_current_upstream = False
+        self.nl_message.is_current_ubuntu = False
         self._checkInvariant()
 
     def test_RemoveMsgId(self):
@@ -488,8 +489,10 @@
             self.nl_message.origin, RosettaTranslationOrigin.ROSETTAWEB)
         (new_nl_message, new_de_message) = self._makeMessages(
             "new", "nieuw", "neu", is_current_upstream=True)
-        self.assertEqual(new_nl_message.origin, RosettaTranslationOrigin.SCM)
-        self.assertEqual(new_de_message.origin, RosettaTranslationOrigin.SCM)
+        removeSecurityProxy(new_nl_message).origin = (
+            RosettaTranslationOrigin.SCM)
+        removeSecurityProxy(new_de_message).origin = (
+            RosettaTranslationOrigin.SCM)
 
         self._removeMessages(
             potemplate=self.potemplate, origin=RosettaTranslationOrigin.SCM)
@@ -551,7 +554,7 @@
             answer.destroySelf()
 
 
-class TestRemoveTranslationsUnmasking(TestCase):
+class TestRemoveTranslationsUnmasking(TestCaseWithFactory):
     """Test that `remove_translations` "unmasks" upstream messages.
 
     When a current, non-upstream message is deleted, the deletion code
@@ -568,70 +571,40 @@
 
         # Set up a template with a Laotian translation file.  There's
         # one message to be translated.
-        factory = LaunchpadObjectFactory()
-        self.pofile = factory.makePOFile('lo')
-        potemplate = self.pofile.potemplate
-        self.potmsgset = factory.makePOTMsgSet(potemplate, 'foo',
-                                               sequence=0)
+        potemplate = self.factory.makePOTemplate()
+        self.potmsgset = self.factory.makePOTMsgSet(
+            potemplate, 'foo', sequence=0)
+        self.pofile = self.factory.makePOFile('lo', potemplate=potemplate)
 
-    def _setTranslation(self, text, is_current_upstream=False):
-        return self.potmsgset.updateTranslation(
-            self.pofile, self.pofile.owner, {0: text},
-            is_current_upstream=is_current_upstream,
-            lock_timestamp=datetime.now(timezone('UTC')))
+        self.ubuntu = self.factory.makeCurrentTranslationMessage(
+            self.pofile, self.potmsgset, current_other=True)
+        self.upstream = self.factory.makeCurrentTranslationMessage(
+            self.pofile, self.potmsgset, current_other=False)
+        Store.of(self.upstream).flush()
+        self.assertFalse(
+            self.upstream.is_current_ubuntu, "Broken test setup.")
+        self.assertTrue(
+            self.upstream.is_current_upstream, "Broken test setup.")
+        self.assertTrue(self.ubuntu.is_current_ubuntu, "Broken test setup.")
+        self.assertFalse(
+            self.ubuntu.is_current_upstream, "Broken test setup.")
 
     def test_unmask_upstream_message(self):
         # Basic use case: upstream message is unmasked.
-        cleanups = []
-        try:
-            upstream = self._setTranslation(
-                'upstream', is_current_upstream=True)
-            cleanups.append(upstream)
-            ubuntu = self._setTranslation(
-                'ubuntu', is_current_upstream=False)
-            cleanups.append(ubuntu)
-            self.assertFalse(upstream.is_current_ubuntu, "Broken test setup.")
-            self.assertTrue(
-                upstream.is_current_upstream, "Broken test setup.")
-            self.assertTrue(ubuntu.is_current_ubuntu, "Broken test setup.")
-            self.assertFalse(ubuntu.is_current_upstream, "Broken test setup.")
-            Store.of(ubuntu).flush()
-
-            remove_translations(ids=[ubuntu.id])
-
-            sync(upstream)
-            self.assertTrue(upstream.is_current_upstream)
-            self.assertTrue(upstream.is_current_ubuntu)
-        finally:
-            # Clean up.
-            remove_translations(ids=[message.id for message in cleanups])
+        remove_translations(ids=[self.ubuntu.id])
+        sync(self.upstream)
+        self.assertTrue(self.upstream.is_current_upstream)
+        self.assertTrue(self.upstream.is_current_ubuntu)
 
     def test_unmask_right_message(self):
         # Unmasking picks the right message, and doesn't try to violate
         # the unique constraint on is_current_upstream.
-        cleanups = []
-        try:
-            inactive = self._setTranslation('inactive')
-            cleanups.append(inactive)
-            upstream = self._setTranslation(
-                'upstream', is_current_upstream=True)
-            cleanups.append(upstream)
-            ubuntu = self._setTranslation(
-                'ubuntu', is_current_upstream=False)
-            self.assertFalse(inactive.is_current_ubuntu, "Broken test setup.")
-            self.assertFalse(
-                inactive.is_current_upstream, "Broken test setup.")
-            Store.of(ubuntu).flush()
-
-            remove_translations(ids=[ubuntu.id])
-
-            sync(upstream)
-            sync(inactive)
-            self.assertTrue(upstream.is_current_ubuntu)
-            self.assertFalse(inactive.is_current_ubuntu)
-        finally:
-            # Clean up.
-            remove_translations(ids=[message.id for message in cleanups])
+        inactive = self.factory.makeSuggestion(self.pofile, self.potmsgset)
+        remove_translations(ids=[self.ubuntu.id])
+        sync(self.upstream)
+        sync(inactive)
+        self.assertTrue(self.upstream.is_current_ubuntu)
+        self.assertFalse(inactive.is_current_ubuntu)
 
 
 def test_suite():