Esempio n. 1
0
def test_mgr_user_translation_counts(
    resource_a,
    locale_a,
):
    """Checks if translation counts are calculated properly.

    Tests creates 3 contributors with different numbers translations
    and checks if their counts match.

    """
    contributors = UserFactory.create_batch(size=3)
    entities = EntityFactory.create_batch(size=36, resource=resource_a)
    batch_kwargs = sum(
        [
            [dict(user=contributors[0], approved=True)] * 7,
            [dict(user=contributors[0], approved=False, fuzzy=False)] * 3,
            [dict(user=contributors[0], fuzzy=True)] * 2,
            [dict(user=contributors[1], approved=True)] * 5,
            [dict(user=contributors[1], approved=False, fuzzy=False)] * 9,
            [dict(user=contributors[1], fuzzy=True)] * 2,
            [dict(user=contributors[2], approved=True)] * 1,
            [dict(user=contributors[2], approved=False, fuzzy=False)] * 2,
            [dict(user=contributors[2], fuzzy=True)] * 5,
        ],
        []
    )

    for i, kwa in enumerate(batch_kwargs):
        kwa.update(dict(entity=entities[i]))

    for args in batch_kwargs:
        TranslationFactory.create(
            locale=locale_a,
            user=args['user'],
            approved=args.get('approved', False),
            fuzzy=args.get('fuzzy', False),
        )

    top_contribs = get_user_model().translators.with_translation_counts()

    assert len(top_contribs) == 3
    assert top_contribs[0] == contributors[1]
    assert top_contribs[1] == contributors[0]
    assert top_contribs[2] == contributors[2]

    assert top_contribs[0].translations_count == 16
    assert top_contribs[0].translations_approved_count == 5
    assert top_contribs[0].translations_unapproved_count == 9
    assert top_contribs[0].translations_needs_work_count == 2

    assert top_contribs[1].translations_count == 12
    assert top_contribs[1].translations_approved_count == 7
    assert top_contribs[1].translations_unapproved_count == 3
    assert top_contribs[1].translations_needs_work_count == 2

    assert top_contribs[2].translations_count == 8
    assert top_contribs[2].translations_approved_count == 1
    assert top_contribs[2].translations_unapproved_count == 2
    assert top_contribs[2].translations_needs_work_count == 5
Esempio n. 2
0
def test_mgr_entity_filter_errors_plural(resource_a, locale_a):
    locale_a.cldr_plurals = '1,5'
    locale_a.save()
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
            string_plural="testpluralentity%s" % i,
        ) for i in range(0, 3)
    ]

    translation00 = TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=0,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=1,
        approved=True,
    )
    translation20 = TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        plural_form=0,
        approved=True,
    )
    translation21 = TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        plural_form=1,
        approved=True,
    )

    ErrorFactory.create(
        translation=translation00
    )
    ErrorFactory.create(
        translation=translation20
    )
    ErrorFactory.create(
        translation=translation21
    )

    assert (
        set(Entity.objects.filter(
            Entity.objects.errors(locale_a)
        )) == {entities[2]}
    )
Esempio n. 3
0
def test_mgr_entity_filter_unreviewed(resource_a, locale_a):
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
        ) for i in range(0, 3)
    ]
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        fuzzy=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        string='translation for 1',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        string='translation for 2',
    )
    assert (
        set(Entity.objects.filter(
            Entity.objects.unreviewed(locale_a)
        )) == {entities[1], entities[2]}
    )
Esempio n. 4
0
def test_mgr_entity_filter_warnings(resource_a, locale_a):
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i
        ) for i in range(0, 3)
    ]

    translations = [
        TranslationFactory.create(
            locale=locale_a,
            entity=entities[i],
            fuzzy=True,
        ) for i in range(0, 3)
    ]

    WarningFactory.create(
        translation=translations[1]
    )
    WarningFactory.create(
        translation=translations[2]
    )
    TranslatedResource.objects.get(
        resource=translations[2].entity.resource,
        locale=translations[2].locale,
    ).calculate_stats()

    translations[2].fuzzy = False
    translations[2].save()

    assert (
        set(Entity.objects.filter(
            Entity.objects.warnings(locale_a)
        )) == {entities[1]}
    )
Esempio n. 5
0
def test_mgr_entity_filter_warnings(resource_a, locale_a):
    entities = [
        EntityFactory.create(resource=resource_a, string="testentity%s" % i)
        for i in range(0, 3)
    ]

    translations = [
        TranslationFactory.create(
            locale=locale_a,
            entity=entities[i],
            fuzzy=True,
        ) for i in range(0, 3)
    ]

    WarningFactory.create(translation=translations[1])
    WarningFactory.create(translation=translations[2])
    TranslatedResource.objects.get(
        resource=translations[2].entity.resource,
        locale=translations[2].locale,
    ).calculate_stats()

    translations[2].fuzzy = False
    translations[2].save()

    assert (set(Entity.objects.filter(
        Entity.objects.warnings(locale_a))) == {entities[1]})
Esempio n. 6
0
def test_view_caighdean(client, entity_a):
    gd = Locale.objects.get(code='gd')
    url = reverse('pontoon.caighdean')

    response = client.get(url, dict(id=entity_a.id))
    assert json.loads(response.content) == {}

    translation = TranslationFactory.create(
        entity=entity_a, locale=gd, string='GD translation'
    )
    entity_a.translation_set.add(translation)

    translator = caighdean.Translator()

    with requests_mock.mock() as m:
        m.post(translator.service_url, text='[["source", "target"]]')
        response = client.get(url, dict(id=entity_a.id))

    assert (
        json.loads(response.content)
        == {
            "translation": "target",
            "original": translation.string,
        }
    )
    assert (
        urlparse.parse_qs(m.request_history[0].text)
        == {
            u'teacs': [translation.string],
            u'foinse': [gd.code],
        }
    )
