Beispiel #1
0
    def test_readonly_locales(self):
        """Don't call commit_changes for locales in read-only mode."""
        project_locale = self.translated_locale.project_locale.get(
            project=self.db_project,
        )
        project_locale.readonly = True
        project_locale.save()

        self.mock_pull_changes.return_value = [
            True,
            {
                self.repository.pk: Locale.objects.filter(
                    pk=self.translated_locale.pk,
                ),
            },
        ]

        sync_translations(
            self.db_project.pk,
            self.project_sync_log.pk,
            self.now,
            no_commit=False,
        )

        assert_false(self.mock_commit_changes.called)
Beispiel #2
0
    def test_remove_duplicate_approvals(self):
        """
        Ensure that duplicate approvals are removed.
        """
        # Trigger creation of new approved translation.
        self.main_vcs_translation.strings[None] = 'New Translated String'
        self.main_vcs_translation.fuzzy = False

        # Translation approved after the sync started simulates the race
        # where duplicate translations occur.
        duplicate_translation = TranslationFactory.create(
            entity=self.main_db_entity,
            locale=self.translated_locale,
            string='Other New Translated String',
            approved=True,
            approved_date=aware_datetime(1970, 1, 3)
        )
        ChangedEntityLocale.objects.filter(entity=self.main_db_entity).delete()

        with patch('pontoon.sync.tasks.VCSProject', return_value=self.vcs_project):
            sync_translations(self.db_project.pk, self.repository.pk,
                              self.project_sync_log.pk, self.now,
                              self.mock_changes)

        # Only one translation should be approved: the duplicate_translation.
        assert_equal(self.main_db_entity.translation_set.filter(approved=True).count(), 1)
        new_translation = self.main_db_entity.translation_set.get(
            string='New Translated String'
        )
        assert_false(new_translation.approved)
        assert_true(new_translation.approved_date is None)

        duplicate_translation.refresh_from_db()
        assert_true(duplicate_translation.approved)
        assert_equal(duplicate_translation.approved_date, aware_datetime(1970, 1, 3))
Beispiel #3
0
    def test_readonly_locales(self):
        """Don't call commit_changes for locales in read-only mode."""
        project_locale = self.translated_locale.project_locale.get(
            project=self.db_project,
        )
        project_locale.readonly = True
        project_locale.save()

        self.mock_pull_locale_repo_changes.return_value = [
            True,
            {
                self.repository.pk: Locale.objects.filter(
                    pk=self.translated_locale.pk,
                )
            },
        ]

        sync_translations(
            self.db_project,
            self.project_sync_log,
            self.now,
            True,
            no_commit=False,
        )

        assert not self.mock_commit_changes.called
Beispiel #4
0
    def test_clear_changed_entities(self):
        """
        Delete all ChangedEntityLocale objects for the project created
        before the sync started after handling it.
        """
        self.now = aware_datetime(1970, 1, 2)
        self.mock_pull_changes.return_value = [True, {
            self.repository.pk: Locale.objects.filter(pk=self.translated_locale.pk)
        }]

        changed1, changed2, changed_after = ChangedEntityLocaleFactory.create_batch(3,
            locale=self.translated_locale,
            entity__resource=self.main_db_resource,
            when=aware_datetime(1970, 1, 1)
        )
        changed_after.when = aware_datetime(1970, 1, 3)
        changed_after.save()

        sync_translations(self.db_project.pk, self.project_sync_log.pk,
                          self.now, self.mock_changes)
        with assert_raises(ChangedEntityLocale.DoesNotExist):
            changed1.refresh_from_db()
        with assert_raises(ChangedEntityLocale.DoesNotExist):
            changed2.refresh_from_db()
        changed_after.refresh_from_db()  # Should not raise
Beispiel #5
0
    def test_new_entities_pretranslation(self):
        """
        Test if pretranslation is called for newly added entities.
        """
        self.db_project.pretranslation_enabled = True
        self.db_project.save()
        self.mock_pull_locale_repo_changes.return_value = [
            True,
            {
                self.repository.pk:
                Locale.objects.filter(pk=self.translated_locale.pk)
            },
        ]
        all_locales = list(self.db_project.locales.values_list("pk",
                                                               flat=True))

        with self.patch("pontoon.sync.tasks.update_translated_resources",
                        return_value=False):
            sync_translations(
                self.db_project,
                self.project_sync_log,
                self.now,
                True,
                [],
                [],
                [],
                ["new_entity"],
            )

        assert self.mock_pretranslate.called
        assert self.mock_pretranslate.call_args[1]["entities"] == [
            "new_entity"
        ]
        assert list(
            self.mock_pretranslate.call_args[1]["locales"]) == all_locales
