def setUp(self):
     # This test needs the privileges of rosettaadmin (to update
     # TranslationMessages) but it also needs to set up test conditions
     # which requires other privileges.
     switch_dbuser('postgres')
     super(TestUpdaterLoop, self).setUp(user='******')
     self.logger = logging.getLogger("migrate-current-flag")
     self.migrate_loop = TranslationMessageImportedFlagUpdater(
         self.layer.txn, self.logger, [])
class TestUpdaterLoop(TestCaseWithFactory):
    """Test updater-loop core functionality."""
    layer = LaunchpadZopelessLayer

    def setUp(self):
        # This test needs the privileges of rosettaadmin (to update
        # TranslationMessages) but it also needs to set up test conditions
        # which requires other privileges.
        switch_dbuser('postgres')
        super(TestUpdaterLoop, self).setUp(user='******')
        self.logger = logging.getLogger("migrate-current-flag")
        self.migrate_loop = TranslationMessageImportedFlagUpdater(
            self.layer.txn, self.logger, [])

    def test_updateTranslationMessages_base(self):
        # Passing in a TranslationMessage.id sets is_imported flag
        # on that message even if it was not set before.
        translation = self.factory.makeSuggestion()
        translation.is_current_ubuntu = True
        self.assertFalse(translation.is_current_upstream)

        self.migrate_loop._updateTranslationMessages([translation.id])
        self.assertTrue(translation.is_current_upstream)

    def test_updateTranslationMessages_unsetting_imported(self):
        # If there was a previous imported message, it is unset
        # first.
        pofile = self.factory.makePOFile()
        imported = self.factory.makeCurrentTranslationMessage(pofile=pofile)
        translation = self.factory.makeSuggestion(
            pofile=pofile, potmsgset=imported.potmsgset)
        translation.is_current_ubuntu = True
        self.assertTrue(imported.is_current_upstream)
        self.assertFalse(imported.is_current_ubuntu)
        self.assertFalse(translation.is_current_upstream)
        self.assertTrue(translation.is_current_ubuntu)

        self.migrate_loop._updateTranslationMessages([translation.id])
        self.assertFalse(imported.is_current_upstream)
        self.assertTrue(translation.is_current_upstream)
        self.assertTrue(translation.is_current_ubuntu)

    def test_updateTranslationMessages_other_language(self):
        # If there was a previous imported message in another language
        # it is not unset.
        pofile = self.factory.makePOFile()
        pofile_other = self.factory.makePOFile(potemplate=pofile.potemplate)
        imported = self.factory.makeCurrentTranslationMessage(
            pofile=pofile_other)
        imported.is_current_ubuntu = True
        translation = self.factory.makeSuggestion(
            pofile=pofile, potmsgset=imported.potmsgset)
        translation.is_current_ubuntu = True
        self.assertTrue(imported.is_current_upstream)
        self.assertTrue(imported.is_current_ubuntu)
        self.assertFalse(translation.is_current_upstream)
        self.assertTrue(translation.is_current_ubuntu)

        self.migrate_loop._updateTranslationMessages([translation.id])
        self.assertTrue(imported.is_current_upstream)
        self.assertTrue(imported.is_current_ubuntu)
        self.assertTrue(translation.is_current_upstream)
        self.assertTrue(translation.is_current_ubuntu)

    def test_updateTranslationMessages_diverged(self):
        # If there was a previous diverged message, it is not
        # touched.
        pofile = self.factory.makePOFile()
        translation = self.factory.makeSuggestion(pofile=pofile)
        translation.is_current_ubuntu = True
        diverged_imported = self.factory.makeCurrentTranslationMessage(
            pofile=pofile, diverged=True, potmsgset=translation.potmsgset)
        diverged_imported.is_current_ubuntu = True
        self.assertEquals(pofile.potemplate, diverged_imported.potemplate)
        self.assertTrue(diverged_imported.is_current_upstream)
        self.assertTrue(diverged_imported.is_current_ubuntu)

        self.migrate_loop._updateTranslationMessages([translation.id])
        self.assertEquals(pofile.potemplate, diverged_imported.potemplate)
        self.assertTrue(diverged_imported.is_current_upstream)
        self.assertTrue(diverged_imported.is_current_ubuntu)