def test_mgr_entity_filter_errors(resource_a, locale_a):
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i
        ) for i in range(0, 3)
    ]

    translations = [
        TranslationFactory.create(
            locale=locale_a,
            entity=entities[i],
            approved=True,
        ) for i in range(0, 3)
    ]

    ErrorFactory.create(
        translation=translations[0]
    )
    ErrorFactory.create(
        translation=translations[2]
    )

    assert (
        set(Entity.objects.filter(
            Entity.objects.errors(locale_a)
        )) == {entities[0], entities[2]}
    )
Esempio n. 8
0
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
Esempio n. 9
0
def test_translation_save_latest_update_first_translation(
        locale_a, project_a, project_locale_a, resource_a, entity_a):
    """
    When a translation is saved, update the latest_translation
    attribute on the related project, locale, translatedresource,
    and project_locale objects.
    """
    tr = TranslatedResourceFactory.create(locale=locale_a, resource=resource_a)

    assert locale_a.latest_translation is None
    assert project_a.latest_translation is None
    assert tr.latest_translation is None
    assert project_locale_a.latest_translation is None

    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
Esempio n. 10
0
def test_view_caighdean(client, entity_a):
    gd = Locale.objects.get(code='gd')
    url = reverse('pontoon.caighdean')

    response = client.get(url, dict(id=entity_a.id))
    assert json.loads(response.content) == {}

    translation = TranslationFactory.create(
        entity=entity_a,
        locale=gd,
        string='GD translation',
        plural_form=None,
        approved=True,
    )
    entity_a.translation_set.add(translation)

    translator = caighdean.Translator()

    with requests_mock.mock() as m:
        m.post(translator.service_url, text='[["source", "target"]]')
        response = client.get(url, dict(id=entity_a.id))

    assert (json.loads(response.content) == {
        "translation": "target",
        "original": translation.string,
    })
    assert (urllib.parse.parse_qs(m.request_history[0].text) == {
        u'teacs': [translation.string],
        u'foinse': [gd.code],
    })
Esempio n. 11
0
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
Esempio n. 12
0
def test_view_caighdean(client, entity_a):
    gd = Locale.objects.get(code="gd")
    url = reverse("pontoon.caighdean")

    response = client.get(url, dict(id=entity_a.id))
    assert json.loads(response.content) == {}

    translation = TranslationFactory.create(
        entity=entity_a,
        locale=gd,
        string="GD translation",
        plural_form=None,
        approved=True,
    )
    entity_a.translation_set.add(translation)

    with requests_mock.mock() as m:
        m.post("https://cadhan.com/api/intergaelic/3.0",
               text='[["source", "target"]]')
        response = client.get(url, dict(id=entity_a.id))

    assert json.loads(response.content) == {
        "translation": "target",
        "original": translation.string,
    }
    assert urllib.parse.parse_qs(m.request_history[0].text) == {
        "teacs": [translation.string],
        "foinse": [gd.code],
    }
Esempio n. 13
0
def test_mgr_entity_filter_rejected(resource_a, locale_a):
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
        ) for i in range(0, 3)
    ]

    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        approved=False,
        fuzzy=False,
        rejected=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        approved=True,
        fuzzy=False,
        rejected=False,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        approved=True,
        fuzzy=False,
        rejected=False,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        approved=False,
        fuzzy=False,
        rejected=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        approved=False,
        fuzzy=False,
        rejected=False,
    )
    assert set(Entity.objects.filter(Entity.objects.rejected(locale_a))) == {
        entities[0],
        entities[1],
    }
def test_mgr_entity_filter_fuzzy_plurals(resource_a, locale_a):
    locale_a.cldr_plurals = '1,5'
    locale_a.save()
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
            string_plural="testpluralentity%s" % i,
        ) for i in range(0, 3)
    ]

    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=0,
        fuzzy=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=1,
        fuzzy=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        plural_form=0,
        fuzzy=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        plural_form=0,
        fuzzy=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        plural_form=1,
        fuzzy=True,
    )
    assert (
        set(Entity.objects.filter(
            Entity.objects.fuzzy(locale_a)
        )) == {entities[0], entities[2]}
    )
Esempio n. 15
0
def test_mgr_entity_filter_warnings_plural(resource_a, locale_a):
    locale_a.cldr_plurals = "1,5"
    locale_a.save()
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
            string_plural="testpluralentity%s" % i,
        )
        for i in range(0, 3)
    ]

    TranslationFactory.create(
        locale=locale_a, entity=entities[0], plural_form=0,
    )
    TranslationFactory.create(
        locale=locale_a, entity=entities[0], plural_form=1, fuzzy=True,
    )
    translation20 = TranslationFactory.create(
        locale=locale_a, entity=entities[2], plural_form=0, fuzzy=True,
    )
    TranslationFactory.create(
        locale=locale_a, entity=entities[2], plural_form=1, fuzzy=True,
    )

    WarningFactory.create(translation=translation20)

    assert set(Entity.objects.filter(Entity.objects.warnings(locale_a))) == {
        entities[2]
    }
Esempio n. 16
0
def test_mgr_entity_filter_translated_plurals(resource_a, locale_a):
    locale_a.cldr_plurals = "1,5"
    locale_a.save()
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
            string_plural="testpluralentity%s" % i,
        ) for i in range(0, 3)
    ]
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=0,
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=1,
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        plural_form=0,
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        plural_form=0,
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        plural_form=1,
        approved=True,
    )
    assert set(
        Entity.objects.filter(
            Entity.objects.translated(
                locale_a, resource_a.project))) == {entities[0], entities[2]}
