|
14 | 14 | CountryAutoSharedModelTranslatedSerializer,
|
15 | 15 | CountryExplicitTranslatedSerializer,
|
16 | 16 | ContinentCountriesTranslatedSerializer,
|
17 |
| - PictureCaptionSerializer, |
| 17 | + PictureCaptionSerializer, FlatContinentCountriesTranslatedSerializer, FlatCountryTranslatedSerializer, |
| 18 | + FlatCountryNoLanguageCodeTranslatedSerializer, FlatCountryExplicitLangTranslatedSerializer, |
18 | 19 | )
|
19 | 20 |
|
20 | 21 |
|
@@ -292,3 +293,168 @@ def test_translation_deserialization(self):
|
292 | 293 | self.assertEqual(instance.caption, "Spanien")
|
293 | 294 | instance.set_current_language('es')
|
294 | 295 | self.assertEqual(instance.caption, "Spain") # fallback on default
|
| 296 | + |
| 297 | + |
| 298 | +class FlatCountryTranslatedSerializerTestCase(TestCase): |
| 299 | + # Disable cache as due to automatic db rollback the instance pk |
| 300 | + # is the same for all tests and with the cache we'd mistakenly |
| 301 | + # skips saves after the first test. |
| 302 | + @override_parler_settings(PARLER_ENABLE_CACHING=False) |
| 303 | + def setUp(self): |
| 304 | + self.instance = Country.objects.create( |
| 305 | + country_code='ES', name="Spain", |
| 306 | + url="http://en.wikipedia.org/wiki/Spain" |
| 307 | + ) |
| 308 | + self.instance.set_current_language('es') |
| 309 | + self.instance.name = "España" |
| 310 | + self.instance.url = "http://es.wikipedia.org/wiki/España" |
| 311 | + self.instance.save() |
| 312 | + |
| 313 | + def test_en_translations_serialization(self): |
| 314 | + self.instance.set_current_language('en') |
| 315 | + expected_en = { |
| 316 | + 'pk': self.instance.pk, |
| 317 | + 'country_code': 'ES', |
| 318 | + 'language_code': 'en', |
| 319 | + 'name': "Spain", |
| 320 | + 'url': "http://en.wikipedia.org/wiki/Spain" |
| 321 | + } |
| 322 | + serializer = FlatCountryTranslatedSerializer(self.instance) |
| 323 | + self.assertDictEqual(serializer.data, expected_en) |
| 324 | + |
| 325 | + def test_es_translations_serialization(self): |
| 326 | + self.instance.set_current_language('es') |
| 327 | + expected_es = { |
| 328 | + 'pk': self.instance.pk, |
| 329 | + 'country_code': 'ES', |
| 330 | + 'language_code': 'es', |
| 331 | + 'name': "España", |
| 332 | + 'url': "http://es.wikipedia.org/wiki/España" |
| 333 | + } |
| 334 | + serializer = FlatCountryTranslatedSerializer(self.instance) |
| 335 | + self.assertDictEqual(serializer.data, expected_es) |
| 336 | + |
| 337 | + def test_translations_serialization_no_language_code(self): |
| 338 | + self.instance.set_current_language('es') |
| 339 | + serializer = FlatCountryNoLanguageCodeTranslatedSerializer(self.instance) |
| 340 | + expected = { |
| 341 | + 'pk': self.instance.pk, |
| 342 | + 'country_code': 'ES', |
| 343 | + 'name': "España", |
| 344 | + 'url': "http://es.wikipedia.org/wiki/España" |
| 345 | + } |
| 346 | + self.assertDictEqual(serializer.data, expected) |
| 347 | + |
| 348 | + def test_language_code_validation(self): |
| 349 | + data = { |
| 350 | + 'country_code': 'es', |
| 351 | + 'language_code': 'es', |
| 352 | + 'name': 'España', |
| 353 | + 'url': "http://es.wikipedia.org/wiki/España" |
| 354 | + } |
| 355 | + serializer = FlatCountryTranslatedSerializer(data=data) |
| 356 | + self.assertTrue(serializer.is_valid(), serializer.errors) |
| 357 | + |
| 358 | + def test_language_code_invalid(self): |
| 359 | + data = { |
| 360 | + 'country_code': 'es', |
| 361 | + 'language_code': 'fr', |
| 362 | + 'name': 'Espagne', |
| 363 | + 'url': "http://fr.wikipedia.org/wiki/Espagne" |
| 364 | + } |
| 365 | + serializer = FlatCountryTranslatedSerializer(data=data) |
| 366 | + self.assertFalse(serializer.is_valid()) |
| 367 | + self.assertIn('language_code', serializer.errors) |
| 368 | + self.assertEqual(serializer.errors['language_code'][0], '"fr" is not a valid choice.') |
| 369 | + |
| 370 | + def test_translated_fields_validation(self): |
| 371 | + data = { |
| 372 | + 'country_code': 'FR', |
| 373 | + 'language_code': 'en', |
| 374 | + 'url': "es.wikipedia.org/wiki/Francia" |
| 375 | + } |
| 376 | + serializer = FlatCountryTranslatedSerializer(data=data) |
| 377 | + self.assertFalse(serializer.is_valid()) |
| 378 | + self.assertIn('name', serializer.errors) |
| 379 | + self.assertEqual(serializer.errors['name'][0], 'This field is required.') |
| 380 | + self.assertIn('url', serializer.errors) |
| 381 | + self.assertEqual(serializer.errors['url'][0], 'Enter a valid URL.') |
| 382 | + |
| 383 | + def test_translation_saving_on_create(self): |
| 384 | + data = { |
| 385 | + 'country_code': 'FR', |
| 386 | + 'language_code': 'es', |
| 387 | + 'name': "Francia", |
| 388 | + 'url': "http://es.wikipedia.org/wiki/Francia" |
| 389 | + } |
| 390 | + serializer = FlatCountryTranslatedSerializer(data=data) |
| 391 | + self.assertTrue(serializer.is_valid(), serializer.errors) |
| 392 | + instance = serializer.save() |
| 393 | + instance = Country.objects.get(pk=instance.pk) |
| 394 | + instance.set_current_language('es') |
| 395 | + self.assertEqual(instance.name, "Francia") |
| 396 | + self.assertEqual(instance.url, "http://es.wikipedia.org/wiki/Francia") |
| 397 | + |
| 398 | + def test_translation_saving_on_update(self): |
| 399 | + data = { |
| 400 | + 'country_code': 'E', |
| 401 | + 'language_code': 'es', |
| 402 | + 'name': "Hispania", |
| 403 | + 'url': "http://es.wikipedia.org/wiki/Hispania" |
| 404 | + } |
| 405 | + serializer = FlatCountryTranslatedSerializer(self.instance, data=data) |
| 406 | + self.assertTrue(serializer.is_valid(), serializer.errors) |
| 407 | + instance = serializer.save() |
| 408 | + instance = Country.objects.get(pk=instance.pk) |
| 409 | + self.assertEqual(instance.country_code, 'E') # also check if shared model is updated |
| 410 | + |
| 411 | + instance.set_current_language('es') |
| 412 | + self.assertEqual(instance.name, "Hispania") |
| 413 | + self.assertEqual(instance.url, "http://es.wikipedia.org/wiki/Hispania") |
| 414 | + |
| 415 | + def test_translations_saving_on_update_with_new_translation(self): |
| 416 | + data = { |
| 417 | + 'country_code': 'ES', |
| 418 | + 'language_code': 'fr', |
| 419 | + 'name': "Espagne", |
| 420 | + 'url': "http://fr.wikipedia.org/wiki/Espagne" |
| 421 | + } |
| 422 | + # Language choices are automatically verified against settings, |
| 423 | + # thus using a serializer with explicitly set language choices |
| 424 | + serializer = FlatCountryExplicitLangTranslatedSerializer(self.instance, data=data) |
| 425 | + self.assertTrue(serializer.is_valid(), serializer.errors) |
| 426 | + instance = serializer.save() |
| 427 | + instance = Country.objects.get(pk=instance.pk) |
| 428 | + instance.set_current_language('fr') |
| 429 | + self.assertEqual(instance.name, "Espagne") |
| 430 | + self.assertEqual(instance.url, "http://fr.wikipedia.org/wiki/Espagne") |
| 431 | + |
| 432 | + def test_translation_saving_on_create_no_language_code(self): |
| 433 | + data = { |
| 434 | + 'country_code': 'FR', |
| 435 | + 'name': "French", |
| 436 | + 'url': "http://en.wikipedia.org/wiki/French" |
| 437 | + } |
| 438 | + serializer = FlatCountryNoLanguageCodeTranslatedSerializer(data=data) |
| 439 | + self.assertTrue(serializer.is_valid(), serializer.errors) |
| 440 | + instance = serializer.save() |
| 441 | + instance = Country.objects.get(pk=instance.pk) |
| 442 | + instance.set_current_language('en') # The fallback language, set via get_language |
| 443 | + self.assertEqual(instance.name, "French") |
| 444 | + self.assertEqual(instance.url, "http://en.wikipedia.org/wiki/French") |
| 445 | + |
| 446 | + def test_nested__translatedserializer(self): |
| 447 | + data = { |
| 448 | + "continent": "Europe", |
| 449 | + "countries": [{ |
| 450 | + 'country_code': 'FR', |
| 451 | + 'language_code': 'en', |
| 452 | + 'name': "France", |
| 453 | + 'url': "http://en.wikipedia.org/wiki/France" |
| 454 | + }] |
| 455 | + } |
| 456 | + serializer = FlatContinentCountriesTranslatedSerializer(data=data) |
| 457 | + self.assertTrue(serializer.is_valid(), serializer.errors) |
| 458 | + nested_data = serializer.validated_data['countries'][0] |
| 459 | + expected = data['countries'][0] |
| 460 | + self.assertDictEqual(nested_data, expected) |
0 commit comments