Example #1
0
 def test_create_instance_shared_nolang(self):
     with LanguageOverride('en'):
         obj = Normal(language_code='en', shared_field="shared")
         obj.save()
         en = Normal.objects.language('en').get(pk=obj.pk)
         self.assertEqual(en.shared_field, "shared")
         self.assertEqual(en.language_code, "en")
Example #2
0
 def test_create_instance_shared_nolang(self):
     with translation.override('en'):
         obj = Normal(language_code='en', shared_field = "shared")
         obj.save()
         en = Normal.objects.language('en').get(pk=obj.pk)
         self.assertEqual(en.shared_field, "shared")
         self.assertEqual(en.language_code, "en")
Example #3
0
 def test_create_instance_shared_nolang(self):
     with LanguageOverride("en"):
         obj = Normal(language_code="en", shared_field="shared")
         obj.save()
         en = Normal.objects.language("en").get(pk=obj.pk)
         self.assertEqual(en.shared_field, "shared")
         self.assertEqual(en.language_code, "en")
Example #4
0
 def test_create_instance_translated_nolang(self):
     with LanguageOverride('en'):
         obj = Normal(language_code='en', translated_field = "English")
         obj.save()
         en = Normal.objects.language('en').get(pk=obj.pk)
         self.assertEqual(en.translated_field, "English")
         self.assertEqual(en.language_code, "en")
Example #5
0
 def test_create_instance_translated_nolang(self):
     with translation.override('en'):
         obj = Normal(language_code='en', translated_field="English")
         obj.save()
         en = Normal.objects.language('en').get(pk=obj.pk)
         self.assertEqual(en.translated_field, "English")
         self.assertEqual(en.language_code, "en")
Example #6
0
 def test_create_instance_both(self):
     obj = Normal(language_code="en", shared_field="shared", translated_field="English")
     obj.save()
     en = Normal.objects.language("en").get(pk=obj.pk)
     self.assertEqual(en.shared_field, "shared")
     self.assertEqual(en.translated_field, "English")
     self.assertEqual(en.language_code, "en")
Example #7
0
 def test_create_instance_both(self):
     obj = Normal(language_code='en', shared_field = "shared",
                  translated_field = "English")
     obj.save()
     en = Normal.objects.language('en').get(pk=obj.pk)
     self.assertEqual(en.shared_field, "shared")
     self.assertEqual(en.translated_field, "English")
     self.assertEqual(en.language_code, "en")
Example #8
0
 def test_create_instance_simple(self):
     obj = Normal(language_code='en')
     obj.shared_field = "shared"
     obj.translated_field = "English"
     obj.save()
     en = Normal.objects.language('en').get(pk=obj.pk)
     self.assertEqual(en.shared_field, "shared")
     self.assertEqual(en.translated_field, "English")
     self.assertEqual(en.language_code, "en")
    def test_translated_new_object_can_be_pickled(self):
        normal = Normal(shared_field="Shared")
        normal.translate("en")
        normal.translated_field = "English"
        serialized_repr = pickle.dumps(normal)

        unpickled = pickle.loads(serialized_repr)
        self.assertEqual(normal.shared_field, unpickled.shared_field)
        self.assertEqual(normal.language_code, unpickled.language_code)
        self.assertEqual(normal.translated_field, unpickled.translated_field)
Example #10
0
    def test_translated_new_object_can_be_pickled(self):
        normal = Normal(shared_field="Shared")
        normal.translate("en")
        normal.translated_field = "English"
        serialized_repr = pickle.dumps(normal)

        unpickled = pickle.loads(serialized_repr)
        self.assertEqual(normal.shared_field, unpickled.shared_field)
        self.assertEqual(normal.language_code, unpickled.language_code)
        self.assertEqual(normal.translated_field, unpickled.translated_field)