Beispiel #6
0
    def test_new_translated_resource_pretranslation(self):
        """
        Test if pretranslation is called for locales with newly added TranslatedResource.
        """
        self.db_project.pretranslation_enabled = True
        self.db_project.save()
        self.mock_pull_locale_repo_changes.return_value = [
            True,
            {
                self.repository.pk:
                Locale.objects.filter(pk=self.translated_locale.pk)
            },
        ]

        sync_translations(
            self.db_project,
            self.project_sync_log,
            self.now,
            True,
            [],
            [],
            [],
            ["new_entity"],
        )

        assert self.mock_pretranslate.called
        assert self.mock_pretranslate.call_args[1]["locales"] == [
            self.translated_locale.pk
        ]

        # Ensure that pretranslate is called only once for the locale.
        assert self.mock_pretranslate.call_args[1].get("entities") is None
Beispiel #7
0
    def test_clear_changed_entities(self):
        """
        Delete all ChangedEntityLocale objects for the project created
        before the sync started after handling it.
        """
        self.now = aware_datetime(1970, 1, 2)
        self.mock_pull_locale_repo_changes.return_value = [
            True,
            {
                self.repository.pk:
                Locale.objects.filter(pk=self.translated_locale.pk)
            },
        ]

        changed1, changed2, changed_after = ChangedEntityLocaleFactory.create_batch(
            3,
            locale=self.translated_locale,
            entity__resource=self.main_db_resource,
            when=aware_datetime(1970, 1, 1),
        )
        changed_after.when = aware_datetime(1970, 1, 3)
        changed_after.save()

        sync_translations(self.db_project, self.project_sync_log, self.now,
                          True)
        with pytest.raises(ChangedEntityLocale.DoesNotExist):
            changed1.refresh_from_db()
        with pytest.raises(ChangedEntityLocale.DoesNotExist):
            changed2.refresh_from_db()
        changed_after.refresh_from_db()  # Should not raise
Beispiel #8
0
    def test_no_pretranslation(self):
        """
        Ensure that pretranslation isn't called if pretranslation not enabled
        or no new Entity, Locale or TranslatedResource is created.
        """
        self.mock_pull_changes.return_value = [
            True,
            {self.repository.pk: Locale.objects.filter(pk=self.translated_locale.pk)},
        ]

        sync_translations(
            self.db_project.pk,
            self.project_sync_log.pk,
            self.now,
            [],
            [],
            [],
            ["new_entity"],
        )

        # Pretranslation is not enabled
        assert_false(self.mock_pretranslate.called)

        self.db_project.pretranslation_enabled = True
        self.db_project.save()

        with self.patch(
            "pontoon.sync.tasks.update_translated_resources", return_value=False
        ):
            sync_translations(self.db_project.pk, self.project_sync_log.pk, self.now)

        # No new Entity, Locale or TranslatedResource
        assert_false(self.mock_pretranslate.called)
Beispiel #9
0
 def test_no_commit(self):
     """Don't call commit_changes if command.no_commit is True."""
     self.mock_pull_changes.return_value = [True, {
         self.repository.pk: Locale.objects.filter(pk=self.translated_locale.pk)
     }]
     sync_translations(self.db_project.pk, self.project_sync_log.pk,
                       self.now, self.mock_changes, no_commit=True)
     assert_false(self.mock_commit_changes.called)
Beispiel #10
0
 def test_no_commit(self):
     """Don't call commit_changes if command.no_commit is True."""
     self.mock_pull_changes.return_value = [
         True,
         {self.repository.pk: Locale.objects.filter(pk=self.translated_locale.pk)},
     ]
     sync_translations(
         self.db_project.pk, self.project_sync_log.pk, self.now, no_commit=True
     )
     assert_false(self.mock_commit_changes.called)
Beispiel #11
0
    def test_create_project_log(self):
        assert_false(RepositorySyncLog.objects.exists())

        repo = RepositoryFactory.create()
        self.db_project.repositories = [repo]
        self.db_project.save()

        sync_translations(self.db_project.pk, self.repository.pk,
                          self.project_sync_log.pk, self.now,
                          self.mock_changes)

        log = RepositorySyncLog.objects.get(repository=self.repository)
        assert_equal(log.repository, self.repository)
