|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
5 | | -from pontoon.base.models import Translation |
| 5 | +from django.contrib.messages import get_messages |
| 6 | + |
| 7 | +from pontoon.base.models import ChangedEntityLocale, Translation |
| 8 | +from pontoon.test.factories import EntityFactory, TranslationFactory |
6 | 9 |
|
7 | 10 |
|
8 | 11 | @pytest.fixture |
@@ -49,6 +52,39 @@ def upload(client, **args): |
49 | 52 | return response |
50 | 53 |
|
51 | 54 |
|
| 55 | +@pytest.fixture |
| 56 | +def approved_po_translation(po_translation): |
| 57 | + po_translation.approved = True |
| 58 | + po_translation.active = True |
| 59 | + po_translation.save() |
| 60 | + |
| 61 | + yield po_translation |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def upload_po(translator_a, project_locale_a, po_translation): |
| 66 | + """ |
| 67 | + Upload the given contents as a .po file for the `po_translation` resource, |
| 68 | + returning the list of (tags, message) pairs. |
| 69 | + """ |
| 70 | + |
| 71 | + def _upload_po(po_contents): |
| 72 | + with NamedTemporaryFile("w+", suffix=".po") as fp: |
| 73 | + fp.write(po_contents) |
| 74 | + fp.flush() |
| 75 | + response = upload( |
| 76 | + translator_a.client, |
| 77 | + slug=project_locale_a.project.slug, |
| 78 | + code=project_locale_a.locale.code, |
| 79 | + part=po_translation.entity.resource.path, |
| 80 | + uploadfile=open(fp.name), |
| 81 | + ) |
| 82 | + assert response.status_code == 303 |
| 83 | + return [(m.tags, m.message) for m in get_messages(response.wsgi_request)] |
| 84 | + |
| 85 | + return _upload_po |
| 86 | + |
| 87 | + |
52 | 88 | @pytest.mark.django_db |
53 | 89 | def test_upload_login_required( |
54 | 90 | client, |
@@ -144,33 +180,174 @@ def test_upload_project_locale_is_readonly( |
144 | 180 |
|
145 | 181 |
|
146 | 182 | @pytest.mark.django_db |
147 | | -def test_upload_file( |
148 | | - translator_a, |
149 | | - project_locale_a, |
150 | | - po_translation, |
151 | | -): |
| 183 | +def test_upload_file(upload_po, po_translation): |
152 | 184 | """ |
153 | 185 | Test a positive upload which changes the translation. |
154 | 186 | """ |
155 | | - po_contents = 'msgid "test_key"\nmsgstr "new translation"' |
156 | | - with NamedTemporaryFile("w+", suffix=".po") as fp: |
157 | | - fp.write(po_contents) |
158 | | - fp.flush() |
159 | | - response = upload( |
160 | | - translator_a.client, |
161 | | - slug=project_locale_a.project.slug, |
162 | | - code=project_locale_a.locale.code, |
163 | | - part=po_translation.entity.resource.path, |
164 | | - uploadfile=open(fp.name), |
165 | | - ) |
166 | | - |
167 | | - assert response.status_code == 303 |
| 187 | + messages = upload_po('msgid "test_key"\nmsgstr "new translation"') |
| 188 | + assert messages == [ |
| 189 | + ("upload success", "Translations uploaded: 1 updated, 0 unchanged.") |
| 190 | + ] |
168 | 191 |
|
169 | 192 | translation = Translation.objects.get(string="new translation") |
170 | | - |
171 | | - assert translation.entity.key == ["test_key"] |
172 | | - assert translation.entity.resource.path == "resource_a.po" |
| 193 | + assert translation.entity == po_translation.entity |
173 | 194 | assert translation.approved |
174 | 195 | assert translation.user |
175 | 196 | assert not translation.warnings.exists() |
176 | 197 | assert not translation.errors.exists() |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.django_db |
| 201 | +def test_upload_is_additive(upload_po, approved_po_translation, locale_a): |
| 202 | + """ |
| 203 | + Approved translations missing from the uploaded file are left untouched. |
| 204 | + """ |
| 205 | + resource = approved_po_translation.entity.resource |
| 206 | + other_translation = TranslationFactory( |
| 207 | + entity=EntityFactory(resource=resource, string="other", key=["other_key"]), |
| 208 | + locale=locale_a, |
| 209 | + string="other translation", |
| 210 | + value=["other translation"], |
| 211 | + approved=True, |
| 212 | + active=True, |
| 213 | + ) |
| 214 | + |
| 215 | + messages = upload_po('msgid "test_key"\nmsgstr "new translation"') |
| 216 | + assert messages == [ |
| 217 | + ("upload success", "Translations uploaded: 1 updated, 0 unchanged.") |
| 218 | + ] |
| 219 | + |
| 220 | + approved_po_translation.refresh_from_db() |
| 221 | + assert not approved_po_translation.approved |
| 222 | + assert approved_po_translation.rejected |
| 223 | + assert ( |
| 224 | + Translation.objects.get( |
| 225 | + entity=approved_po_translation.entity, approved=True |
| 226 | + ).string |
| 227 | + == "new translation" |
| 228 | + ) |
| 229 | + |
| 230 | + other_translation.refresh_from_db() |
| 231 | + assert other_translation.approved |
| 232 | + assert other_translation.active |
| 233 | + assert not other_translation.rejected |
| 234 | + |
| 235 | + |
| 236 | +@pytest.mark.django_db |
| 237 | +def test_upload_approves_matching_suggestion(upload_po, po_translation, locale_a): |
| 238 | + """ |
| 239 | + An uploaded translation matching an existing suggestion approves it, |
| 240 | + and the entity is still marked as changed for the next sync. |
| 241 | + """ |
| 242 | + assert not po_translation.approved |
| 243 | + |
| 244 | + messages = upload_po(f'msgid "test_key"\nmsgstr "{po_translation.string}"') |
| 245 | + assert messages == [ |
| 246 | + ("upload success", "Translations uploaded: 1 updated, 0 unchanged.") |
| 247 | + ] |
| 248 | + |
| 249 | + assert Translation.objects.filter(entity=po_translation.entity).count() == 1 |
| 250 | + po_translation.refresh_from_db() |
| 251 | + assert po_translation.approved |
| 252 | + assert ChangedEntityLocale.objects.filter( |
| 253 | + entity=po_translation.entity, locale=locale_a |
| 254 | + ).exists() |
| 255 | + |
| 256 | + |
| 257 | +@pytest.mark.django_db |
| 258 | +def test_upload_approves_matching_fuzzy_translation(upload_po, po_translation): |
| 259 | + """ |
| 260 | + An uploaded non-fuzzy translation matching an existing fuzzy one approves it. |
| 261 | + """ |
| 262 | + po_translation.fuzzy = True |
| 263 | + po_translation.active = True |
| 264 | + po_translation.save() |
| 265 | + |
| 266 | + messages = upload_po(f'msgid "test_key"\nmsgstr "{po_translation.string}"') |
| 267 | + assert messages == [ |
| 268 | + ("upload success", "Translations uploaded: 1 updated, 0 unchanged.") |
| 269 | + ] |
| 270 | + |
| 271 | + assert Translation.objects.filter(entity=po_translation.entity).count() == 1 |
| 272 | + po_translation.refresh_from_db() |
| 273 | + assert po_translation.approved |
| 274 | + assert not po_translation.fuzzy |
| 275 | + |
| 276 | + |
| 277 | +@pytest.mark.django_db |
| 278 | +def test_upload_identical_translation_is_ignored(upload_po, approved_po_translation): |
| 279 | + messages = upload_po(f'msgid "test_key"\nmsgstr "{approved_po_translation.string}"') |
| 280 | + assert messages == [ |
| 281 | + ("upload info", "Translations uploaded: 0 updated, 1 unchanged.") |
| 282 | + ] |
| 283 | + |
| 284 | + assert ( |
| 285 | + Translation.objects.filter(entity=approved_po_translation.entity).count() == 1 |
| 286 | + ) |
| 287 | + approved_po_translation.refresh_from_db() |
| 288 | + assert approved_po_translation.approved |
| 289 | + |
| 290 | + |
| 291 | +@pytest.mark.django_db |
| 292 | +def test_upload_undefined_keys_are_reported(upload_po, po_translation): |
| 293 | + """ |
| 294 | + Translations for keys that are missing or obsolete in Pontoon are ignored. |
| 295 | + """ |
| 296 | + resource = po_translation.entity.resource |
| 297 | + EntityFactory(resource=resource, string="old", key=["obsolete_key"], obsolete=True) |
| 298 | + |
| 299 | + messages = upload_po( |
| 300 | + 'msgid "test_key"\nmsgstr "new translation"\n\n' |
| 301 | + 'msgid "obsolete_key"\nmsgstr "obsolete translation"\n\n' |
| 302 | + 'msgid "missing_key"\nmsgstr "missing translation"\n' |
| 303 | + ) |
| 304 | + assert messages == [ |
| 305 | + ( |
| 306 | + "upload success", |
| 307 | + "Translations uploaded: 1 updated, 0 unchanged, 2 not found in Pontoon.", |
| 308 | + ) |
| 309 | + ] |
| 310 | + assert set( |
| 311 | + Translation.objects.filter(entity__resource=resource).values_list( |
| 312 | + "string", flat=True |
| 313 | + ) |
| 314 | + ) == {po_translation.string, "new translation"} |
| 315 | + |
| 316 | + |
| 317 | +@pytest.mark.django_db |
| 318 | +def test_upload_ignores_translations_of_obsolete_entities( |
| 319 | + upload_po, po_translation, locale_a |
| 320 | +): |
| 321 | + """ |
| 322 | + A key removed and later re-added leaves an obsolete entity with the same key. |
| 323 | + If the new key is untranslated, the upload should fill it and be counted as |
| 324 | + translated, even if that matches the original translation in the obsolete key. |
| 325 | + """ |
| 326 | + TranslationFactory( |
| 327 | + entity=EntityFactory( |
| 328 | + resource=po_translation.entity.resource, |
| 329 | + string="entity a", |
| 330 | + key=["test_key"], |
| 331 | + obsolete=True, |
| 332 | + ), |
| 333 | + locale=locale_a, |
| 334 | + string="new translation", |
| 335 | + value=["new translation"], |
| 336 | + approved=True, |
| 337 | + active=True, |
| 338 | + ) |
| 339 | + |
| 340 | + messages = upload_po('msgid "test_key"\nmsgstr "new translation"') |
| 341 | + assert messages == [ |
| 342 | + ("upload success", "Translations uploaded: 1 updated, 0 unchanged.") |
| 343 | + ] |
| 344 | + assert ( |
| 345 | + Translation.objects.get(entity=po_translation.entity, approved=True).string |
| 346 | + == "new translation" |
| 347 | + ) |
| 348 | + |
| 349 | + |
| 350 | +@pytest.mark.django_db |
| 351 | +def test_upload_file_without_translations(upload_po): |
| 352 | + messages = upload_po("# Just a comment\n") |
| 353 | + assert messages == [("error", "No translations found in uploaded file.")] |
0 commit comments