Esempio n. 17
0
def test_view_caighdean_bad(client, entity_a):
    gd = Locale.objects.get(code='gd')
    url = reverse('pontoon.caighdean')

    response = client.get(url)
    assert response.status_code == 400
    assert response.get("Content-Type") == 'application/json'
    assert (
        json.loads(response.content)["message"]
        == 'Bad Request: "\'id\'"'
    )

    response = client.get(url, dict(id="DOESNOTEXIST"))
    assert response.status_code == 400
    assert response.get("Content-Type") == 'application/json'
    assert (
        json.loads(response.content)["message"]
        == ("Bad Request: invalid literal for int() "
            "with base 10: 'DOESNOTEXIST'")
    )

    maxid = (
        Entity.objects
        .values_list("id", flat=True)
        .order_by("-id")
        .first()
    )
    response = client.get(url, dict(id=maxid + 1))
    assert response.status_code == 404
    assert response.get("Content-Type") == 'application/json'
    assert (
        json.loads(response.content)["message"]
        == 'Not Found: Entity matching query does not exist.'
    )

    translator = caighdean.Translator()
    translation = TranslationFactory.create(
        entity=entity_a,
        locale=gd,
        string='foo',
        plural_form=None,
        approved=True,
    )
    entity_a.translation_set.add(translation)

    with requests_mock.mock() as m:
        m.post(translator.service_url, status_code=403)
        response = client.get(url, dict(id=entity_a.id))

    assert response.status_code == 500
    assert response.get("Content-Type") == 'application/json'
    assert (
        json.loads(response.content)["message"]
        == 'Server Error: Unable to connect to translation service'
    )
Esempio n. 18
0
def test_view_caighdean_bad(client, entity_a):
    gd = Locale.objects.get(code='gd')
    url = reverse('pontoon.caighdean')

    response = client.get(url)
    assert response.status_code == 400
    assert response.get("Content-Type") == 'application/json'
    assert (
        json.loads(response.content)["message"]
        == 'Bad Request: "\'id\'"'
    )

    response = client.get(url, dict(id="DOESNOTEXIST"))
    assert response.status_code == 400
    assert response.get("Content-Type") == 'application/json'
    assert (
        json.loads(response.content)["message"]
        == ("Bad Request: invalid literal for int() "
            "with base 10: 'DOESNOTEXIST'")
    )

    maxid = (
        Entity.objects
        .values_list("id", flat=True)
        .order_by("-id")
        .first()
    )
    response = client.get(url, dict(id=maxid + 1))
    assert response.status_code == 404
    assert response.get("Content-Type") == 'application/json'
    assert (
        json.loads(response.content)["message"]
        == 'Not Found: Entity matching query does not exist.'
    )

    translator = caighdean.Translator()
    translation = TranslationFactory.create(
        entity=entity_a,
        locale=gd,
        string='foo',
        plural_form=None,
        approved=True,
    )
    entity_a.translation_set.add(translation)

    with requests_mock.mock() as m:
        m.post(translator.service_url, status_code=403)
        response = client.get(url, dict(id=entity_a.id))

    assert response.status_code == 500
    assert response.get("Content-Type") == 'application/json'
    assert (
        json.loads(response.content)["message"]
        == 'Server Error: Unable to connect to translation service'
    )
Esempio n. 19
0
def test_mgr_user_contributors_order(
    resource_b, locale_a,
):
    """
    Checks if users are ordered by count of contributions.
    """
    contributors = UserFactory.create_batch(size=5)
    entities = EntityFactory.create_batch(size=22, resource=resource_b)

    # create a list of contributors/entity for translations
    for i, count in enumerate([2, 4, 9, 1, 6]):
        for j in range(count):
            TranslationFactory.create(
                locale=locale_a, user=contributors[i], entity=entities[i],
            )

    # Ordered by approved count
    assert list(users_with_translations_counts()) == [
        contributors[i] for i in [2, 4, 1, 0, 3]
    ]
Esempio n. 20
0
def test_translation_unapproved_not_in_tm(locale_a, entity_a):
    """
    Unapproved translation shouldn't be in the translation memory.
    """
    translation = TranslationFactory.create(locale=locale_a, entity=entity_a,)
    with pytest.raises(TranslationMemoryEntry.DoesNotExist):
        TranslationMemoryEntry.objects.get(
            source=translation.entity.string,
            target=translation.string,
            locale=translation.locale,
        )
Esempio n. 21
0
def test_machinery_sources_values(locale_a, entity_a):
    """
    Return comma-separated machinery_sources values for the given translation.
    """
    translation_no_machinery_sources = TranslationFactory.create(
        locale=locale_a,
        entity=entity_a,
    )
    assert translation_no_machinery_sources.machinery_sources_values == ""

    translation = TranslationFactory.create(
        locale=locale_a,
        entity=entity_a,
        machinery_sources=[
            Translation.MachinerySource.TRANSLATION_MEMORY,
            Translation.MachinerySource.GOOGLE_TRANSLATE,
        ],
    )
    assert (translation.machinery_sources_values ==
            "Translation Memory, Google Translate")
Esempio n. 22
0
def test_mgr_entity_filter_errors_plural(resource_a, locale_a):
    locale_a.cldr_plurals = '1,5'
    locale_a.save()
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
            string_plural="testpluralentity%s" % i,
        ) for i in range(0, 3)
    ]

    translation00 = TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=0,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=1,
        approved=True,
    )
    translation20 = TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        plural_form=0,
        approved=True,
    )
    translation21 = TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        plural_form=1,
        approved=True,
    )

    ErrorFactory.create(translation=translation00)
    ErrorFactory.create(translation=translation20)
    ErrorFactory.create(translation=translation21)

    assert (set(Entity.objects.filter(
        Entity.objects.errors(locale_a))) == {entities[2]})
Esempio n. 23
0
def test_mgr_user_contributors_limit(
    resource_a,
    locale_a,
):
    """
    Checks if proper count of user is returned.
    """
    contributors = UserFactory.create_batch(size=102)
    entities = EntityFactory.create_batch(
        size=102,
        resource=resource_a,
    )
    for i, contrib in enumerate(contributors):
        TranslationFactory.create(
            locale=locale_a,
            approved=True,
            user=contrib,
            entity=entities[i],
        )
    top_contributors = users_with_translations_counts()
    assert len(top_contributors) == 100
Esempio n. 24
0
def test_mgr_entity_filter_unchanged(resource_a, locale_a):
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string='Unchanged string',
        ) for i in range(0, 3)
    ]
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        approved=True,
        string='Unchanged string',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        fuzzy=True,
        string='Unchanged string',
    )
    assert (set(Entity.objects.filter(
        Entity.objects.unchanged(locale_a))) == {entities[0], entities[2]})