Example #11
0
    def test_translated_foreignkey_set(self):
        cache = Related._meta.translations_cache

        normal = Normal(language_code='en')
        normal.save()
        related = Related(language_code='en')
        related.translated = normal
        self.assertNotIn('translated_id', related.__dict__)
        self.assertIn('translated_id', getattr(related, cache).__dict__)
        self.assertEqual(getattr(related, cache).__dict__['translated_id'], normal.pk)

        related.translated_id = 4242
        self.assertNotIn('translated_id', related.__dict__)
        self.assertIn('translated_id', getattr(related, cache).__dict__)
        self.assertEqual(getattr(related, cache).__dict__['translated_id'], 4242)
Example #12
0
    def test_create_not_enforcing(self):
        'Calling save on a new instance with no language_code in cleaned_data'
        data = {
            'shared_field': 'shared',
            'translated_field': u'српски',
        }
        # no instance, should use current language
        with translation.override('sr'):
            form = NormalForm(data)
            with self.assertNumQueries(2):
                obj = form.save()
            with self.assertNumQueries(0):
                self.assertNotEqual(obj.pk, None)
                self.assertEqual(obj.language_code, 'sr')
                self.assertEqual(obj.shared_field, 'shared')
                self.assertEqual(obj.translated_field, u'српски')

        # an instance with a translation loaded, should use that
        with translation.override('en'):
            form = NormalForm(data, instance=Normal(language_code='sr'))
            with self.assertNumQueries(2):
                obj = form.save()
            with self.assertNumQueries(0):
                self.assertNotEqual(obj.pk, None)
                self.assertEqual(obj.language_code, 'sr')
                self.assertEqual(obj.shared_field, 'shared')
                self.assertEqual(obj.translated_field, u'српски')
Example #13
0
    def test_all_translations(self):
        # Create an unstranslated model and get the translations
        myadmin = self._get_admin(Normal)

        obj = Normal.objects.untranslated().create(shared_field="shared")
        self.assertEqual(myadmin.all_translations(obj), "")

        # Create a english translated model and make sure the active language
        # is highlighted in admin with <strong></strong>
        obj = Normal.objects.language("en").get(pk=self.normal_id[1])
        with translation.override('en'):
            # make sure no the call will not generate a spurious query in assertNumQueries
            ContentType.objects.get_for_model(Normal)
            with self.assertNumQueries(1):
                self.assertTrue(myadmin.all_translations(obj).find("<strong>") != -1)
            with self.assertNumQueries(1):
                # Entries should be linked to the corresponding translation page
                self.assertTrue(myadmin.all_translations(obj).find("?language=en") != -1)

        with translation.override('th'):
            with self.assertNumQueries(1):
                self.assertTrue(myadmin.all_translations(obj).find("<strong>") == -1)

        # An unsaved object, shouldn't have any translations
        obj = Normal()
        self.assertEqual(myadmin.all_translations(obj), "")
Example #14
0
 def create_normal(self, data, translations=None):
     obj = Normal(shared_field=data.shared_field)
     for code in translations or self.translations:
         obj.translate(code)
         obj.translated_field = data.translated_field[code]
         obj.save()
     return obj
Example #15
0
 def test_filtered_get(self):
     obj = Normal(shared_field='field_1')
     obj.translate('en')
     obj.translated_field = 'field_2'
     obj.save()
     qs = Normal.objects.language('en') | Normal.objects.language('de')
     found = qs.filter(shared_field='field_1').get(pk=obj.pk)
     self.assertEqual(found.pk, obj.pk)
Example #16
0
 def test_normal_model_form_initaldata_instance(self):
     # Check if it accepts inital data and instance
     SHARED = 'Shared'
     TRANSLATED = 'English'
     data = {
         'shared_field': SHARED,
         'translated_field': TRANSLATED,
         'language_code': 'en'
     }
     form = NormalForm(data, instance=Normal(), initial=data)
     self.assertTrue(form.is_valid(), form.errors.as_text())
