Exemplo n.º 1
0
def test_view_tm_minimal_quality(client, locale_a, resource_a):
    """
    View shouldn't return any entries if 70% of quality at minimum.
    """
    entities = [
        EntityFactory(resource=resource_a, string='Entity %s' % i, order=i)
        for i in range(5)
    ]
    for i, entity in enumerate(entities):
        TranslationMemoryFactory.create(
            entity=entity,
            source="source %s" % entity.string,
            target="target %s" % entity.string,
            locale=locale_a,
        )
    response = client.get(
        '/translation-memory/',
        {
            'text': 'no match',
            'pk': entities[0].pk,
            'locale': locale_a.code,
        }
    )
    assert response.status_code == 200
    assert json.loads(response.content) == []
Exemplo n.º 2
0
def test_view_translation_memory_best_quality_entry(
    client, locale_a, resource_a,
):
    """
    Translation memory should return results entries aggregated by
    translation string.
    """
    entities = [
        EntityFactory(resource=resource_a, string="Entity %s" % i, order=i)
        for i in range(3)
    ]
    tm = TranslationMemoryFactory.create(
        entity=entities[0], source="aaa", target="ccc", locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1], source="aaa", target="ddd", locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[2], source="bbb", target="ccc", locale=locale_a,
    )
    response = client.get(
        "/translation-memory/",
        {"text": "aaa", "pk": tm.entity.pk, "locale": locale_a.code},
    )
    assert json.loads(response.content) == [
        {"count": 1, "source": u"aaa", "quality": u"100", "target": u"ddd"}
    ]
Exemplo n.º 3
0
def test_view_tm_minimal_quality(client, locale_a, resource_a):
    """
    View shouldn't return any entries if 70% of quality at minimum.
    """
    entities = [
        EntityFactory(resource=resource_a, string='Entity %s' % i, order=i)
        for i in range(5)
    ]
    for i, entity in enumerate(entities):
        TranslationMemoryFactory.create(
            entity=entity,
            source="source %s" % entity.string,
            target="target %s" % entity.string,
            locale=locale_a,
        )
    response = client.get(
        '/translation-memory/',
        {
            'text': 'no match',
            'pk': entities[0].pk,
            'locale': locale_a.code,
        }
    )
    assert response.status_code == 200
    assert json.loads(response.content) == []
Exemplo n.º 4
0
def test_get_translations(gt_mock, locale_b, resource_a, google_translate_locale):
    entities = [
        EntityFactory(resource=resource_a, string=x, order=i)
        for i, x in enumerate(["abaa", "abac", "aaab", "abab"])
    ]

    entities[1].string_plural = entities[1].string
    entities[3].string_plural = entities[3].string
    entities[1].save()
    entities[3].save()

    google_translate_locale.cldr_plurals = "1, 2"
    google_translate_locale.save()

    for entity in entities[0:2]:
        TranslationMemoryFactory.create(
            entity=entity, source=entity.string, target=entity.string, locale=locale_b,
        )
        TranslationMemoryFactory.create(
            entity=entity,
            source=entity.string,
            target=entity.string,
            locale=google_translate_locale,
        )

    # Mock the return value of get_google_translate_data
    gt_mock.return_value = {
        "status": True,
        "translation": "gt_translation",
    }

    tm_user = User.objects.get(email="*****@*****.**")
    gt_user = User.objects.get(email="*****@*****.**")

    # 100% match exists in translation memory.
    response_a = get_translations(entities[0], locale_b)
    response_b = get_translations(entities[0], google_translate_locale)
    assert response_a == [(entities[0].string, None, tm_user)]
    assert response_b == [(entities[0].string, None, tm_user)]

    # 100% match does not exists and locale.google_translate_code is None.
    response = get_translations(entities[2], locale_b)
    assert response == []

    # 100% match does not exists and locale.google_translate_code is not None.
    response = get_translations(entities[2], google_translate_locale)
    assert response == [("gt_translation", None, gt_user)]

    # Entity.string_plural is not None.
    response_a = get_translations(entities[1], google_translate_locale)
    response_b = get_translations(entities[3], google_translate_locale)
    assert response_a == [
        (entities[1].string, 0, tm_user),
        (entities[1].string, 1, tm_user),
    ]
    assert response_b == [
        ("gt_translation", 0, gt_user),
        ("gt_translation", 1, gt_user),
    ]