Esempio n. 25
0
def test_mgr_user_contributors_limit(
    resource_a,
    locale_a,
):
    """
    Checks if proper count of user is returned.
    """
    contributors = UserFactory.create_batch(size=102)
    entities = EntityFactory.create_batch(
        size=102,
        resource=resource_a,
    )
    for i, contrib in enumerate(contributors):
        TranslationFactory.create(
            locale=locale_a,
            approved=True,
            user=contrib,
            entity=entities[i],
        )
    top_contributors = get_user_model().translators.with_translation_counts()
    assert len(top_contributors) == 100
def test_mgr_entity_filter_missing(resource_a, locale_a):
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
        ) for i in range(0, 3)
    ]

    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        fuzzy=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
    )
    assert (
        set(resource_a.entities.filter(
            Entity.objects.missing(locale_a)
        )) == {entities[1]}
    )
Esempio n. 27
0
def test_mgr_entity_filter_partially_translated_plurals(resource_a, locale_a):
    locale_a.cldr_plurals = '1,5'
    locale_a.save()
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string='Unchanged string',
            string_plural='Unchanged plural string',
        ) for i in range(0, 3)
    ]

    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=0,
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=1,
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        plural_form=0,
        approved=True,
    )
    assert (set(resource_a.entities.filter(
        Entity.objects.missing(locale_a))) == {entities[1], entities[2]})
Esempio n. 28
0
def test_mgr_entity_filter_combined(resource_a, locale_a, user_a):
    """
    All filters should be joined by AND instead of OR.
    Tests filters against bug introduced by bug 1243115.
    """
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
        ) for i in range(0, 2)
    ]

    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        approved=True,
        user=user_a,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        fuzzy=True,
        user=user_a,
    )
    assert (
        list(Entity.for_project_locale(
            resource_a.project,
            locale_a,
            status='unreviewed',
            author=user_a.email,
        )) == []
    )
    assert (
        list(Entity.for_project_locale(
            resource_a.project,
            locale_a,
            status='unreviewed',
            time='201001010100-205001010100',
        )) == []
    )
Esempio n. 29
0
def test_mgr_entity_filter_combined(admin, resource_a, locale_a, user_a):
    """
    All filters should be joined by AND instead of OR.
    Tests filters against bug introduced by bug 1243115.
    """
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
        ) for i in range(0, 2)
    ]

    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        approved=True,
        user=user_a,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        fuzzy=True,
        user=user_a,
    )
    assert (list(
        Entity.for_project_locale(
            admin,
            resource_a.project,
            locale_a,
            status="unreviewed",
            author=user_a.email,
        )) == [])
    assert (list(
        Entity.for_project_locale(
            admin,
            resource_a.project,
            locale_a,
            status="unreviewed",
            time="201001010100-205001010100",
        )) == [])
Esempio n. 30
0
def test_mgr_entity_filter_unchanged(resource_a, locale_a):
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string='Unchanged string',
        ) for i in range(0, 3)
    ]
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        active=True,
        approved=True,
        string='Unchanged string',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        active=True,
        fuzzy=True,
        string='Unchanged string',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        active=False,
        approved=True,
        string='Unchanged string',
    )
    assert (
        set(Entity.objects.filter(
            Entity.objects.unchanged(locale_a)
        )) == {entities[0], entities[2]}
    )
Esempio n. 31
0
def test_mgr_entity_filter_partially_translated_plurals(resource_a, locale_a):
    locale_a.cldr_plurals = '1,5'
    locale_a.save()
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string='Unchanged string',
            string_plural='Unchanged plural string',
        ) for i in range(0, 3)
    ]

    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=0,
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        plural_form=1,
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        plural_form=0,
        approved=True,
    )
    assert (
        set(resource_a.entities.filter(
            Entity.objects.missing(locale_a)
        )) == {entities[1], entities[2]}
    )
Esempio n. 32
0
def test_translation_approved_in_tm(locale_a, entity_a):
    """
    Every save of approved translation should generate a new
    entry in the translation memory.
    """
    translation = TranslationFactory.create(
        locale=locale_a, entity=entity_a, approved=True,
    )
    assert TranslationMemoryEntry.objects.get(
        source=translation.entity.string,
        target=translation.string,
        locale=translation.locale,
    )
Esempio n. 33
0
def test_translation_rejected_not_in_tm(locale_a, entity_a):
    """
    When translation is deleted, its corresponding TranslationMemoryEntry
    needs to be deleted, too.
    """
    translation = TranslationFactory.create(
        locale=locale_a, entity=entity_a, rejected=True,
    )
    with pytest.raises(TranslationMemoryEntry.DoesNotExist):
        TranslationMemoryEntry.objects.get(
            source=translation.entity.string,
            target=translation.string,
            locale=translation.locale,
        )
Esempio n. 34
0
def test_reset_term_translation(locale_a):
    """
    Test if TermTranslation gets properly updated when translation
    in the "Terminology" project changes.
    """
    project, _ = Project.objects.get_or_create(slug="terminology")
    entity = EntityFactory.create(resource=project.resources.first())

    term = TermFactory.create()
    entity.term = term

    # No approved Translations of an Entity: no TermTranslation
    TranslationFactory.create(locale=locale_a, entity=entity)
    entity.reset_term_translation(locale_a)
    assert entity.term.translations.filter(locale=locale_a).count() == 0

    # First approved Translation of an Entity added: create TermTranslation to match the Translation
    translation_approved = TranslationFactory.create(
        locale=locale_a, entity=entity, approved=True
    )
    entity.reset_term_translation(locale_a)
    assert entity.term.translations.filter(locale=locale_a).count() == 1
    assert (
        entity.term.translations.get(locale=locale_a).text
        == translation_approved.string
    )

    # Another approved Translation of an Entity added: update TermTranslation to match the Translation
    translation_approved_2 = TranslationFactory.create(
        locale=locale_a, entity=entity, approved=True
    )
    entity.reset_term_translation(locale_a)
    assert entity.term.translations.filter(locale=locale_a).count() == 1
    assert (
        entity.term.translations.get(locale=locale_a).text
        == translation_approved_2.string
    )
