def test_query_args_filtering(self): """ Tests if query args are honored properly and contributors are filtered. """ locale_first, locale_second = LocaleFactory.create_batch(2) first_contributor = self.create_contributor_with_translation_counts( approved=12, unapproved=1, needs_work=2, locale=locale_first) second_contributor = self.create_contributor_with_translation_counts( approved=11, unapproved=1, needs_work=2, locale=locale_second) third_contributor = self.create_contributor_with_translation_counts( approved=10, unapproved=12, needs_work=2, locale=locale_first) # Testing filtering for the first locale top_contributors = User.translators.with_translation_counts(aware_datetime(2015, 1, 1), Q(translation__locale=locale_first)) assert_equal(top_contributors.count(), 2) assert_equal(top_contributors[0], third_contributor) assert_attributes_equal(top_contributors[0], translations_count=24, translations_approved_count=10, translations_unapproved_count=12, translations_needs_work_count=2) assert_equal(top_contributors[1], first_contributor) assert_attributes_equal(top_contributors[1], translations_count=15, translations_approved_count=12, translations_unapproved_count=1, translations_needs_work_count=2) # Testing filtering for the second locale top_contributors = User.translators.with_translation_counts(aware_datetime(2015, 1, 1), Q(translation__locale=locale_second)) assert_equal(top_contributors.count(), 1) assert_equal(top_contributors[0], second_contributor) assert_attributes_equal(top_contributors[0], translations_count=14, translations_approved_count=11, translations_unapproved_count=1, translations_needs_work_count=2)
def test_latest_datetime(self): larger = aware_datetime(2015, 1, 1) smaller = aware_datetime(2014, 1, 1) assert_is_none(latest_datetime([None, None, None])) assert_equal(latest_datetime([None, larger]), larger) assert_equal(latest_datetime([None, smaller, larger]), larger)
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
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))
def test_status(self): repo = RepositoryFactory.create() project_sync_log = ProjectSyncLogFactory.create( project__repositories=[repo], skipped=False ) # Repos aren't finished, status should be in-progress. assert project_sync_log.status == ProjectSyncLog.IN_PROGRESS # Once repo is finished, status should be synced. RepositorySyncLogFactory.create( repository=repo, project_sync_log=project_sync_log, start_time=aware_datetime(2015, 1, 1), end_time=aware_datetime(2015, 1, 1, 1), ) del project_sync_log.finished del project_sync_log.status assert project_sync_log.status == ProjectSyncLog.SYNCED # Skipped projects are just "skipped". skipped_log = ProjectSyncLogFactory.create( project__repositories=[repo], skipped=True, ) assert skipped_log.status == ProjectSyncLog.SKIPPED
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
def test_update_db_unapprove_existing(self): """ Any existing translations that don't match anything in VCS get unapproved, unless they were created after self.now. """ self.main_db_translation.approved = True self.main_db_translation.approved_date = aware_datetime(1970, 1, 1) self.main_db_translation.approved_user = UserFactory.create() self.main_db_translation.save() self.main_vcs_translation.strings[None] = 'New Translated String' created_after_translation = TranslationFactory.create( entity=self.main_db_entity, approved=True, approved_date=aware_datetime(1970, 1, 3) ) self.update_main_db_entity() self.main_db_translation.refresh_from_db() assert_attributes_equal( self.main_db_translation, approved=False, approved_user=None, approved_date=None ) created_after_translation.refresh_from_db() assert_attributes_equal( created_after_translation, approved=True, approved_date=aware_datetime(1970, 1, 3) )
def test_finished(self): sync_log = SyncLogFactory.create() # Create repo without existing log so sync is unfinished. repo = RepositoryFactory.create() project_sync_log = ProjectSyncLogFactory.create( sync_log=sync_log, project__repositories=[repo] ) # Sync isn't finished until all repos are finished. assert not sync_log.finished repo_log = RepositorySyncLogFactory.create( repository=repo, project_sync_log=project_sync_log, start_time=aware_datetime(2015, 1, 1), end_time=None, ) del sync_log.finished assert not sync_log.finished repo_log.end_time = aware_datetime(2015, 1, 2) repo_log.save() del sync_log.finished assert sync_log.finished
def test_finished(self): sync_log = SyncLogFactory.create() # Create repo without existing log so sync is unfinished. repo = RepositoryFactory.create() project_sync_log = ProjectSyncLogFactory.create( sync_log=sync_log, project__repositories=[repo]) # Sync isn't finished until all repos are finished. assert_false(sync_log.finished) repo_log = RepositorySyncLogFactory.create( repository=repo, project_sync_log=project_sync_log, start_time=aware_datetime(2015, 1, 1), end_time=None ) del sync_log.finished assert_false(sync_log.finished) repo_log.end_time = aware_datetime(2015, 1, 2) repo_log.save() del sync_log.finished assert_true(sync_log.finished)
def test_translation_save_latest_update_older_translation( locale_a, project_a, project_locale_a, resource_a, entity_a ): """ When an older translation is saved, do not update the latest_translation attribute on the related project, locale, translatedresource, and project_locale objects. """ tr = TranslatedResourceFactory.create(locale=locale_a, resource=resource_a) translation = TranslationFactory.create( locale=locale_a, entity=entity_a, date=aware_datetime(1970, 2, 1), ) for i in [locale_a, project_a, project_locale_a, tr]: i.refresh_from_db() assert locale_a.latest_translation == translation assert project_a.latest_translation == translation assert tr.latest_translation == translation assert project_locale_a.latest_translation == translation # older translation TranslationFactory.create( locale=locale_a, entity=entity_a, date=aware_datetime(1970, 1, 1), ) for i in [locale_a, project_a, project_locale_a, tr]: i.refresh_from_db() assert locale_a.latest_translation == translation assert project_a.latest_translation == translation assert tr.latest_translation == translation assert project_locale_a.latest_translation == translation
def test_status(self): repo = RepositoryFactory.create() project_sync_log = ProjectSyncLogFactory.create( project__repositories=[repo], skipped=False ) # Repos aren't finished, status should be in-progress. assert_equal(project_sync_log.status, ProjectSyncLog.IN_PROGRESS) # Once repo is finished, status should be synced. RepositorySyncLogFactory.create( repository=repo, project_sync_log=project_sync_log, start_time=aware_datetime(2015, 1, 1), end_time=aware_datetime(2015, 1, 1, 1), ) del project_sync_log.finished del project_sync_log.status assert_equal(project_sync_log.status, ProjectSyncLog.SYNCED) # Skipped projects are just "skipped". skipped_log = ProjectSyncLogFactory.create( project__repositories=[repo], skipped=True, ) assert_equal(skipped_log.status, ProjectSyncLog.SKIPPED)
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_project_repo(self.db_project.pk, self.repository.pk, self.project_sync_log.pk, self.now) # 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))
def _translation(self, user, submitted, approved): return TranslationFactory.create( date=aware_datetime(*submitted), user=user, approved_date=aware_datetime(*approved) if approved else None, approved_user=user )
def test_translation_save_latest_update_approved_translation( locale_a, project_a, project_locale_a, resource_a, entity_a): """ When a translation is approved, update the latest_translation attribute on the related project, locale, translatedresource, and project_locale objects if it was approved later than the last translation was saved before. """ tr = TranslatedResourceFactory.create(locale=locale_a, resource=resource_a) translation = TranslationFactory.create( locale=locale_a, entity=entity_a, date=aware_datetime(1970, 2, 1), approved_date=aware_datetime(1970, 2, 1), ) for i in [locale_a, project_a, project_locale_a, tr]: i.refresh_from_db() assert locale_a.latest_translation == translation assert project_a.latest_translation == translation assert tr.latest_translation == translation assert project_locale_a.latest_translation == translation later_approved_translation = TranslationFactory.create( locale=locale_a, entity=entity_a, date=aware_datetime(1970, 1, 1), approved_date=aware_datetime(1970, 3, 1), ) for i in [locale_a, project_a, project_locale_a, tr]: i.refresh_from_db() assert locale_a.latest_translation == later_approved_translation assert project_a.latest_translation == later_approved_translation assert tr.latest_translation == later_approved_translation assert project_locale_a.latest_translation == later_approved_translation
def test_save_latest_translation_update(self): """ When a translation is saved, update the latest_translation attribute on the related project, locale, stats, and project_locale objects. """ locale = LocaleFactory.create(latest_translation=None) project = ProjectFactory.create(locales=[locale], latest_translation=None) resource = ResourceFactory.create(project=project) stats = StatsFactory.create(locale=locale, resource=resource, latest_translation=None) project_locale = ProjectLocale.objects.get(locale=locale, project=project) assert_is_none(locale.latest_translation) assert_is_none(project.latest_translation) assert_is_none(stats.latest_translation) assert_is_none(project_locale.latest_translation) translation = TranslationFactory.create(locale=locale, entity__resource=resource, date=aware_datetime( 1970, 1, 1)) self.assert_latest_translation(locale, translation) self.assert_latest_translation(project, translation) self.assert_latest_translation(stats, translation) self.assert_latest_translation(project_locale, translation) # Ensure translation is replaced for newer translations newer_translation = TranslationFactory.create( locale=locale, entity__resource=resource, date=aware_datetime(1970, 2, 1)) self.assert_latest_translation(locale, newer_translation) self.assert_latest_translation(project, newer_translation) self.assert_latest_translation(stats, newer_translation) self.assert_latest_translation(project_locale, newer_translation) # Ensure translation isn't replaced for older translations. TranslationFactory.create(locale=locale, entity__resource=resource, date=aware_datetime(1970, 1, 5)) self.assert_latest_translation(locale, newer_translation) self.assert_latest_translation(project, newer_translation) self.assert_latest_translation(stats, newer_translation) self.assert_latest_translation(project_locale, newer_translation) # Ensure approved_date is taken into consideration as well. newer_approved_translation = TranslationFactory.create( locale=locale, entity__resource=resource, approved_date=aware_datetime(1970, 3, 1)) self.assert_latest_translation(locale, newer_approved_translation) self.assert_latest_translation(project, newer_approved_translation) self.assert_latest_translation(stats, newer_approved_translation) self.assert_latest_translation(project_locale, newer_approved_translation)
def test_save_latest_translation_update(self): """ When a translation is saved, update the latest_translation attribute on the related project, locale, stats, and project_locale objects. """ locale = LocaleFactory.create(latest_translation=None) project = ProjectFactory.create(locales=[locale], latest_translation=None) resource = ResourceFactory.create(project=project) stats = StatsFactory.create(locale=locale, resource=resource, latest_translation=None) project_locale = ProjectLocale.objects.get(locale=locale, project=project) assert_is_none(locale.latest_translation) assert_is_none(project.latest_translation) assert_is_none(stats.latest_translation) assert_is_none(project_locale.latest_translation) translation = TranslationFactory.create( locale=locale, entity__resource=resource, date=aware_datetime(1970, 1, 1) ) self.assert_latest_translation(locale, translation) self.assert_latest_translation(project, translation) self.assert_latest_translation(stats, translation) self.assert_latest_translation(project_locale, translation) # Ensure translation is replaced for newer translations newer_translation = TranslationFactory.create( locale=locale, entity__resource=resource, date=aware_datetime(1970, 2, 1) ) self.assert_latest_translation(locale, newer_translation) self.assert_latest_translation(project, newer_translation) self.assert_latest_translation(stats, newer_translation) self.assert_latest_translation(project_locale, newer_translation) # Ensure translation isn't replaced for older translations. TranslationFactory.create( locale=locale, entity__resource=resource, date=aware_datetime(1970, 1, 5) ) self.assert_latest_translation(locale, newer_translation) self.assert_latest_translation(project, newer_translation) self.assert_latest_translation(stats, newer_translation) self.assert_latest_translation(project_locale, newer_translation) # Ensure approved_date is taken into consideration as well. newer_approved_translation = TranslationFactory.create( locale=locale, entity__resource=resource, approved_date=aware_datetime(1970, 3, 1) ) self.assert_latest_translation(locale, newer_approved_translation) self.assert_latest_translation(project, newer_approved_translation) self.assert_latest_translation(stats, newer_approved_translation) self.assert_latest_translation(project_locale, newer_approved_translation)
def setUp(self): timezone_patch = patch.object(sync_projects, 'timezone') self.mock_timezone = timezone_patch.start() self.addCleanup(timezone_patch.stop) self.mock_timezone.now.return_value = aware_datetime(1970, 1, 1) self.translated_locale = LocaleFactory.create(code='translated-locale') self.inactive_locale = LocaleFactory.create(code='inactive-locale') self.repository = RepositoryFactory() self.db_project = ProjectFactory.create( name='db-project', locales=[self.translated_locale], repositories=[self.repository] ) self.main_db_resource = ResourceFactory.create( project=self.db_project, path='main.lang', format='lang' ) self.other_db_resource = ResourceFactory.create( project=self.db_project, path='other.lang', format='lang' ) self.missing_db_resource = ResourceFactory.create( project=self.db_project, path='missing.lang', format='lang' ) # Load paths from the fake locale directory. checkout_path_patch = patch.object( Project, 'checkout_path', new_callable=PropertyMock, return_value=FAKE_CHECKOUT_PATH ) checkout_path_patch.start() self.addCleanup(checkout_path_patch.stop) self.vcs_project = VCSProject(self.db_project) self.main_vcs_resource = self.vcs_project.resources[self.main_db_resource.path] self.other_vcs_resource = self.vcs_project.resources[self.other_db_resource.path] self.missing_vcs_resource = self.vcs_project.resources[self.missing_db_resource.path] self.main_vcs_entity = self.main_vcs_resource.entities['Source String'] self.main_vcs_translation = self.main_vcs_entity.translations['translated-locale'] # Mock VCSResource.save() for each resource to avoid altering # the filesystem. resource_save_patch = patch.object(VCSResource, 'save') resource_save_patch.start() self.addCleanup(resource_save_patch.stop) self.changeset = sync_projects.ChangeSet( self.db_project, self.vcs_project, aware_datetime(1970, 1, 1) )
def test_given_period(self): """ Checks if view sets and returns data for right period. """ with patch('django.utils.timezone.now', wraps=now, return_value=aware_datetime(2015, 7, 5)): self.client.get('/contributors/?period=6') assert_equal(self.mock_render.call_args[0][0]['period'], 6) assert_equal(self.mock_translations_manager.call_args[0][0], aware_datetime(2015, 1, 5))
def test_end_time_skipped(self): """Include skipped repos in finding the latest end time.""" sync_log = SyncLogFactory.create() RepositorySyncLogFactory.create(project_sync_log__sync_log=sync_log, end_time=aware_datetime(2015, 1, 1)) ProjectSyncLogFactory.create(sync_log=sync_log, skipped=True, skipped_end_time=aware_datetime(2015, 1, 2)) ProjectSyncLogFactory.create(sync_log=sync_log, skipped=True, skipped_end_time=aware_datetime(2015, 1, 4)) assert_equal(sync_log.end_time, aware_datetime(2015, 1, 4))
def test_sync_log(self): """Create a new sync log when command is run.""" assert not SyncLog.objects.exists() ProjectFactory.create() with patch.object(sync_projects, "timezone") as mock_timezone: mock_timezone.now.return_value = aware_datetime(2015, 1, 1) self.execute_command() sync_log = SyncLog.objects.all()[0] assert sync_log.start_time == aware_datetime(2015, 1, 1)
def test_sync_log(self): """Create a new sync log when command is run.""" assert_false(SyncLog.objects.exists()) ProjectFactory.create() with patch.object(sync_projects, 'timezone') as mock_timezone: mock_timezone.now.return_value = aware_datetime(2015, 1, 1) self.execute_command() sync_log = SyncLog.objects.all()[0] assert_equal(sync_log.start_time, aware_datetime(2015, 1, 1))
def test_end_time_skipped(self): """ If a sync is skipped, it's end_time is self.skipped_end_time. """ repo = RepositoryFactory.create() project_sync_log = ProjectSyncLogFactory.create( project__repositories=[repo], skipped=True, skipped_end_time=aware_datetime(2015, 1, 1), ) assert_equal(project_sync_log.end_time, aware_datetime(2015, 1, 1))
def test_end_time(self): """ Return the latest end time among repo sync logs for this log. """ sync_log = SyncLogFactory.create() RepositorySyncLogFactory.create(project_sync_log__sync_log=sync_log, end_time=aware_datetime(2015, 1, 1)) RepositorySyncLogFactory.create(project_sync_log__sync_log=sync_log, end_time=aware_datetime(2015, 1, 2)) assert_equal(sync_log.end_time, aware_datetime(2015, 1, 2))
def test_given_period(member, mock_contributors_render, mock_users_translations_counts): """ Checks if view sets and returns data for right period. """ with patch( "django.utils.timezone.now", wraps=now, return_value=aware_datetime(2015, 7, 5), ): member.client.get("/contributors/?period=6") assert mock_contributors_render.call_args[0][0]["period"] == 6 assert mock_users_translations_counts.call_args[0][0] == aware_datetime( 2015, 1, 5 )
def test_end_time(self): """ Return the latest end time among repo sync logs for this log. """ project = ProjectFactory.create(repositories=[]) source_repo, repo1, repo2 = RepositoryFactory.create_batch(3, project=project) project_sync_log = ProjectSyncLogFactory.create(project=project) RepositorySyncLogFactory.create(project_sync_log=project_sync_log, repository=repo1, end_time=aware_datetime(2015, 1, 1)) assert_equal(project_sync_log.end_time, aware_datetime(2015, 1, 1))
def test_update_db_existing_translation(self): """ Update an existing translation in the DB with changes from VCS. """ # Set up DB and VCS to differ and require an update. self.main_db_translation.fuzzy = True self.main_db_translation.extra = {} self.main_db_translation.save() self.main_vcs_entity.key = "Source String" self.main_vcs_entity.comments = ["first comment", "second"] self.main_vcs_entity.order = 7 self.main_vcs_entity.string_plural = "plural string" self.main_vcs_entity.source = ["foo.py:87"] self.main_vcs_translation.fuzzy = False # The test translation is from a langfile so we can use tags # for testing extra. self.main_vcs_translation.tags = set(["ok"]) self.update_main_db_entity() self.main_db_entity.refresh_from_db() assert_attributes_equal( self.main_db_entity, key="Source String", comment="first comment\nsecond", order=7, string_plural="plural string", source=["foo.py:87"], date_updated=aware_datetime(1970, 1, 1), ) self.main_db_translation.refresh_from_db() assert_attributes_equal( self.main_db_translation, fuzzy=False, extra={"tags": ["ok"]} )
def create_db_entities_translations(self): """ Create entities and translations in the database for strings from the fake checkout. """ self.main_db_entity = EntityFactory.create( resource=self.main_db_resource, string='Source String', key='Source String', obsolete=False ) self.other_db_entity = EntityFactory.create( resource=self.other_db_resource, string='Other Source String', key='Other Source String', obsolete=False ) self.main_db_translation = TranslationFactory.create( entity=self.main_db_entity, plural_form=None, locale=self.translated_locale, string='Translated String', date=aware_datetime(1970, 1, 1), approved=True, extra={'tags': []} )
def test_translation_save_latest_missing_project_locale(locale_a, project_a): """ If a translation is saved for a locale that isn't active on the project, do not fail due to a missing ProjectLocale. """ resource = ResourceFactory.create( project=project_a, path="resource.po", format="po", ) tr = TranslatedResourceFactory.create(locale=locale_a, resource=resource) entity = EntityFactory.create(resource=resource, string="Entity X") # This calls .save, this should fail if we're not properly # handling the missing ProjectLocale. translation = TranslationFactory.create( locale=locale_a, entity=entity, date=aware_datetime(1970, 1, 1), ) locale_a.refresh_from_db() project_a.refresh_from_db() tr.refresh_from_db() assert locale_a.latest_translation == translation assert project_a.latest_translation == translation assert tr.latest_translation == translation
def test_obsolete_entities_asymmetric_resources_sync(self): """ Synchronization should modify asymmetric resources files if their entities were made obsolete, even if their entities weren't changed. """ TranslationFactory.create(locale=self.translated_locale, entity=self.main_db_entity, approved=True, date=aware_datetime(2015, 1, 1)) resource_file = MagicMock() self.changeset.vcs_project.resources = { self.main_db_entity.resource.path: resource_file } # Entity must be made obsolete self.changeset.changes['obsolete_vcs_entities'] = [ self.main_db_entity.pk ] # Resource file format must be asymmetric self.main_db_entity.resource.format = 'dtd' self.main_db_entity.resource.save() with patch.object(self.main_db_entity, 'has_changed', return_value=False) as mock_has_changed: self.changeset.update_vcs_entity(self.translated_locale, self.main_db_entity, MagicMock()) self.changeset.execute_update_vcs() assert mock_has_changed.called assert resource_file.save.called
def test_no_new_translations(self): """ Don't change any resource if there aren't any new translations. """ TranslationFactory.create( locale=self.translated_locale, entity=self.main_db_entity, approved=True, date=aware_datetime(2015, 1, 1) ) with patch.object( self.main_db_entity, 'has_changed', return_value=False ) as mock_has_changed: resource_file = MagicMock() self.changeset.update_vcs_entity( self.translated_locale, self.main_db_entity, MagicMock() ) self.changeset.vcs_project.resources = { self.main_db_entity.resource.path: resource_file } self.changeset.execute_update_vcs() assert mock_has_changed.called assert not resource_file.save.called
def test_create_db(self): """Create new entity in the database.""" self.main_db_entity.delete() self.main_vcs_entity.key = 'Source String' self.main_vcs_entity.comments = ['first comment', 'second'] self.main_vcs_entity.order = 7 self.main_vcs_translation.fuzzy = False self.main_vcs_entity.string_plural = 'plural string' self.main_vcs_entity.source = ['foo.py:87'] self.changeset.create_db_entity(self.main_vcs_entity) self.changeset.execute() new_entity = Entity.objects.get( resource__path=self.main_vcs_resource.path, string=self.main_vcs_entity.string) assert_attributes_equal( new_entity, resource=self.main_db_resource, string='Source String', key='Source String', comment='first comment\nsecond', order=7, string_plural='plural string', source=['foo.py:87'], ) new_translation = new_entity.translation_set.all()[0] assert_attributes_equal(new_translation, locale=self.translated_locale, string='Translated String', plural_form=None, approved=True, approved_date=aware_datetime(1970, 1, 1), fuzzy=False)
def test_update_db_unapprove_fuzzy(self): """ If an existing translation is fuzzy and doesn't match anything in VCS, unapprove and unfuzzy that translation without rejecting it. """ self.main_db_translation.fuzzy = True self.main_db_translation.approved = True self.main_db_translation.approved_date = aware_datetime(1970, 1, 1) self.main_db_translation.approved_user = UserFactory.create() self.main_db_translation.save() self.main_vcs_translation.strings[None] = "New Translated String" self.update_main_db_entity() self.main_db_translation.refresh_from_db() assert_attributes_equal( self.main_db_translation, approved=False, approved_user=None, approved_date=None, rejected=False, fuzzy=False, ) assert ActionLog.objects.filter( action_type="translation:unapproved", translation=self.main_db_translation.pk, ).exists()
def test_obsolete_entities_asymmetric_resources_sync(self): """ Synchronization should modify asymmetric resources files if their entities were made obsolete, even if their entities weren't changed. """ TranslationFactory.create(locale=self.translated_locale, entity=self.main_db_entity, approved=True, date=aware_datetime(2015, 1, 1)) resource_file = MagicMock() self.changeset.vcs_project.resources = { self.main_db_entity.resource.path: resource_file } # Entity must be made obsolete self.changeset.changes['obsolete_vcs'] = [self.main_db_entity.pk] # Resource file format must be asymmetric self.main_db_entity.resource.format = 'dtd' self.main_db_entity.resource.save() with patch.object(self.main_db_entity, 'has_changed', return_value=False) as mock_has_changed: self.changeset.update_vcs_entity(self.translated_locale, self.main_db_entity, MagicMock()) self.changeset.execute_update_vcs() assert mock_has_changed.called assert resource_file.save.called
def test_unchanged_resources_sync(self): """ Synchronization shouldn't modify resources if their entities weren't changed. """ TranslationFactory.create( locale=self.translated_locale, entity=self.main_db_entity, approved=True, date=aware_datetime(2015, 1, 1) ) resource_file = MagicMock() self.changeset.vcs_project.resources = { self.main_db_entity.resource.path: resource_file } with patch.object( self.main_db_entity, 'has_changed', return_value=False ) as mock_has_changed: self.changeset.update_vcs_entity( self.translated_locale, self.main_db_entity, MagicMock() ) self.changeset.execute_update_vcs() assert mock_has_changed.called assert len(resource_file.save.mock_calls) == 0
def test_finished(self): log = RepositorySyncLogFactory.create(end_time=None) assert_false(log.finished) log.end_time = aware_datetime(2015, 1, 1) log.save() assert_true(log.finished)
def test_helper_to_json(): obj = { 'a': 'foo', 'b': aware_datetime(2015, 1, 1), } string = '{"a": "foo", "b": "2015-01-01T00:00:00+00:00"}' assert to_json(obj) == string
def test_translation_save_latest_update_for_system_project(locale_a, system_project_a): """ When a translation is saved for a system project, update the latest_translation attribute on the project, translatedresource and project_locale objects, but not on the locale object. """ project_locale = ProjectLocaleFactory.create( project=system_project_a, locale=locale_a, ) resource = ResourceFactory.create( project=system_project_a, path="resource.po", format="po", ) tr = TranslatedResourceFactory.create(locale=locale_a, resource=resource) entity = EntityFactory.create(resource=resource, string="Entity X") assert locale_a.latest_translation is None assert system_project_a.latest_translation is None assert tr.latest_translation is None assert project_locale.latest_translation is None translation = TranslationFactory.create( locale=locale_a, entity=entity, date=aware_datetime(1970, 1, 1), ) for i in [locale_a, system_project_a, project_locale, tr]: i.refresh_from_db() assert locale_a.latest_translation is None assert system_project_a.latest_translation == translation assert tr.latest_translation == translation assert project_locale.latest_translation == translation
def test_helper_base_format_dt_builtin(settings): """ Test that there exist built-in formats. We're not interested in testing "all" of them, just that the capability exists. """ settings.TIME_ZONE = 'UTC' datetime = aware_datetime(2015, 1, 1, 5, 7) assert format_datetime(datetime, 'time') == '05:07 UTC'
def test_format_builtin(self): """ Test that there exist built-in formats. We're not interested in testing "all" of them, just that the capability exists. """ with self.settings(TIME_ZONE='UTC'): datetime = aware_datetime(2015, 1, 1, 5, 7) assert_equal(format_datetime(datetime, 'time'), '05:07 UTC')
def test_finished(self): repo = RepositoryFactory.create() project_sync_log = ProjectSyncLogFactory.create( project__repositories=[repo]) # Sync isn't finished until all repos are finished. assert_false(project_sync_log.finished) repo_log = RepositorySyncLogFactory.create( repository=repo, project_sync_log=project_sync_log, start_time=aware_datetime(2015, 1, 1), end_time=None) assert_false(project_sync_log.finished) repo_log.end_time = aware_datetime(2015, 1, 2) repo_log.save() assert_true(project_sync_log.finished)
def test_finished(self): repo = RepositoryFactory.create() project_sync_log = ProjectSyncLogFactory.create(project__repositories=[repo]) # Sync isn't finished until all repos are finished. assert_false(project_sync_log.finished) repo_log = RepositorySyncLogFactory.create( repository=repo, project_sync_log=project_sync_log, start_time=aware_datetime(2015, 1, 1), end_time=None ) assert_false(project_sync_log.finished) repo_log.end_time = aware_datetime(2015, 1, 2) repo_log.save() assert_true(project_sync_log.finished)
def test_save_extra_metadata(self): """ If last_updated or last_translator is set on the latest translation, update the metadata for those fields. """ test_input = self.generate_pofile( dedent(""" msgid "Latest" msgstr "Latest" msgid "Older" msgstr "Older" """), revision_date='2012-01-01 00:00+0000', last_translator='last <*****@*****.**>' ) path, resource = self.parse_string(test_input) latest_translation, older_translation = resource.translations latest_translation.last_updated = aware_datetime(2015, 1, 1, 0, 0, 0) latest_translation.last_translator = UserFactory( first_name='New', email='*****@*****.**' ) older_translation.last_updated = aware_datetime(1970, 1, 1, 0, 0, 0) older_translation.last_translator = UserFactory( first_name='Old', email='*****@*****.**' ) resource.save(self.locale) self.assert_file_content(path, self.generate_pofile( dedent(""" msgid "Latest" msgstr "Latest" msgid "Older" msgstr "Older" """), revision_date='2015-01-01 00:00+0000', last_translator='New <*****@*****.**>' ))
def test_no_translations(self): """ We don't attribute anyone if there aren't any new translations. """ TranslationFactory.create(locale=self.translated_locale, entity=self.main_db_entity, approved=True, date=aware_datetime(2015, 1, 1)) with patch.object(self.main_db_entity, 'has_changed', return_value=False): self.changeset.update_vcs_entity(self.translated_locale, self.main_db_entity, MagicMock()) self.changeset.execute_update_vcs() assert_equal(self.changeset.commit_authors_per_locale[self.translated_locale.code], [])
def test_period_filters(self): """ Total counts should be filtered by given date. Test creates 2 contributors with different activity periods and checks if they are filtered properly. """ first_contributor = self.create_contributor_with_translation_counts(approved=12, unapproved=1, needs_work=2, date=aware_datetime(2015, 3, 2)) # Second contributor self.create_contributor_with_translation_counts(approved=2, unapproved=11, needs_work=2, date=aware_datetime(2015, 6, 1)) TranslationFactory.create_batch(5, approved=True, user=first_contributor, date=aware_datetime(2015, 7, 2)) top_contributors = User.translators.with_translation_counts(aware_datetime(2015, 6, 10)) assert_equal(top_contributors.count(), 1) assert_attributes_equal(top_contributors[0], translations_count=5, translations_approved_count=5, translations_unapproved_count=0, translations_needs_work_count=0) top_contributors = User.translators.with_translation_counts(aware_datetime(2015, 5, 10)) assert_equal(top_contributors.count(), 2) assert_attributes_equal(top_contributors[0], translations_count=15, translations_approved_count=2, translations_unapproved_count=11, translations_needs_work_count=2) assert_attributes_equal(top_contributors[1], translations_count=5, translations_approved_count=5, translations_unapproved_count=0, translations_needs_work_count=0) top_contributors = User.translators.with_translation_counts(aware_datetime(2015, 1, 10)) assert_equal(top_contributors.count(), 2) assert_attributes_equal(top_contributors[0], translations_count=20, translations_approved_count=17, translations_unapproved_count=1, translations_needs_work_count=2) assert_attributes_equal(top_contributors[1], translations_count=15, translations_approved_count=2, translations_unapproved_count=11, translations_needs_work_count=2)
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