Exemplo n.º 5
0
def test_view_concordance_search(client, project_a, locale_a, resource_a):
    entities = [
        EntityFactory(
            resource=resource_a,
            string=x,
            order=i,
        ) for i, x in enumerate(["abaa", "aBaf", "aaAb", "aAab"])
    ]
    TranslationMemoryFactory.create(
        entity=entities[0],
        source=entities[0].string,
        target="ccc",
        locale=locale_a,
        project=project_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1],
        source=entities[1].string,
        target="cCDd",
        locale=locale_a,
        project=project_a,
    )

    response = client.get(
        "/concordance-search/",
        {
            "text": "cdd",
            "locale": locale_a.code
        },
    )
    result = json.loads(response.content)
    assert result == {
        "results": [{
            "source": "aBaf",
            "target": "cCDd",
            "project_names": [project_a.name]
        }],
        "has_next":
        False,
    }

    response = client.get(
        "/concordance-search/",
        {
            "text": "abaa",
            "locale": locale_a.code
        },
    )
    result = json.loads(response.content)
    assert result == {
        "results": [{
            "source": "abaa",
            "target": "ccc",
            "project_names": [project_a.name]
        }],
        "has_next":
        False,
    }
Exemplo n.º 6
0
def test_view_concordance_search(client, project_a, locale_a, resource_a):
    entities = [
        EntityFactory(
            resource=resource_a,
            string=x,
            order=i,
        ) for i, x in enumerate(["abaa", "abaf", "aaab", "aaab"])
    ]
    TranslationMemoryFactory.create(
        entity=entities[0],
        source=entities[0].string,
        target="ccc",
        locale=locale_a,
        project=project_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1],
        source=entities[1].string,
        target="ccdd",
        locale=locale_a,
        project=project_a,
    )

    response = client.get(
        "/concordance-search/",
        {
            "text": "cdd",
            "locale": locale_a.code
        },
    )
    result = json.loads(response.content)
    assert result == [{
        u"project_name": project_a.name,
        u"quality": 86,
        u"source": u"abaf",
        u"target": u"ccdd",
    }]

    response = client.get(
        "/concordance-search/",
        {
            "text": "abaa",
            "locale": locale_a.code
        },
    )
    result = json.loads(response.content)
    assert result == [{
        u"project_name": project_a.name,
        u"quality": 100,
        u"source": u"abaa",
        u"target": u"ccc",
    }]
Exemplo n.º 7
0
def test_view_translation_memory_best_quality_entry(
    client,
    locale_a,
    resource_a,
):
    """
    Translation memory should return results entries aggregated by
    translation string.
    """
    entities = [
        EntityFactory(resource=resource_a, string='Entity %s' % i, order=i)
        for i in range(3)
    ]
    tm = TranslationMemoryFactory.create(
        entity=entities[0],
        source="aaa",
        target="ccc",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1],
        source="aaa",
        target="ddd",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[2],
        source="bbb",
        target="ccc",
        locale=locale_a,
    )
    response = client.get(
        '/translation-memory/',
        {
            'text': 'aaa',
            'pk': tm.entity.pk,
            'locale': locale_a.code,
        }
    )
    assert (
        json.loads(response.content)
        == [{
            "count": 1,
            "source": u"aaa",
            "quality": u"100",
            "target": u"ddd",
        }]
    )
Exemplo n.º 8
0
def test_view_tm_best_quality_entry(
    client,
    locale_a,
    resource_a,
):
    """
    Translation memory should return results entries aggregated by
    translation string.
    """
    entities = [
        EntityFactory(resource=resource_a, string='Entity %s' % i, order=i)
        for i in range(3)
    ]
    tm = TranslationMemoryFactory.create(
        entity=entities[0],
        source="aaa",
        target="ccc",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1],
        source="aaa",
        target="ddd",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[2],
        source="bbb",
        target="ccc",
        locale=locale_a,
    )
    response = client.get(
        '/translation-memory/',
        {
            'text': 'aaa',
            'pk': tm.entity.pk,
            'locale': locale_a.code,
        }
    )
    assert (
        json.loads(response.content)
        == [{
            "count": 1,
            "source": "aaa",
            "quality": 100.0,
            "target": "ddd",
        }]
    )