Esempio n. 35
0
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
Esempio n. 36
0
def test_mgr_user_contributors_order(
    resource_b,
    locale_a,
):
    """
    Checks if users are ordered by count of contributions.
    """
    contributors = UserFactory.create_batch(size=5)
    entities = EntityFactory.create_batch(size=22, resource=resource_b)

    # create a list of contributors/entity for translations
    for i, count in enumerate([2, 4, 9, 1, 6]):
        for j in range(count):
            TranslationFactory.create(
                locale=locale_a,
                user=contributors[i],
                entity=entities[i],
            )

    # Ordered by approved count
    assert (
        list(get_user_model().translators.with_translation_counts())
        == [contributors[i] for i in [2, 4, 1, 0, 3]]
    )
Esempio n. 37
0
def translation_dtd_unapproved():
    translation = TranslationFactory.create(
        string='Test Translation',
        approved=False,
        entity__key='test',
        entity__resource__format='dtd',
        entity__resource__path='test.dtd',
    )
    bulk_run_checks([translation])

    ProjectLocaleFactory.create(
        project=translation.entity.resource.project,
        locale=translation.locale,
    )

    yield translation
def test_mgr_entity_filter_unreviewed_plural(resource_a, locale_a):
    locale_a.cldr_plurals = '1,5'
    locale_a.save()
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
            string_plural="testpluralentity%s" % i,
        ) for i in range(0, 3)
    ]

    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        approved=False,
        fuzzy=False,
        plural_form=0,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        approved=False,
        fuzzy=False,
        plural_form=1,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        approved=False,
        fuzzy=False,
        plural_form=0,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        approved=False,
        fuzzy=False,
        plural_form=1,
    )
    assert (
        set(Entity.objects.filter(
            Entity.objects.unreviewed(locale_a)
        )) == {entities[0], entities[2]}
    )
Esempio n. 39
0
def translation_dtd_invalid_unapproved():
    # Provide invalid characters in translation to cause checks to fail
    translation = TranslationFactory.create(
        string='!@#$""\'',
        approved=False,
        entity__key='test',
        entity__resource__format='dtd',
        entity__resource__path='test.dtd',
    )
    bulk_run_checks([translation])

    ProjectLocaleFactory.create(
        project=translation.entity.resource.project,
        locale=translation.locale,
    )

    yield translation
Esempio n. 40
0
def translation_dtd_unapproved():
    translation = TranslationFactory.create(
        string="Test Translation",
        active=True,
        approved=False,
        entity__key="test",
        entity__resource__format="dtd",
        entity__resource__path="test.dtd",
    )
    bulk_run_checks([translation])

    ProjectLocaleFactory.create(
        project=translation.entity.resource.project,
        locale=translation.locale,
    )

    yield translation
Esempio n. 41
0
def test_mgr_entity_filter_unchanged(resource_a, locale_a):
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="Unchanged string",
        ) for i in range(0, 4)
    ]
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        active=True,
        approved=True,
        string="Unchanged string",
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        active=False,
        approved=True,
        string="Unchanged string",
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        active=True,
        fuzzy=True,
        string="Unchanged string",
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[3],
        active=True,
        pretranslated=True,
        string="Unchanged string",
    )
    assert set(Entity.objects.filter(Entity.objects.unchanged(locale_a))) == {
        entities[0],
        entities[2],
        entities[3],
    }
Esempio n. 42
0
def test_view_caighdean_bad(client, entity_a):
    gd = Locale.objects.get(code="gd")
    url = reverse("pontoon.caighdean")

    response = client.get(url)
    assert response.status_code == 400
    assert response.get("Content-Type") == "application/json"
    assert json.loads(response.content)["message"] == "Bad Request: 'id'"

    response = client.get(url, dict(id="DOESNOTEXIST"))
    assert response.status_code == 400
    assert response.get("Content-Type") == "application/json"
    assert json.loads(response.content)["message"] == (
        "Bad Request: invalid literal for int() "
        "with base 10: 'DOESNOTEXIST'")

    maxid = Entity.objects.values_list("id", flat=True).order_by("-id").first()
    response = client.get(url, dict(id=maxid + 1))
    assert response.status_code == 400
    assert response.get("Content-Type") == "application/json"
    assert (json.loads(response.content)["message"] ==
            "Bad Request: Entity matching query does not exist.")

    translation = TranslationFactory.create(
        entity=entity_a,
        locale=gd,
        string="foo",
        plural_form=None,
        approved=True,
    )
    entity_a.translation_set.add(translation)

    with requests_mock.mock() as m:
        m.post("https://cadhan.com/api/intergaelic/3.0", status_code=500)
        response = client.get(url, dict(id=entity_a.id))

    assert response.status_code == 500
    assert response.get("Content-Type") == "application/json"
    assert (
        json.loads(response.content)["message"] ==
        "500 Server Error: None for url: https://cadhan.com/api/intergaelic/3.0"
    )
Esempio n. 43
0
def test_translation_unapproved_not_in_tm(locale_a, project_a):
    """
    Unapproved translation shouldn't be in the translation memory.
    """
    resource = ResourceFactory.create(
        project=project_a,
        path="resource.po",
        format="po",
    )
    entity = EntityFactory.create(resource=resource, string="Entity X")
    translation = TranslationFactory.create(
        locale=locale_a,
        entity=entity,
    )
    with pytest.raises(TranslationMemoryEntry.DoesNotExist):
        TranslationMemoryEntry.objects.get(
            source=translation.entity.string,
            target=translation.string,
            locale=translation.locale,
        )
Esempio n. 44
0
def test_translation_approved_in_tm(locale_a, project_a):
    """
    Every save of approved translation should generate a new
    entry in the translation memory.
    """
    resource = ResourceFactory.create(
        project=project_a,
        path="resource.po",
        format="po",
    )
    entity = EntityFactory.create(resource=resource, string="Entity X")
    translation = TranslationFactory.create(
        locale=locale_a,
        entity=entity,
        approved=True,
    )
    assert TranslationMemoryEntry.objects.get(
        source=translation.entity.string,
        target=translation.string,
        locale=translation.locale,
    )
