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")
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")
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")
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")
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_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")
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)
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())
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())
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), "")
def test_create_instance_shared(self): 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")
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')
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)
def test_minus_order_by(self): with LanguageOverride("en"): obj = Normal(language_code='en', translated_field = "English", shared_field="lol") obj.save() self.assertEqual(Normal.objects.order_by('-shared_field').count(), 1)