Exemplo n.º 9
0
def test_view_translation_memory_translation_counts(
    client, locale_a, resource_a,
):
    """
    Translation memory should aggregate identical translations strings
    from the different entities and count up their occurrences.
    """
    entities = [
        EntityFactory(resource=resource_a, string=x, order=i)
        for i, x in enumerate(["abaa", "abaa", "aaab", "aaab"])
    ]
    tm = TranslationMemoryFactory.create(
        entity=entities[0], source=entities[0].string, target="ccc", locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1], source=entities[1].string, target="ccc", locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[2], source=entities[2].string, target="ccc", locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[3], source=entities[3].string, target="ccc", locale=locale_a,
    )
    response = client.get(
        "/translation-memory/",
        {"text": "aaaa", "pk": tm.entity.pk, "locale": locale_a.code},
    )
    result = json.loads(response.content)
    assert result[0].pop("source") in ("abaa", "aaab", "aaab")
    assert result == [{u"count": 3, u"quality": u"75", u"target": u"ccc"}]
Exemplo n.º 10
0
def test_view_concordance_search_remove_duplicates(
    client, project_a, locale_a, resource_a
):
    """Check Concordance search doesn't produce duplicated search results."""
    entities = [
        EntityFactory(resource=resource_a, string=x, order=i,)
        for i, x in enumerate(["abaa", "abaf"])
    ]
    TranslationMemoryFactory.create(
        entity=entities[0],
        source=entities[0].string,
        target="ccc",
        locale=locale_a,
        project=project_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1],
        source=entities[1].string,
        target="ccc",
        locale=locale_a,
        project=project_a,
    )

    TranslationMemoryFactory.create(
        entity=entities[1],
        source=entities[1].string,
        target="cccbbb",
        locale=locale_a,
        project=project_a,
    )

    TranslationMemoryFactory.create(
        entity=entities[1],
        source=entities[1].string,
        target="cccbbb",
        locale=locale_a,
        project=project_a,
    )

    response = client.get(
        "/concordance-search/", {"text": "ccc", "locale": locale_a.code},
    )
    results = json.loads(response.content)
    assert results == {
        "results": [
            {"source": "abaa", "target": "ccc", "project_names": [project_a.name]},
            {"source": "abaf", "target": "ccc", "project_names": [project_a.name]},
            {"source": "abaf", "target": "cccbbb", "project_names": [project_a.name]},
        ],
        "has_next": False,
    }
Exemplo n.º 11
0
def test_view_tm_translation_counts(
    client,
    locale_a,
    resource_a,
):
    """
    Translation memory should aggregate identical translations strings
    from the different entities and count up their occurrences.
    """
    entities = [
        EntityFactory(resource=resource_a, string=x, order=i)
        for i, x in enumerate(["abaa", "abaa", "aaab", "aaab"])
    ]
    tm = TranslationMemoryFactory.create(
        entity=entities[0],
        source=entities[0].string,
        target="ccc",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1],
        source=entities[1].string,
        target="ccc",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[2],
        source=entities[2].string,
        target="ccc",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[3],
        source=entities[3].string,
        target="ccc",
        locale=locale_a,
    )
    response = client.get(
        '/translation-memory/',
        {
            'text': 'aaaa',
            'pk': tm.entity.pk,
            'locale': locale_a.code,
        }
    )
    result = json.loads(response.content)
    assert result[0].pop('source') in ('abaa', 'aaab', 'aaab')
    assert (
        result
        == [{
            u'count': 3,
            u'quality': 75.0,
            u'target': u'ccc'
        }]
    )