Esempio n. 45
0
def test_translation_rejected_not_in_tm(locale_a, project_a):
    """
    When translation is deleted, its corresponding TranslationMemoryEntry
    needs to be deleted, too.
    """
    resource = ResourceFactory.create(
        project=project_a,
        path="resource.po",
        format="po",
    )
    entity = EntityFactory.create(resource=resource, string="Entity X")
    translation = TranslationFactory.create(
        locale=locale_a,
        entity=entity,
        rejected=True,
    )
    with pytest.raises(TranslationMemoryEntry.DoesNotExist):
        TranslationMemoryEntry.objects.get(
            source=translation.entity.string,
            target=translation.string,
            locale=translation.locale,
        )
Esempio n. 46
0
def test_mgr_entity_filter_unchanged_plural(resource_a, locale_a):
    locale_a.cldr_plurals = '1,5'
    locale_a.save()
    entities = [
        EntityFactory.create(
            resource=resource_a,
            string='Unchanged string',
            string_plural='Unchanged plural string',
        ) for i in range(0, 3)
    ]

    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        active=True,
        approved=True,
        plural_form=0,
        string='Unchanged string',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[0],
        active=True,
        approved=True,
        plural_form=1,
        string='Unchanged plural string',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        active=False,
        fuzzy=True,
        plural_form=0,
        string='Unchanged string',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        active=False,
        fuzzy=True,
        plural_form=1,
        string='Unchanged plural string',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        active=True,
        fuzzy=True,
        plural_form=0,
        string='Unchanged string',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        active=True,
        fuzzy=True,
        plural_form=1,
        string='Unchanged plural string',
    )
    assert (
        set(Entity.objects.filter(
            Entity.objects.unchanged(locale_a)
        )) == {entities[0], entities[2]}
    )
Esempio n. 47
0
def test_translation_save_latest_update(locale_a, project_a):
    """
    When a translation is saved, update the latest_translation
    attribute on the related project, locale, translatedresource,
    and project_locale objects.
    """
    project_locale = ProjectLocaleFactory.create(
        project=project_a, locale=locale_a,
    )
    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")

    assert locale_a.latest_translation is None
    assert 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),
    )
    locale_a.refresh_from_db()
    project_a.refresh_from_db()
    tr.refresh_from_db()
    project_locale.refresh_from_db()
    assert locale_a.latest_translation == translation
    assert project_a.latest_translation == translation
    assert tr.latest_translation == translation
    assert project_locale.latest_translation == translation

    # Ensure translation is replaced for newer translations
    newer_translation = TranslationFactory.create(
        locale=locale_a,
        entity=entity,
        date=aware_datetime(1970, 2, 1),
    )
    locale_a.refresh_from_db()
    project_a.refresh_from_db()
    tr.refresh_from_db()
    project_locale.refresh_from_db()
    assert locale_a.latest_translation == newer_translation
    assert project_a.latest_translation == newer_translation
    assert tr.latest_translation == newer_translation
    assert project_locale.latest_translation == newer_translation

    # Ensure translation isn't replaced for older translations.
    TranslationFactory.create(
        locale=locale_a,
        entity=entity,
        date=aware_datetime(1970, 1, 5),
    )
    locale_a.refresh_from_db()
    project_a.refresh_from_db()
    tr.refresh_from_db()
    project_locale.refresh_from_db()
    assert locale_a.latest_translation == newer_translation
    assert project_a.latest_translation == newer_translation
    assert tr.latest_translation == newer_translation
    assert project_locale.latest_translation == newer_translation

    # Ensure approved_date is taken into consideration as well.
    newer_approved_translation = TranslationFactory.create(
        locale=locale_a,
        entity=entity,
        approved_date=aware_datetime(1970, 3, 1),
    )
    locale_a.refresh_from_db()
    project_a.refresh_from_db()
    tr.refresh_from_db()
    project_locale.refresh_from_db()
    assert locale_a.latest_translation == newer_approved_translation
    assert project_a.latest_translation == newer_approved_translation
    assert tr.latest_translation == newer_approved_translation
    assert project_locale.latest_translation == newer_approved_translation
Esempio n. 48
0
def test_manage_project_strings_download_csv(client_superuser):
    locale_kl = LocaleFactory.create(code='kl', name='Klingon')
    locale_gs = LocaleFactory.create(code='gs', name='Geonosian')
    project = ProjectFactory.create(
        data_source='database',
        locales=[locale_kl, locale_gs],
        repositories=[]
    )

    url = reverse('pontoon.admin.project.strings', args=(project.slug,))

    new_strings = """
        And on the pedestal these words appear:
        'My name is Ozymandias, king of kings:
        Look on my works, ye Mighty, and despair!'
    """
    response = client_superuser.post(url, {'new_strings': new_strings})
    assert response.status_code == 200

    # Test downloading the data.
    response = client_superuser.get(url, {'format': 'csv'})
    assert response.status_code == 200
    assert response._headers['content-type'] == ('Content-Type', 'text/csv')

    # Verify the original content is here.
    assert 'pedestal' in response.content
    assert 'Ozymandias' in response.content
    assert 'Mighty' in response.content

    # Verify we have the locale columns.
    assert 'kl' in response.content
    assert 'gs' in response.content

    # Now add some translations.
    entity = Entity.objects.filter(string='And on the pedestal these words appear:')[0]
    TranslationFactory.create(
        string='Et sur le piédestal il y a ces mots :',
        entity=entity,
        locale=locale_kl,
        approved=True,
    )
    TranslationFactory.create(
        string='Und auf dem Sockel steht die Schrift: ‚Mein Name',
        entity=entity,
        locale=locale_gs,
        approved=True,
    )

    entity = Entity.objects.filter(string='\'My name is Ozymandias, king of kings:')[0]
    TranslationFactory.create(
        string='"Mon nom est Ozymandias, Roi des Rois.',
        entity=entity,
        locale=locale_kl,
        approved=True,
    )
    TranslationFactory.create(
        string='Ist Osymandias, aller Kön’ge König: –',
        entity=entity,
        locale=locale_gs,
        approved=True,
    )

    entity = Entity.objects.filter(string='Look on my works, ye Mighty, and despair!\'')[0]
    TranslationFactory.create(
        string='Voyez mon œuvre, vous puissants, et désespérez !"',
        entity=entity,
        locale=locale_kl,
        approved=True,
    )
    TranslationFactory.create(
        string='Seht meine Werke, Mächt’ge, und erbebt!‘',
        entity=entity,
        locale=locale_gs,
        approved=True,
    )

    response = client_superuser.get(url, {'format': 'csv'})

    # Verify the translated content is here.
    assert 'pedestal' in response.content
    assert 'piédestal' in response.content
    assert 'Sockel' in response.content

    assert 'Mighty' in response.content
    assert 'puissants' in response.content
    assert 'Mächt’ge' in response.content
