def test_locales_parts_stats_pages_tied_to_resources(self): """ Return subpage name and stats for locales resources are available for. """ resource_other = ResourceFactory.create(project=self.project, path='/other/path.po') EntityFactory.create(resource=resource_other) StatsFactory.create(resource=resource_other, locale=self.locale) StatsFactory.create(resource=resource_other, locale=self.locale_other) SubpageFactory.create(project=self.project, name='Subpage', resources=[self.resource]) SubpageFactory.create(project=self.project, name='Other Subpage', resources=[resource_other]) project_details = self._fetch_locales_parts_stats() details = project_details.get(self.locale.code) details_other = project_details.get(self.locale_other.code) assert_equal(details[0]['resource__path'], 'Other Subpage') assert_equal(details[0]['translated_count'], 0) assert_equal(details[1]['resource__path'], 'Subpage') assert_equal(details[1]['translated_count'], 0) assert_equal(details_other[0]['resource__path'], 'Other Subpage') assert_equal(details_other[0]['translated_count'], 0)
def test_locales_parts_stats_pages_tied_to_resources(self): """ Return subpage name and stats for locales resources are available for. """ resource_other = ResourceFactory.create( project=self.project, path='/other/path.po' ) EntityFactory.create(resource=resource_other) StatsFactory.create(resource=resource_other, locale=self.locale) StatsFactory.create(resource=resource_other, locale=self.locale_other) SubpageFactory.create( project=self.project, name='Subpage', resources=[self.resource] ) SubpageFactory.create( project=self.project, name='Other Subpage', resources=[resource_other] ) project_details = self._fetch_locales_parts_stats() details = project_details.get(self.locale.code) details_other = project_details.get(self.locale_other.code) assert_equal(details[0]['resource__path'], 'Other Subpage') assert_equal(details[0]['translated_count'], 0) assert_equal(details[1]['resource__path'], 'Subpage') assert_equal(details[1]['translated_count'], 0) assert_equal(details_other[0]['resource__path'], 'Other Subpage') assert_equal(details_other[0]['translated_count'], 0)
def test_latest_activity(self): """Ensure that the latest_activity field is added to parts.""" locale = LocaleFactory.create(code='test') project = ProjectFactory.create(locales=[locale], slug='test-project') resource = ResourceFactory.create(project=project, path='has/stats.po') translation = TranslationFactory.create(entity__resource=resource, locale=locale) StatsFactory.create(resource=resource, locale=locale, latest_translation=translation) with patch.object(Project, 'locales_parts_stats') as mock_locales_parts_stats, \ patch('pontoon.base.views.render') as mock_render: mock_locales_parts_stats.return_value = [{ 'resource__path': 'has/stats.po' }, { 'resource__path': 'no/stats.po' }] views.locale_project(self.factory.get('/'), locale='test', slug='test-project') ctx = mock_render.call_args[0][2] assert_equal(ctx['parts'], [{ 'resource__path': 'has/stats.po', 'latest_activity': translation }, { 'resource__path': 'no/stats.po', 'latest_activity': None }])
def setUp(self): self.locale, self.locale_other = LocaleFactory.create_batch(2) self.project = ProjectFactory.create( locales=[self.locale, self.locale_other]) self.resource = ResourceFactory.create(project=self.project, path='/main/path.po') EntityFactory.create(resource=self.resource) StatsFactory.create(resource=self.resource, locale=self.locale)
def setUp(self): self.locale, self.locale_other = LocaleFactory.create_batch(2) self.project = ProjectFactory.create( locales=[self.locale, self.locale_other] ) self.resource = ResourceFactory.create( project=self.project, path='/main/path.po' ) EntityFactory.create(resource=self.resource) StatsFactory.create(resource=self.resource, locale=self.locale)
def test_not_authed_public_project(self): """ If the user is not authenticated and we're translating project ID 1, return a 200. """ # Clear out existing project with ID=1 if necessary. Project.objects.filter(id=1).delete() locale = LocaleFactory.create(code='fakelocale') project = ProjectFactory.create(id=1, slug='valid-project', locales=[locale]) resource = ResourceFactory.create(project=project, path='foo.lang', entity_count=1) StatsFactory.create(resource=resource, locale=locale) response = self.client.get('/fakelocale/valid-project/foo.lang/') assert_equal(response.status_code, 200)
def test_no_subpage_one_stats_in_current_locale(self): """ If there is just one stats for a resource available in the current locale, and no subpages, set the part to the resource path. """ locale = LocaleFactory.create() project = ProjectFactory.create(locales=[locale]) resource = ResourceFactory.create(project=project, path='foo.lang', entity_count=1) StatsFactory.create(resource=resource, locale=locale) self.client_login() url = '/{}/'.format('/'.join([locale.code, project.slug, resource.path])) with patch('pontoon.base.views.render', wraps=render) as mock_render: self.client.get(url) assert_equal(mock_render.call_args[0][2]['part'], 'foo.lang')
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 test_no_subpage_no_stats_in_current_locale(self): """ If there are stats for a resource available in other locales but not in the current one, and no subpages, do not set ctx['part']. """ locale, locale_no_stats = LocaleFactory.create_batch(2) project = ProjectFactory.create(locales=[locale, locale_no_stats]) resource = ResourceFactory.create(project=project, path='foo.lang', entity_count=1) ResourceFactory.create(project=project, entity_count=1) StatsFactory.create(resource=resource, locale=locale) self.client_login() url = '/{}/'.format('/'.join([locale_no_stats.code, project.slug, resource.path])) with patch('pontoon.base.views.render', wraps=render) as mock_render: self.client.get(url) assert_true('part' not in mock_render.call_args[0][2])
def test_no_subpage_one_stats_in_current_locale(self): """ If there is just one stats for a resource available in the current locale, and no subpages, do not set ctx['part']. """ locale = LocaleFactory.create() project = ProjectFactory.create(locales=[locale]) # Need two resources to trigger setting the part value. resource = ResourceFactory.create(project=project, path='foo.lang', entity_count=1) ResourceFactory.create(project=project, entity_count=1) StatsFactory.create(resource=resource, locale=locale) self.client_login() url = '/{locale.code}/{project.slug}/'.format(locale=locale, project=project) with patch('pontoon.base.views.render', wraps=render) as mock_render: self.client.get(url) assert_true('part' not in mock_render.call_args[0][2])
def test_parts_stats_no_page_multiple_resources(self): """ Return resource paths and stats for locales resources are available for. """ resource_other = ResourceFactory.create(project=self.project, path="/other/path.po") EntityFactory.create(resource=resource_other) StatsFactory.create(resource=resource_other, locale=self.locale) StatsFactory.create(resource=resource_other, locale=self.locale_other) details = self.locale.parts_stats(self.project) details_other = self.locale_other.parts_stats(self.project) assert_equal(details[0]["resource__path"], "/main/path.po") assert_equal(details[0]["translated_count"], 0) assert_equal(details[1]["resource__path"], "/other/path.po") assert_equal(details[1]["translated_count"], 0) assert_equal(len(details_other), 1) assert_equal(details_other[0]["resource__path"], "/other/path.po") assert_equal(details_other[0]["translated_count"], 0)
def test_no_subpage_multiple_stats_in_current_locale(self): """ If there are multiple stats for a resource available in the current locale, and no subpages, set the part to the resource path. """ locale = LocaleFactory.create() project = ProjectFactory.create(locales=[locale]) # Need at least two resources and stats to trigger setting the part value. resource1 = ResourceFactory.create(project=project, path='foo1.lang', entity_count=1) resource2 = ResourceFactory.create(project=project, path='foo2.lang', entity_count=1) StatsFactory.create(resource=resource1, locale=locale) StatsFactory.create(resource=resource2, locale=locale) self.client_login() url = '/{locale.code}/{project.slug}/'.format(locale=locale, project=project) with patch('pontoon.base.views.render', wraps=render) as mock_render: self.client.get(url) assert_equal(mock_render.call_args[0][2]['part'], 'foo1.lang')
def test_parts_stats_pages_tied_to_resources(self): """ Return subpage name and stats for locales resources are available for. """ resource_other = ResourceFactory.create(project=self.project, path="/other/path.po") EntityFactory.create(resource=resource_other) StatsFactory.create(resource=resource_other, locale=self.locale) StatsFactory.create(resource=resource_other, locale=self.locale_other) SubpageFactory.create(project=self.project, name="Subpage", resources=[self.resource]) SubpageFactory.create(project=self.project, name="Other Subpage", resources=[resource_other]) details = self.locale.parts_stats(self.project) details_other = self.locale_other.parts_stats(self.project) assert_equal(details[0]["resource__path"], "Other Subpage") assert_equal(details[0]["translated_count"], 0) assert_equal(details[1]["resource__path"], "Subpage") assert_equal(details[1]["translated_count"], 0) assert_equal(details_other[0]["resource__path"], "Other Subpage") assert_equal(details_other[0]["translated_count"], 0)
def test_parts_stats_no_page_multiple_resources(self): """ Return resource paths and stats for locales resources are available for. """ resource_other = ResourceFactory.create(project=self.project, path='/other/path.po') EntityFactory.create(resource=resource_other) StatsFactory.create(resource=resource_other, locale=self.locale) StatsFactory.create(resource=resource_other, locale=self.locale_other) details = self.locale.parts_stats(self.project) details_other = self.locale_other.parts_stats(self.project) assert_equal(details[0]['resource__path'], '/main/path.po') assert_equal(details[0]['translated_count'], 0) assert_equal(details[1]['resource__path'], '/other/path.po') assert_equal(details[1]['translated_count'], 0) assert_equal(len(details_other), 1) assert_equal(details_other[0]['resource__path'], '/other/path.po') assert_equal(details_other[0]['translated_count'], 0)
def test_latest_activity(self): """Ensure that the latest_activity field is added to parts.""" locale = LocaleFactory.create(code='test') project = ProjectFactory.create(locales=[locale], slug='test-project') resource = ResourceFactory.create(project=project, path='has/stats.po') translation = TranslationFactory.create(entity__resource=resource, locale=locale) StatsFactory.create(resource=resource, locale=locale, latest_translation=translation) with patch.object(Project, 'locales_parts_stats') as mock_locales_parts_stats, \ patch('pontoon.base.views.render') as mock_render: mock_locales_parts_stats.return_value = [ {'resource__path': 'has/stats.po'}, {'resource__path': 'no/stats.po'} ] views.locale_project(self.factory.get('/'), locale='test', slug='test-project') ctx = mock_render.call_args[0][2] assert_equal(ctx['parts'], [ {'resource__path': 'has/stats.po', 'latest_activity': translation}, {'resource__path': 'no/stats.po', 'latest_activity': None} ])
def test_no_subpage_no_stats_in_current_locale(self): """ If there are stats for a resource available in other locales but not in the current one, and no subpages, do not set ctx['part']. """ locale, locale_no_stats = LocaleFactory.create_batch(2) project = ProjectFactory.create(locales=[locale, locale_no_stats]) # Need two resources to trigger setting the part value. resource = ResourceFactory.create(project=project, path='foo.lang', entity_count=1) ResourceFactory.create(project=project, entity_count=1) StatsFactory.create(resource=resource, locale=locale) self.client_login() url = '/{locale.code}/{project.slug}/'.format(locale=locale_no_stats, project=project) with patch('pontoon.base.views.render', wraps=render) as mock_render: self.client.get(url) assert_true('part' not in mock_render.call_args[0][2])
def test_locales_parts_stats_no_page_multiple_resources(self): """ Return resource paths and stats for locales resources are available for. """ resource_other = ResourceFactory.create( project=self.project, path='/other/path.po' ) EntityFactory.create(resource=resource_other) StatsFactory.create(resource=resource_other, locale=self.locale) StatsFactory.create(resource=resource_other, locale=self.locale_other) project_details = self._fetch_locales_parts_stats() details = project_details.get(self.locale.code) details_other = project_details.get(self.locale_other.code) assert_equal(details[0]['resource__path'], '/main/path.po') assert_equal(details[0]['translated_count'], 0) assert_equal(details[1]['resource__path'], '/other/path.po') assert_equal(details[1]['translated_count'], 0) assert_equal(len(details_other), 1) assert_equal(details_other[0]['resource__path'], '/other/path.po') assert_equal(details_other[0]['translated_count'], 0)
def test_save_latest_translation_missing_project_locale(self): """ If a translation is saved for a locale that isn't active on the project, do not fail due to a missing ProjectLocale. """ locale = LocaleFactory.create(latest_translation=None) project = ProjectFactory.create(latest_translation=None) resource = ResourceFactory.create(project=project) stats = StatsFactory.create(locale=locale, resource=resource, latest_translation=None) # This calls .save, this should fail if we're not properly # handling the missing ProjectLocale. 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)
def test_save_latest_translation_missing_project_locale(self): """ If a translation is saved for a locale that isn't active on the project, do not fail due to a missing ProjectLocale. """ locale = LocaleFactory.create(latest_translation=None) project = ProjectFactory.create(latest_translation=None) resource = ResourceFactory.create(project=project) stats = StatsFactory.create(locale=locale, resource=resource, latest_translation=None) # This calls .save, this should fail if we're not properly # handling the missing ProjectLocale. 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)