Example #17
0
 def create_normal(self, data, translations=None):
     obj = Normal(shared_field=data.shared_field)
     for code in translations or self.translations:
         obj.translate(code)
         obj.translated_field = data.translated_field[code]
         obj.save()
     return obj
Example #18
0
 def test_filtered_get(self):
     obj = Normal(shared_field="field_1")
     obj.translate("en")
     obj.translated_field = "field_2"
     obj.save()
     qs = Normal.objects.language("en") | Normal.objects.language("de")
     found = qs.filter(shared_field="field_1").get(pk=obj.pk)
     self.assertEqual(found.pk, obj.pk)
Example #19
0
 def test_filtered_get(self):
     obj = Normal(shared_field='field_1')
     obj.translate('en')
     obj.translated_field = 'field_2'
     obj.save()
     qs = Normal.objects.language('en') | Normal.objects.language('de')
     found = qs.filter(shared_field='field_1').get(pk=obj.pk)
     self.assertEqual(found.pk, obj.pk)
Example #20
0
 def test_create_enforcing(self):
     'Calling save() on a new instance with a language_code in cleaned_data'
     Form = translatable_modelform_factory('ja', Normal, form=NormalForm)
     data = {
         'shared_field': 'shared',
         'translated_field': 'Japanese',
     }
     with translation.override('en'):
         form = Form(data, instance=Normal(language_code='sr'))
         with self.assertNumQueries(2):
             obj = form.save()
         with self.assertNumQueries(0):
             self.assertNotEqual(obj.pk, None)
             self.assertEqual(obj.language_code, 'ja')
             self.assertEqual(obj.shared_field, 'shared')
             self.assertEqual(obj.translated_field, 'Japanese')
Example #21
0
 def test_normal_model_form_instantiation(self):
     # Basic example and checking it gives us all the fields needed
     form = NormalForm()
     self.assertTrue("translated_field" in form.fields)
     self.assertTrue("shared_field" in form.fields)
     self.assertTrue("translated_field" in form.base_fields)
     self.assertTrue("shared_field" in form.base_fields)
     self.assertFalse(form.is_valid())
     
     # Check if it works with media argument too
     form = NormalMediaForm()
     self.assertFalse(form.is_valid())
     self.assertTrue("layout.css" in str(form.media))
     
     # Check if it works with an instance of Normal
     form = NormalForm(instance=Normal())
     self.assertFalse(form.is_valid())
Example #22
0
    def test_all_translations(self):
        # Create an unstranslated model and get the translations
        myadmin = self._get_admin(Normal)
        obj = Normal.objects.untranslated().create(shared_field="shared", )
        self.assertEqual(myadmin.all_translations(obj), "")

        # Create a english translated model and make sure the active language
        # is highlighted in admin with <strong></strong>
        obj = Normal.objects.language("en").create(shared_field="shared", )
        with LanguageOverride('en'):
            self.assertEqual(myadmin.all_translations(obj),
                             "<strong>en</strong>")

        with LanguageOverride('ja'):
            self.assertEqual(myadmin.all_translations(obj), "en")

        # An unsaved object, shouldnt have any translations

        obj = Normal()
        self.assertEqual(myadmin.all_translations(obj), "")
Example #23
0
 def test_languagecodeattribute(self):
     # Its not possible to set/delete a language code
     self.assertRaises(AttributeError, setattr, Normal(), 'language_code',
                       "en")
     self.assertRaises(AttributeError, delattr, Normal(), 'language_code')
Example #24
0
 def test_internal_properties(self):
     self.assertCountEqual(
         Normal()._translated_field_names,
         ['id', 'master', 'master_id', 'language_code', 'translated_field'])
Example #25
0
    def test_untranslated_new_object_can_be_pickled(self):
        normal = Normal(shared_field="Shared")
        serialized_repr = pickle.dumps(normal)

        unpickled = pickle.loads(serialized_repr)
        self.assertEqual(normal.shared_field, unpickled.shared_field)