Esempio n. 1
0
    def test_handle_entities(self):
        """Call handle_entity on all matching Entity and VCSEntity pairs."""
        vcs_entities = {
            'match': VCSEntityFactory(),
            'no_db_match': VCSEntityFactory()
        }
        db_entities = {
            'match': EntityFactory(),
            'no_vcs_match': EntityFactory()
        }

        with patch('pontoon.sync.core.get_vcs_entities', return_value=vcs_entities), \
             patch('pontoon.sync.core.get_db_entities', return_value=db_entities), \
             patch('pontoon.sync.core.handle_entity') as mock_handle_entity:
            sync_project(self.db_project)

        mock_handle_entity.assert_has_calls([
            call(ANY, self.db_project, 'match', db_entities['match'],
                 vcs_entities['match']),
            call(ANY, self.db_project, 'no_vcs_match',
                 db_entities['no_vcs_match'], None),
            call(ANY, self.db_project, 'no_db_match', None,
                 vcs_entities['no_db_match']),
        ],
                                            any_order=True)
Esempio n. 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.core.VCSProject', return_value=self.vcs_project):
            sync_project(self.db_project)

        # 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))
Esempio n. 3
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.core.VCSProject',
                   return_value=self.vcs_project):
            sync_project(self.db_project)

        # 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))
Esempio n. 4
0
    def test_reset_project_has_changed(self):
        """After syncing, set db_project.has_changed to False."""
        self.db_project.has_changed = True
        self.db_project.save()

        sync_project(self.db_project)
        self.db_project.refresh_from_db()
        assert_false(self.db_project.has_changed)
Esempio n. 5
0
    def test_reset_project_has_changed(self):
        """After syncing, set db_project.has_changed to False."""
        self.db_project.has_changed = True
        self.db_project.save()

        sync_project(self.db_project)
        self.db_project.refresh_from_db()
        assert_false(self.db_project.has_changed)
Esempio n. 6
0
    def test_db_changed_no_repo_changed(self):
        """
        If the database has changes and VCS doesn't, do not skip syncing
        the project.
        """
        self.mock_pull_changes.return_value = False
        self.mock_project_needs_sync.return_value = True

        with patch('pontoon.sync.core.handle_entity') as mock_handle_entity:
            sync_project(self.db_project)
        assert_true(mock_handle_entity.called)
Esempio n. 7
0
    def test_db_changed_no_repo_changed(self):
        """
        If the database has changes and VCS doesn't, do not skip syncing
        the project.
        """
        self.mock_pull_changes.return_value = False
        self.mock_project_needs_sync.return_value = True

        with patch('pontoon.sync.core.handle_entity') as mock_handle_entity:
            sync_project(self.db_project)
        assert_true(mock_handle_entity.called)
Esempio n. 8
0
    def test_no_changes_skip(self):
        """
        If the database and VCS both have no changes, skip sync and log
        a message.
        """
        self.mock_pull_changes.return_value = False
        self.mock_project_needs_sync.return_value = False

        with patch('pontoon.sync.core.log') as mock_log, \
             patch('pontoon.sync.core.handle_entity') as mock_handle_entity:
            sync_project(self.db_project)

        assert_false(mock_handle_entity.called)
        mock_log.info.assert_called_with(
            CONTAINS('Skipping', self.db_project.slug))
Esempio n. 9
0
    def test_no_changes_skip(self):
        """
        If the database and VCS both have no changes, skip sync and log
        a message.
        """
        self.mock_pull_changes.return_value = False
        self.mock_project_needs_sync.return_value = False

        with patch('pontoon.sync.core.log') as mock_log, \
             patch('pontoon.sync.core.handle_entity') as mock_handle_entity:
            sync_project(self.db_project)

        assert_false(mock_handle_entity.called)
        mock_log.info.assert_called_with(
            CONTAINS('Skipping', self.db_project.slug)
        )
Esempio n. 10
0
    def test_handle_entities(self):
        """Call handle_entity on all matching Entity and VCSEntity pairs."""
        vcs_entities = {
            'match': VCSEntityFactory(),
            'no_db_match': VCSEntityFactory()
        }
        db_entities = {
            'match': EntityFactory(),
            'no_vcs_match': EntityFactory()
        }

        with patch('pontoon.sync.core.get_vcs_entities', return_value=vcs_entities), \
             patch('pontoon.sync.core.get_db_entities', return_value=db_entities), \
             patch('pontoon.sync.core.handle_entity') as mock_handle_entity:
            sync_project(self.db_project)

        mock_handle_entity.assert_has_calls([
            call(ANY, self.db_project, 'match', db_entities['match'], vcs_entities['match']),
            call(ANY, self.db_project, 'no_vcs_match', db_entities['no_vcs_match'], None),
            call(ANY, self.db_project, 'no_db_match', None, vcs_entities['no_db_match']),
        ], any_order=True)
Esempio n. 11
0
    def test_clear_changed_entities(self):
        """
        Delete all ChangedEntityLocale objects for the project created
        before the sync started after handling it.
        """
        self.mock_timezone.return_value = aware_datetime(1970, 1, 2)
        changed1, changed2, changed_after = ChangedEntityLocaleFactory.create_batch(3,
            locale=self.translated_locale,
            entity__resource=self.main_db_resource,
            entity__resource__project=self.db_project,
            when=aware_datetime(1970, 1, 1)
        )
        changed_after.when = aware_datetime(1970, 1, 3)
        changed_after.save()

        sync_project(self.db_project)
        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
Esempio n. 12
0
    def test_clear_changed_entities(self):
        """
        Delete all ChangedEntityLocale objects for the project created
        before the sync started after handling it.
        """
        self.mock_timezone.return_value = aware_datetime(1970, 1, 2)
        changed1, changed2, changed_after = ChangedEntityLocaleFactory.create_batch(
            3,
            locale=self.translated_locale,
            entity__resource=self.main_db_resource,
            entity__resource__project=self.db_project,
            when=aware_datetime(1970, 1, 1))
        changed_after.when = aware_datetime(1970, 1, 3)
        changed_after.save()

        sync_project(self.db_project)
        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
Esempio n. 13
0
 def test_no_pull(self):
     """
     Don't call repo.pull if command.no_pull is True.
     """
     sync_project(self.db_project, no_pull=True)
     assert_false(self.mock_pull_changes.called)
Esempio n. 14
0
 def test_no_commit(self):
     """Don't call commit_changes if command.no_commit is True."""
     with patch('pontoon.sync.core.commit_changes') as mock_commit_changes:
         sync_project(self.db_project, no_commit=True)
     assert_false(mock_commit_changes.called)
Esempio n. 15
0
 def test_no_pull(self):
     """
     Don't call repo.pull if command.no_pull is True.
     """
     sync_project(self.db_project, no_pull=True)
     assert_false(self.mock_pull_changes.called)
Esempio n. 16
0
 def test_no_commit(self):
     """Don't call commit_changes if command.no_commit is True."""
     with patch('pontoon.sync.core.commit_changes') as mock_commit_changes:
         sync_project(self.db_project, no_commit=True)
     assert_false(mock_commit_changes.called)