Esempio n. 49
0
def test_mgr_entity_reset_active_translations(resource_a, locale_a):
    locale_a.cldr_plurals = '1,5'
    locale_a.save()

    entities = [
        EntityFactory.create(
            resource=resource_a,
            string="testentity%s" % i,
        ) for i in range(0, 4)
    ] + [
        EntityFactory(
            resource=resource_a,
            string='testentity4',
            string_plural='testentity4plural',
        )
    ]
    entities_qs = Entity.objects.filter(pk__in=[e.pk for e in entities])

    # Translations for Entity 0:
    # No translations
    pass

    # Translations for Entity 1:
    # 2 unreviewed translations
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        string=entities[1].string + ' translation1',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[1],
        string=entities[1].string + ' translation2',
    )

    # Translations for Entity 2:
    # Approved and unreviewed translation
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        string=entities[2].string + ' translation1',
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[2],
        string=entities[2].string + ' translation2',
    )

    # Translations for Entity 3:
    # Fuzzy and unreviewed translation
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[3],
        string=entities[3].string + ' translation1',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[3],
        string=entities[3].string + ' translation2',
        fuzzy=True,
    )

    # Translations for Entity 4 - pluralized:
    # Approved and unreviewed translation for first form,
    # a single unreviewed translation for second form
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[4],
        plural_form=0,
        string=entities[4].string + ' translation1',
        approved=True,
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[4],
        plural_form=0,
        string=entities[4].string + ' translation2',
    )
    TranslationFactory.create(
        locale=locale_a,
        entity=entities[4],
        plural_form=1,
        string=entities[4].string_plural + ' translation1plural',
    )

    entities_qs.reset_active_translations(locale=locale_a)

    # Active translations for Entity 0:
    # no active translations
    assert entities[0].translation_set.filter(active=True).count() == 0

    # Active translations for Entity 1:
    # latest translation is active
    assert (
        entities[1].translation_set.get(active=True).string ==
        entities[1].string + ' translation2'
    )

    # Active translations for Entity 2:
    # approved translation is active
    assert (
        entities[2].translation_set.get(active=True).string ==
        entities[2].string + ' translation1'
    )

    # Active translations for Entity 3:
    # fuzzy translation is active
    assert (
        entities[3].translation_set.get(active=True).string ==
        entities[3].string + ' translation2'
    )

    # Active translations for Entity 4 - pluralized:
    # Approved translation for first form,
    # a single unreviewed translation for second form
    active = entities[4].translation_set.filter(active=True)
    assert active[0].string == entities[4].string + ' translation1'
    assert active[1].string == entities[4].string_plural + ' translation1plural'
Esempio n. 50
0
def entity_test_search(resource_a, locale_a):
    """This fixture provides:

    - 7 translated entities
    - A lambda for searching for entities using Entity.for_project_locale
    """
    TranslatedResourceFactory.create(
        locale=locale_a,
        resource=resource_a,
    )

    entity_args = [
        {
            'string': 'First entity string',
            'string_plural': 'First plural string',
            'comment': 'random notes',
        }, {
            'string': 'Second entity string',
            'string_plural': 'Second plural string',
            'comment': 'random',
        }, {
            'string': u'Third entity string with some twist: ZAŻÓŁĆ GĘŚLĄ',
            'string_plural': 'Third plural',
            'comment': 'even more random notes',
        }, {
            'string': 'Entity with first string',
            'string_plural': 'Entity with plural first string',
            'comment': 'random notes',
        }, {
            'string': 'First Entity',
            'string_plural': 'First plural entity',
            'comment': 'random notes',
        }, {
            'string': 'First Entity with string',
            'string_plural': 'First plural entity',
            'comment': 'random notes',
        }, {
            'string': 'Entity with quoted "string"',
            'string_plural': 'plural entity',
            'comment': 'random notes',
        },
    ]
    entities = [
        EntityFactory(
            resource=resource_a,
            string=x['string'],
            string_plural=x['string_plural'],
            comment=x['comment'],
            order=i,
        ) for i, x in enumerate(entity_args)
    ]

    translation_args = [
        {'string': 'First translation', 'entity': entities[0]},
        {'string': 'Second translation', 'entity': entities[1]},
        {'string': 'Third translation', 'entity': entities[2]},
        {'string': 'Fourth translation', 'entity': entities[3]},
        {'string': 'Fifth translation', 'entity': entities[4]},
        {'string': 'Sixth translation', 'entity': entities[5]},
        {'string': 'Seventh translation', 'entity': entities[6]},
    ]
    for x in translation_args:
        TranslationFactory.create(
            locale=locale_a,
            string=x['string'],
            entity=x['entity'],
        )

    return (
        entities,
        lambda q: list(
            Entity.for_project_locale(
                resource_a.project,
                locale_a,
                search=q,
            )
        )
    )