Beispiel #12
0
    def test_create_repository_log(self):
        assert_false(RepositorySyncLog.objects.exists())

        repo = RepositoryFactory.create()
        self.db_project.repositories = [repo]
        self.db_project.save()
        self.mock_pull_changes.return_value = [True, {
            repo.pk: Locale.objects.filter(pk=self.translated_locale.pk)
        }]

        sync_translations(self.db_project.pk, self.project_sync_log.pk,
                          self.now, self.mock_changes)

        log = RepositorySyncLog.objects.get(repository=repo.pk)
        assert_equal(log.repository, repo)
Beispiel #13
0
    def test_create_repository_log(self):
        assert not RepositorySyncLog.objects.exists()

        repo = RepositoryFactory.create()
        self.db_project.repositories.set([repo])
        self.db_project.save()
        self.mock_pull_locale_repo_changes.return_value = [
            True,
            {repo.pk: Locale.objects.filter(pk=self.translated_locale.pk)},
        ]

        sync_translations(self.db_project, self.project_sync_log, self.now, True)

        log = RepositorySyncLog.objects.get(repository=repo.pk)
        assert log.repository == repo
Beispiel #14
0
    def test_create_repository_log(self):
        assert_false(RepositorySyncLog.objects.exists())

        repo = RepositoryFactory.create()
        self.db_project.repositories = [repo]
        self.db_project.save()
        self.mock_pull_changes.return_value = [
            True,
            {repo.pk: Locale.objects.filter(pk=self.translated_locale.pk)},
        ]

        sync_translations(self.db_project.pk, self.project_sync_log.pk, self.now)

        log = RepositorySyncLog.objects.get(repository=repo.pk)
        assert_equal(log.repository, repo)
Beispiel #15
0
    def test_remove_duplicate_approvals(self):
        """
        Ensure that duplicate approvals are removed.
        """
        # Trigger creation of new approved translation.
        self.main_vcs_translation.strings[None] = "New Translated String"
        self.main_vcs_translation.fuzzy = False
        self.mock_pull_locale_repo_changes.return_value = [
            True,
            {
                self.repository.pk:
                Locale.objects.filter(pk=self.translated_locale.pk)
            },
        ]

        # Translation approved after the sync started simulates the race
        # where duplicate translations occur.
        duplicate_translation = TranslationFactory.create(
            entity=self.main_db_entity,
            locale=self.translated_locale,
            string="Other New Translated String",
            approved=True,
            approved_date=aware_datetime(1970, 1, 3),
        )
        ChangedEntityLocale.objects.filter(entity=self.main_db_entity).delete()

        with patch("pontoon.sync.tasks.VCSProject",
                   return_value=self.vcs_project):
            sync_translations(self.db_project, self.project_sync_log, self.now,
                              True)

        # Only one translation should be approved: the duplicate_translation.
        assert self.main_db_entity.translation_set.filter(
            approved=True).count() == 1
        new_translation = self.main_db_entity.translation_set.get(
            string="New Translated String")
        assert not new_translation.approved
        assert new_translation.approved_date is None

        duplicate_translation.refresh_from_db()
        assert duplicate_translation.approved
        assert duplicate_translation.approved_date == aware_datetime(
            1970, 1, 3)
Beispiel #16
0
    def test_clear_changed_entities(self):
        """
        Delete all ChangedEntityLocale objects for the project created
        before the sync started after handling it.
        """
        self.now = aware_datetime(1970, 1, 2)
        changed1, changed2, changed_after = ChangedEntityLocaleFactory.create_batch(3,
            locale=self.translated_locale,
            entity__resource=self.main_db_resource,
            when=aware_datetime(1970, 1, 1)
        )
        changed_after.when = aware_datetime(1970, 1, 3)
        changed_after.save()

        sync_translations(self.db_project.pk, self.repository.pk,
                          self.project_sync_log.pk, self.now,
                          self.mock_changes)
        with assert_raises(ChangedEntityLocale.DoesNotExist):
            changed1.refresh_from_db()
        with assert_raises(ChangedEntityLocale.DoesNotExist):
            changed2.refresh_from_db()
        changed_after.refresh_from_db()  # Should not raise
Beispiel #17
0
 def test_no_commit(self):
     """Don't call commit_changes if command.no_commit is True."""
     sync_translations(self.db_project.pk, self.repository.pk,
                       self.project_sync_log.pk, self.now,
                       self.mock_changes, no_commit=True)
     assert_false(self.mock_commit_changes.called)