Exemplo n.º 12
0
def test_view_translation_memory_translation_counts(
    client,
    locale_a,
    resource_a,
):
    """
    Translation memory should aggregate identical translations strings
    from the different entities and count up their occurrences.
    """
    entities = [
        EntityFactory(resource=resource_a, string=x, order=i)
        for i, x in enumerate(["abaa", "abaa", "aaab", "aaab"])
    ]
    tm = TranslationMemoryFactory.create(
        entity=entities[0],
        source=entities[0].string,
        target="ccc",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1],
        source=entities[1].string,
        target="ccc",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[2],
        source=entities[2].string,
        target="ccc",
        locale=locale_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[3],
        source=entities[3].string,
        target="ccc",
        locale=locale_a,
    )
    response = client.get(
        '/translation-memory/',
        {
            'text': 'aaaa',
            'pk': tm.entity.pk,
            'locale': locale_a.code,
        }
    )
    result = json.loads(response.content)
    assert result[0].pop('source') in ('abaa', 'aaab', 'aaab')
    assert (
        result
        == [{
            u'count': 3,
            u'quality': u'75',
            u'target': u'ccc'
        }]
    )
Exemplo n.º 13
0
def test_view_tm_exclude_entity(client, entity_a, locale_a, resource_a):
    """
    Exclude entity from results to avoid false positive results.
    """
    tm = TranslationMemoryFactory.create(
        entity=entity_a, source=entity_a.string, target="ccc", locale=locale_a,
    )
    response = client.get(
        "/translation-memory/",
        {"text": entity_a.string, "pk": entity_a.pk, "locale": tm.locale.code},
    )
    assert response.status_code == 200
    assert json.loads(response.content) == []
Exemplo n.º 14
0
def test_view_tm_exclude_entity(client, entity_a, locale_a, resource_a):
    """
    Exclude entity from results to avoid false positive results.
    """
    tm = TranslationMemoryFactory.create(
        entity=entity_a,
        source=entity_a.string,
        target="ccc",
        locale=locale_a,
    )
    response = client.get(
        '/translation-memory/',
        {
            'text': entity_a.string,
            'pk': entity_a.pk,
            'locale': tm.locale.code,
        }
    )
    assert response.status_code == 200
    assert json.loads(response.content) == []
Exemplo n.º 15
0
def tm_entry_short():
    return TranslationMemoryFactory.create(source=u"a" * 50, )
Exemplo n.º 16
0
def tm_entry_medium():
    return TranslationMemoryFactory.create(source=u"a" * 255, )
Exemplo n.º 17
0
def tm_entry_long():
    return TranslationMemoryFactory.create(source=u"a" * 500, )
Exemplo n.º 18
0
def test_view_concordance_search_pagination(client, project_a, locale_a, resource_a):
    entities = [
        EntityFactory(resource=resource_a, string=x, order=i)
        for i, x in enumerate(["abaa", "abaf"])
    ]
    TranslationMemoryFactory.create(
        entity=entities[0],
        source=entities[0].string,
        target="ccc",
        locale=locale_a,
        project=project_a,
    )
    TranslationMemoryFactory.create(
        entity=entities[1],
        source=entities[1].string,
        target="cccbbb",
        locale=locale_a,
        project=project_a,
    )

    TranslationMemoryFactory.create(
        entity=entities[1],
        source=entities[1].string,
        target="cccbbb",
        locale=locale_a,
        project=project_a,
    )

    response = client.get(
        "/concordance-search/", {"text": "ccc", "locale": locale_a.code, "limit": 1},
    )
    results = json.loads(response.content)
    assert results == {
        "results": [
            {"source": "abaa", "target": "ccc", "project_names": [project_a.name]},
        ],
        "has_next": True,
    }

    response = client.get(
        "/concordance-search/",
        {"text": "ccc", "locale": locale_a.code, "limit": 1, "page": 2},
    )
    results = json.loads(response.content)
    assert results == {
        "results": [
            {"source": "abaf", "target": "cccbbb", "project_names": [project_a.name]},
        ],
        "has_next": False,
    }

    # Check a query that should return no results
    response = client.get(
        "/concordance-search/",
        {"text": "TEST", "locale": locale_a.code, "limit": 1, "page": 2},
    )
    results = json.loads(response.content)
    assert results == {
        "results": [],
        "has_next": False,
    }
Exemplo n.º 19
0
def tm_entry_medium():
    return TranslationMemoryFactory.create(
        source=u'a' * 255,
    )
Exemplo n.º 20
0
def tm_entry_short():
    return TranslationMemoryFactory.create(
        source=u'a' * 50,
    )
Exemplo n.º 21
0
def tm_entry_long():
    return TranslationMemoryFactory.create(
        source=u'a' * 500,
    )