Esempio n. 51
0
def test_mgr_user_period_filters(
    locale_a,
    resource_a,
):
    """Total counts should be filtered by given date.

    Test creates 2 contributors with different activity periods and checks
    if they are filtered properly.
    """
    contributors = UserFactory.create_batch(size=2)
    entities = EntityFactory.create_batch(size=35, resource=resource_a)
    batch_kwargs = sum(
        [
            [dict(
                user=contributors[0],
                date=aware_datetime(2015, 3, 2),
                approved=True,
            )] * 12,
            [dict(
                user=contributors[0],
                date=aware_datetime(2015, 7, 2),
                approved=True,
            )] * 5,
            [dict(
                user=contributors[0],
                date=aware_datetime(2015, 3, 2),
                approved=False,
                fuzzy=False,
            )] * 1,
            [dict(
                user=contributors[0],
                date=aware_datetime(2015, 3, 2),
                fuzzy=True,
            )] * 2,
            [dict(
                user=contributors[1],
                date=aware_datetime(2015, 6, 1),
                approved=True,
            )] * 2,
            [dict(
                user=contributors[1],
                date=aware_datetime(2015, 6, 1),
                approved=False,
                fuzzy=False,
            )] * 11,
            [dict(
                user=contributors[1],
                date=aware_datetime(2015, 6, 1),
                fuzzy=True,
            )] * 2
        ],
        [],
    )

    for i, kwa in enumerate(batch_kwargs):
        kwa.update(dict(entity=entities[i]))

    for args in batch_kwargs:
        TranslationFactory.create(
            locale=locale_a,
            user=args['user'],
            date=args['date'],
            approved=args.get('approved', False),
            fuzzy=args.get('fuzzy', False),
        )

    top_contribs = get_user_model().translators.with_translation_counts(
        aware_datetime(2015, 6, 10)
    )
    assert len(top_contribs) == 1
    assert top_contribs[0].translations_count == 5
    assert top_contribs[0].translations_approved_count == 5
    assert top_contribs[0].translations_unapproved_count == 0
    assert top_contribs[0].translations_needs_work_count == 0

    top_contribs = get_user_model().translators.with_translation_counts(
        aware_datetime(2015, 5, 10)
    )
    assert len(top_contribs) == 2
    assert top_contribs[0].translations_count == 15
    assert top_contribs[0].translations_approved_count == 2
    assert top_contribs[0].translations_unapproved_count == 11
    assert top_contribs[0].translations_needs_work_count == 2
    assert top_contribs[1].translations_count == 5
    assert top_contribs[1].translations_approved_count == 5
    assert top_contribs[1].translations_unapproved_count == 0
    assert top_contribs[1].translations_needs_work_count == 0

    top_contribs = get_user_model().translators.with_translation_counts(
        aware_datetime(2015, 1, 10)
    )
    assert len(top_contribs) == 2
    assert top_contribs[0].translations_count == 20
    assert top_contribs[0].translations_approved_count == 17
    assert top_contribs[0].translations_unapproved_count == 1
    assert top_contribs[0].translations_needs_work_count == 2
    assert top_contribs[1].translations_count == 15
    assert top_contribs[1].translations_approved_count == 2
    assert top_contribs[1].translations_unapproved_count == 11
    assert top_contribs[1].translations_needs_work_count == 2
Esempio n. 52
0
def test_mgr_user_query_args_filtering(
    locale_a,
    resource_a,
    locale_b,
):
    """
    Tests if query args are honored properly and contributors are filtered.
    """
    contributors = UserFactory.create_batch(size=3)
    entities = EntityFactory.create_batch(size=53, resource=resource_a)

    batch_kwargs = sum(
        [
            [dict(
                user=contributors[0],
                locale=locale_a,
                approved=True
            )] * 12,
            [dict(
                user=contributors[0],
                locale=locale_a,
                approved=False,
                fuzzy=False
            )] * 1,
            [dict(
                user=contributors[0],
                locale=locale_a,
                fuzzy=True
            )] * 2,
            [dict(
                user=contributors[1],
                locale=locale_b,
                approved=True
            )] * 11,
            [dict(
                user=contributors[1],
                locale=locale_b,
                approved=False,
                fuzzy=False
            )] * 1,
            [dict(
                user=contributors[1],
                locale=locale_b,
                fuzzy=True
            )] * 2,
            [dict(
                user=contributors[2],
                locale=locale_a,
                approved=True
            )] * 10,
            [dict(
                user=contributors[2],
                locale=locale_a,
                approved=False,
                fuzzy=False
            )] * 12,
            [dict(
                user=contributors[2],
                locale=locale_a,
                fuzzy=True
            )] * 2
        ],
        [],
    )

    for i, kwa in enumerate(batch_kwargs):
        kwa.update(dict(entity=entities[i]))

    for args in batch_kwargs:
        TranslationFactory.create(
            locale=args['locale'],
            user=args['user'],
            approved=args.get('approved', False),
            fuzzy=args.get('fuzzy', False),
        )

    top_contribs = get_user_model().translators.with_translation_counts(
        aware_datetime(2015, 1, 1),
        Q(locale=locale_a),
    )
    assert len(top_contribs) == 2
    assert top_contribs[0] == contributors[2]
    assert top_contribs[0].translations_count == 24
    assert top_contribs[0].translations_approved_count == 10
    assert top_contribs[0].translations_unapproved_count == 12
    assert top_contribs[0].translations_needs_work_count == 2
    assert top_contribs[1] == contributors[0]
    assert top_contribs[1].translations_count == 15
    assert top_contribs[1].translations_approved_count == 12
    assert top_contribs[1].translations_unapproved_count == 1
    assert top_contribs[1].translations_needs_work_count == 2

    top_contribs = get_user_model().translators.with_translation_counts(
        aware_datetime(2015, 1, 1),
        Q(locale=locale_b),
    )
    assert len(top_contribs) == 1
    assert top_contribs[0] == contributors[1]
    assert top_contribs[0].translations_count == 14
    assert top_contribs[0].translations_approved_count == 11
    assert top_contribs[0].translations_unapproved_count == 1
    assert top_contribs[0].translations_needs_work_count == 2