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(TestMigrateCurrentFlag, self).setUp(user='******')
     self.migrate_process = MigrateCurrentFlagProcess(self.layer.txn)
 def main(self):
     fixer = MigrateCurrentFlagProcess(self.txn, self.logger)
     fixer.run()
class TestMigrateCurrentFlag(TestCaseWithFactory):
    """Test current-flag migration script."""
    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(TestMigrateCurrentFlag, self).setUp(user='******')
        self.migrate_process = MigrateCurrentFlagProcess(self.layer.txn)

    def test_getProductsWithTemplates_sampledata(self):
        # Sample data already has 3 products with templates.
        sampledata_products = list(
            self.migrate_process.getProductsWithTemplates())
        self.assertEquals(3, len(sampledata_products))

    def test_getProductsWithTemplates_noop(self):
        # Adding a product with no templates doesn't change anything.
        sampledata_products = list(
            self.migrate_process.getProductsWithTemplates())
        self.factory.makeProduct()
        products = self.migrate_process.getProductsWithTemplates()
        self.assertContentEqual(sampledata_products, list(products))

    def test_getProductsWithTemplates_new_template(self):
        # A new product with a template is included.
        sampledata_products = list(
            self.migrate_process.getProductsWithTemplates())
        product = self.factory.makeProduct()
        self.factory.makePOTemplate(productseries=product.development_focus)
        products = self.migrate_process.getProductsWithTemplates()
        self.assertContentEqual(
            sampledata_products + [product], list(products))

    def test_getCurrentNonimportedTranslations_empty(self):
        # For a product with no translations no messages are returned.
        potemplate = self.factory.makePOTemplate()
        results = list(
            self.migrate_process.getCurrentNonimportedTranslations(
                potemplate.productseries.product))
        self.assertContentEqual([], results)

    def test_getCurrentNonimportedTranslations_noncurrent(self):
        # For a product with non-current translations no messages
        # are returned.
        potemplate = self.factory.makePOTemplate()
        potmsgset = self.factory.makePOTMsgSet(
            potemplate=potemplate,
            sequence=1)
        pofile = self.factory.makePOFile(potemplate=potemplate)
        self.factory.makeSuggestion(pofile=pofile, potmsgset=potmsgset)
        results = list(
            self.migrate_process.getCurrentNonimportedTranslations(
                potemplate.productseries.product))
        self.assertContentEqual([], results)

    def test_getCurrentNonimportedTranslations_current_imported(self):
        # For a product with current, imported translations no messages
        # are returned.
        potemplate = self.factory.makePOTemplate()
        potmsgset = self.factory.makePOTMsgSet(
            potemplate=potemplate,
            sequence=1)
        pofile = self.factory.makePOFile(potemplate=potemplate)
        translation = self.factory.makeCurrentTranslationMessage(
            pofile=pofile, potmsgset=potmsgset)
        translation.is_current_ubuntu = True
        results = list(
            self.migrate_process.getCurrentNonimportedTranslations(
                potemplate.productseries.product))
        self.assertContentEqual([], results)

    def test_getCurrentNonimportedTranslations_current_nonimported(self):
        # For a product with current, non-imported translations,
        # that translation is returned.
        potemplate = self.factory.makePOTemplate()
        potmsgset = self.factory.makePOTMsgSet(
            potemplate=potemplate,
            sequence=1)
        pofile = self.factory.makePOFile(potemplate=potemplate)
        translation = self.factory.makeSuggestion(
            pofile=pofile, potmsgset=potmsgset)
        translation.is_current_ubuntu = True
        results = list(
            self.migrate_process.getCurrentNonimportedTranslations(
                potemplate.productseries.product))
        self.assertContentEqual([translation.id], results)
 def main(self):
     fixer = MigrateCurrentFlagProcess(self.txn, self.logger)
     fixer.run()