Skip to content

Commit

Permalink
Include active fuzzy translations when writing to repo
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Dec 17, 2024
1 parent 74e4a14 commit 357f7fe
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 112 deletions.
3 changes: 2 additions & 1 deletion pontoon/sync/core/translations_to_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ def update_changed_resources(
entity__resource__project_id=project.pk,
entity__resource__path=path,
locale__in=[locale.pk for locale in locales],
active=True,
)
.filter(Q(approved=True) | Q(pretranslated=True))
.filter(Q(approved=True) | Q(pretranslated=True) | Q(fuzzy=True))
.exclude(approved_date__gt=now) # includes approved_date = None
.select_related("entity")
)
Expand Down
149 changes: 145 additions & 4 deletions pontoon/sync/tests/test_e2e.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re

from os import makedirs
from os.path import join
from re import fullmatch
from tempfile import TemporaryDirectory
from textwrap import dedent
from unittest.mock import patch
Expand All @@ -9,14 +10,14 @@

from django.conf import settings

from pontoon.base.models import ChangedEntityLocale
from pontoon.base.models.translated_resource import TranslatedResource
from pontoon.base.models import ChangedEntityLocale, TranslatedResource, Translation
from pontoon.base.tests import (
EntityFactory,
LocaleFactory,
ProjectFactory,
RepositoryFactory,
ResourceFactory,
TranslatedResourceFactory,
TranslationFactory,
)
from pontoon.sync.tasks import sync_project_task
Expand Down Expand Up @@ -64,6 +65,7 @@ def test_end_to_end():
entity=entity,
locale=locale,
string=f"key-{i} = New translation {locale.code[:2]} {i}\n",
active=True,
approved=True,
)

