|
21 | 21 | from botocore.stub import ANY, Stubber |
22 | 22 | from django.core.management import call_command |
23 | 23 | from django.core.management.base import CommandError |
24 | | -from django.test import TestCase |
| 24 | +from django.test import SimpleTestCase, TestCase |
25 | 25 | from django.urls import reverse |
26 | 26 | from google.api_core import exceptions as google_api_exceptions |
27 | 27 | from google.cloud.translate import ( |
@@ -2972,6 +2972,20 @@ def test_exists(self) -> None: |
2972 | 2972 | results = machine.translate(unit, self.user) |
2973 | 2973 | self.assertNotEqual(results, []) |
2974 | 2974 |
|
| 2975 | + @patch("weblate.machinery.weblatetm.adjust_similarity_threshold") |
| 2976 | + def test_matches_still_probe_fuzzy_lookup(self, adjust_threshold) -> None: |
| 2977 | + unit = Unit.objects.filter(translation__language_code="cs")[0] |
| 2978 | + other = unit.translation.unit_set.exclude(pk=unit.pk)[0] |
| 2979 | + other.source = unit.source |
| 2980 | + other.target = "Preklad" |
| 2981 | + other.state = STATE_TRANSLATED |
| 2982 | + other.save() |
| 2983 | + |
| 2984 | + machine = WeblateTranslation({}) |
| 2985 | + machine.translate(unit, self.user) |
| 2986 | + |
| 2987 | + adjust_threshold.assert_called_once_with(0.98) |
| 2988 | + |
2975 | 2989 |
|
2976 | 2990 | class CyrTranslitTranslationTest(ViewTestCase, BaseMachineTranslationTest): |
2977 | 2991 | ENGLISH = "sr@latin" |
@@ -3318,6 +3332,92 @@ def test_configure_invalid(self) -> None: |
3318 | 3332 | ) |
3319 | 3333 |
|
3320 | 3334 |
|
| 3335 | +class WeblateTranslationLookupTest(SimpleTestCase): |
| 3336 | + @patch("weblate.machinery.weblatetm.Unit.objects.filter") |
| 3337 | + @patch("weblate.machinery.weblatetm.Translation.objects") |
| 3338 | + def test_get_base_queryset_uses_translation_subquery( |
| 3339 | + self, translation_objects, unit_filter |
| 3340 | + ) -> None: |
| 3341 | + machine = WeblateTranslation({}) |
| 3342 | + user = MagicMock() |
| 3343 | + translations = MagicMock() |
| 3344 | + filtered_translations = MagicMock() |
| 3345 | + translation_ids = MagicMock() |
| 3346 | + base = MagicMock() |
| 3347 | + |
| 3348 | + translation_objects.all.return_value = translations |
| 3349 | + translations.filter_access.return_value = filtered_translations |
| 3350 | + filtered_translations.filter.return_value = translation_ids |
| 3351 | + translation_ids.values.return_value = "translation-subquery" |
| 3352 | + unit_filter.return_value = base |
| 3353 | + |
| 3354 | + queryset = machine.get_base_queryset(user, "en", "cs") |
| 3355 | + |
| 3356 | + self.assertEqual(queryset, base) |
| 3357 | + translations.filter_access.assert_called_once_with(user) |
| 3358 | + filtered_translations.filter.assert_called_once_with( |
| 3359 | + component__source_language="en", |
| 3360 | + language="cs", |
| 3361 | + ) |
| 3362 | + translation_ids.values.assert_called_once_with("id") |
| 3363 | + unit_filter.assert_called_once_with( |
| 3364 | + state__gte=STATE_TRANSLATED, |
| 3365 | + translation_id__in="translation-subquery", |
| 3366 | + ) |
| 3367 | + |
| 3368 | + @patch("weblate.machinery.weblatetm.adjust_similarity_threshold") |
| 3369 | + def test_get_matching_units_uses_fuzzy_lookup(self, adjust_threshold) -> None: |
| 3370 | + machine = WeblateTranslation({}) |
| 3371 | + base = MagicMock() |
| 3372 | + queryset = MagicMock() |
| 3373 | + fuzzy_match = MagicMock(pk=1) |
| 3374 | + base.filter.return_value = queryset |
| 3375 | + |
| 3376 | + with patch.object(machine, "prepare_queryset", return_value=[fuzzy_match]): |
| 3377 | + results = machine.get_matching_units(base, "Hello", 75) |
| 3378 | + |
| 3379 | + self.assertEqual(results, [fuzzy_match]) |
| 3380 | + base.filter.assert_called_once_with(source__trgm_search="Hello") |
| 3381 | + adjust_threshold.assert_called_once_with(0.98) |
| 3382 | + |
| 3383 | + @patch("weblate.machinery.weblatetm.adjust_similarity_threshold") |
| 3384 | + def test_get_matching_units_orders_short_queries_before_slicing( |
| 3385 | + self, adjust_threshold |
| 3386 | + ) -> None: |
| 3387 | + machine = WeblateTranslation({}) |
| 3388 | + base = MagicMock() |
| 3389 | + short_queryset = MagicMock() |
| 3390 | + fuzzy_match = MagicMock(pk=1) |
| 3391 | + |
| 3392 | + with ( |
| 3393 | + patch.object( |
| 3394 | + machine, "get_short_query_matches", return_value=short_queryset |
| 3395 | + ) as get_short_query_matches, |
| 3396 | + patch.object(machine, "prepare_queryset", return_value=[fuzzy_match]), |
| 3397 | + ): |
| 3398 | + results = machine.get_matching_units(base, "id", 75) |
| 3399 | + |
| 3400 | + self.assertEqual(results, [fuzzy_match]) |
| 3401 | + get_short_query_matches.assert_called_once_with(base, "id") |
| 3402 | + adjust_threshold.assert_called_once_with(0.98) |
| 3403 | + |
| 3404 | + @patch("weblate.machinery.weblatetm.adjust_similarity_threshold") |
| 3405 | + def test_get_matching_units_uses_exact_lookup_at_full_threshold( |
| 3406 | + self, adjust_threshold |
| 3407 | + ) -> None: |
| 3408 | + machine = WeblateTranslation({}) |
| 3409 | + base = MagicMock() |
| 3410 | + queryset = MagicMock() |
| 3411 | + exact_match = MagicMock(pk=1) |
| 3412 | + base.filter.return_value = queryset |
| 3413 | + |
| 3414 | + with patch.object(machine, "prepare_queryset", return_value=[exact_match]): |
| 3415 | + results = machine.get_matching_units(base, "Hello", 100) |
| 3416 | + |
| 3417 | + self.assertEqual(results, [exact_match]) |
| 3418 | + adjust_threshold.assert_not_called() |
| 3419 | + |
| 3420 | + |
3321 | 3421 | class CommandTest(FixtureTestCase): |
3322 | 3422 | """Test for management commands.""" |
3323 | 3423 |
|
|
0 commit comments