Expand Down Expand Up @@ -138,7 +140,7 @@ def test_end_to_end():
),
("revision", (tgt_root,)),
]
assert fullmatch(
assert re.fullmatch(
dedent(
r"""
Pontoon/test-project: Update Test (German|French) \((de|fr)-Test\), Test (German|French) \((de|fr)-Test\)
Expand Down Expand Up @@ -177,6 +179,7 @@ def test_translation_before_source():
),
locale=locale_de,
string="a0 = Translation 0\n",
active=True,
approved=True,
)

Expand All @@ -187,6 +190,7 @@ def test_translation_before_source():
),
locale=locale_de,
string="b0 = Translation 0\n",
active=True,
approved=True,
)

Expand Down Expand Up @@ -231,3 +235,140 @@ def test_translation_before_source():
# Test -- New a0 translation is picked up, added a1 is dropped
with open(join(repo_tgt.checkout_path, "de-Test", "a.ftl")) as file:
assert file.read() == "a0 = New translation 0\n"


@pytest.mark.django_db
def test_fuzzy():
with TemporaryDirectory() as root:
# Database setup
settings.MEDIA_ROOT = root
synclog = SyncLogFactory.create()
locale = LocaleFactory.create(code="fr-Test", name="Test French")
repo = RepositoryFactory(url="http://example.com/repo")
project = ProjectFactory.create(
name="test-write-fuzzy", locales=[locale], repositories=[repo]
)
res = ResourceFactory.create(project=project, path="res.po", format="po")
TranslatedResourceFactory.create(locale=locale, resource=res)
for i in range(5):
string = f"Message {i}\n"
fuzzy = i < 3
entity = EntityFactory.create(resource=res, key=f"key-{i}", string=string)
TranslationFactory.create(
entity=entity,
locale=locale,
string=string.replace("Message", "Fuzzy" if fuzzy else "Translation"),
active=True,
approved=not fuzzy,
fuzzy=fuzzy,
)
ChangedEntityLocale.objects.filter(entity__resource__project=project).delete()

# Filesystem setup
res_src = dedent(
"""
#, fuzzy
msgid "key-0"
msgstr ""
#, fuzzy
msgid "key-1"
msgstr ""
msgid "key-2"
msgstr ""
msgid "key-3"
msgstr ""
#, fuzzy
msgid "key-4"
msgstr ""
"""
)
res_tgt = dedent(
"""
#, fuzzy
msgid "key-0"
msgstr "Fuzzy 0"
#, fuzzy
msgid "key-1"
msgstr "Fuzzy Changed 1"
msgid "key-2"
msgstr "Not Fuzzy 2"
msgid "key-3"
msgstr "Translation 3"
#, fuzzy
msgid "key-4"
msgstr "Made Fuzzy 4"
"""
)
makedirs(repo.checkout_path)
build_file_tree(
repo.checkout_path,
{"en-US": {"res.pot": res_src}, "fr-Test": {"res.po": res_tgt}},
)

# Sync
mock_vcs = MockVersionControl(
changes=([join("en-US", "res.pot"), join("fr-test", "res.po")], [], [])
)
with (
patch("pontoon.sync.core.checkout.get_repo", return_value=mock_vcs),
patch(
"pontoon.sync.core.translations_to_repo.get_repo",
return_value=mock_vcs,
),
):
sync_project_task(project.id, synclog.id)

# Test
trans = Translation.objects.filter(
entity__resource=res, locale=locale, active=True
).values_list("string", flat=True)
assert set(trans) == {
"Fuzzy 0",
"Fuzzy Changed 1",
"Not Fuzzy 2",
"Translation 3",
"Made Fuzzy 4",
}
assert set(trans.filter(fuzzy=True)) == {
"Fuzzy 0",
"Fuzzy Changed 1",
"Made Fuzzy 4",
}
assert set(trans.filter(approved=True)) == {
"Not Fuzzy 2",
"Translation 3",
}
with open(join(repo.checkout_path, "fr-Test", "res.po")) as file:
assert re.sub(r'^".*"\n', "", file.read(), flags=re.MULTILINE) == dedent(
"""\
#
msgid ""
msgstr ""
#, fuzzy
msgid "key-0"
msgstr "Fuzzy 0"
#, fuzzy
msgid "key-1"
msgstr "Fuzzy Changed 1"
msgid "key-2"
msgstr "Not Fuzzy 2"
msgid "key-3"
msgstr "Translation 3"
#, fuzzy
msgid "key-4"
msgstr "Made Fuzzy 4"
"""
)
1 change: 1 addition & 0 deletions pontoon/sync/tests/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def test_update_resource():
entity=entity,
locale=locale,
string=f"key-{n}-{i} = Translation {i}\n",
active=True,
approved=True,
)

Expand Down
110 changes: 3 additions & 107 deletions pontoon/sync/tests/test_translations_from_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
from django.conf import settings
from django.utils import timezone

from pontoon.base.models import (
ChangedEntityLocale,
TranslatedResource,
Translation,
TranslationMemoryEntry,
)
from pontoon.base.models import TranslatedResource, Translation, TranslationMemoryEntry
from pontoon.base.tests import (
EntityFactory,
LocaleFactory,
Expand Down Expand Up @@ -65,6 +60,7 @@ def test_add_ftl_translation():
entity=entity,
locale=locale,
string=string.replace("Message", "Translation"),
active=True,
approved=True,
)

Expand Down Expand Up @@ -148,6 +144,7 @@ def test_remove_po_target_resource():
entity=entity,
locale=locale,
string=string.replace("Message", "Translation"),
active=True,
approved=True,
)

Expand Down Expand Up @@ -184,104 +181,3 @@ def test_remove_po_target_resource():
update_stats(project)
project.refresh_from_db()
assert (project.total_strings, project.approved_strings) == (6, 6)


@pytest.mark.django_db
def test_update_fuzzy_po_translation():
with TemporaryDirectory() as root:
# Database setup
settings.MEDIA_ROOT = root
locale = LocaleFactory.create(code="fr-Test", name="Test French")
locale_map = {locale.code: locale}
repo = RepositoryFactory(url="http://example.com/repo")
project = ProjectFactory.create(
name="test-fuzzy-po", locales=[locale], repositories=[repo]
)
res = ResourceFactory.create(
project=project, path="res.po", format="po", total_strings=3
)
TranslatedResourceFactory.create(locale=locale, resource=res)
for i in range(5):
key = f"key-{i}"
string = f"Message {i}"
fuzzy = i < 3
entity = EntityFactory.create(resource=res, string=string, key=key)
TranslationFactory.create(
entity=entity,
locale=locale,
string=string.replace("Message", "Fuzzy" if fuzzy else "Translation"),
active=True,
approved=not fuzzy,
fuzzy=fuzzy,
)
ChangedEntityLocale.objects.filter(entity__resource=res).delete()

# Filesystem setup
res_po = dedent(
"""
#, fuzzy
msgid "key-0"
msgstr "Fuzzy 0"
#, fuzzy
msgid "key-1"
msgstr "Fuzzy Changed 1"
msgid "key-2"
msgstr "Not Fuzzy 2"
msgid "key-3"
msgstr "Translation 3"
#, fuzzy
msgid "key-4"
msgstr "Made Fuzzy 4"
"""
)
makedirs(repo.checkout_path)
build_file_tree(
repo.checkout_path,
{"en-US": {"res.pot": ""}, "fr-Test": {"res.po": res_po}},
)

# Paths setup
mock_checkout = Mock(
Checkout,
path=repo.checkout_path,
changed=[join("fr-Test", "res.po")],
removed=[],
)
checkouts = Checkouts(mock_checkout, mock_checkout)
paths = find_paths(project, checkouts)

# Test sync
sync_translations_from_repo(project, locale_map, checkouts, paths, [], now)
trans = Translation.objects.filter(
entity__resource=res, locale=locale
).values_list("string", flat=True)
assert set(trans) == {
"Fuzzy 0",
"Fuzzy 1",
"Fuzzy Changed 1",
"Fuzzy 2",
"Not Fuzzy 2",
"Translation 3",
"Translation 4",
"Made Fuzzy 4",
}
assert set(trans.filter(active=True)) == {
"Fuzzy 0",
"Fuzzy Changed 1",
"Not Fuzzy 2",
"Translation 3",
"Made Fuzzy 4",
}
assert set(trans.filter(fuzzy=True)) == {
"Fuzzy 0",
"Fuzzy Changed 1",
"Made Fuzzy 4",
}
assert set(trans.filter(approved=True)) == {
"Not Fuzzy 2",
"Translation 3",
}
2 changes: 2 additions & 0 deletions pontoon/sync/tests/test_translations_to_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def test_remove_entity():
entity=entity,
locale=locale,
string=f"key-{i} = Translation {i}\n",
active=True,
approved=True,
)

Expand Down Expand Up @@ -183,6 +184,7 @@ def test_add_translation():
entity=entity,
locale=locale,
string=f"key-{i} = Translation {i}\n",
active=True,
approved=True,
)

Expand Down

0 comments on commit 357f7fe

Please sign in to comment.