mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-05-17 17:21:05 +00:00
Test registration and translation fields inheritance with a more complex model hierarchy (inspired by Mezzanine).
This commit is contained in:
parent
772d51c1c3
commit
36c5d0b81a
3 changed files with 122 additions and 6 deletions
|
|
@ -33,7 +33,7 @@ from modeltranslation.tests.translation import (FallbackModel2TranslationOptions
|
|||
FieldInheritanceCTranslationOptions,
|
||||
FieldInheritanceETranslationOptions)
|
||||
from modeltranslation.tests.test_settings import TEST_SETTINGS
|
||||
from modeltranslation.utils import build_css_class
|
||||
from modeltranslation.utils import build_css_class, build_localized_fieldname
|
||||
|
||||
try:
|
||||
from django.test.utils import override_settings
|
||||
|
|
@ -227,16 +227,24 @@ class ModeltranslationTest(ModeltranslationTestBase):
|
|||
self.failUnless(translator.translator)
|
||||
|
||||
# Check that all models are registered for translation
|
||||
self.failUnlessEqual(len(translator.translator._registry), 13)
|
||||
self.assertEqual(len(translator.translator.get_registered_models()), 19)
|
||||
|
||||
# Try to unregister a model that is not registered
|
||||
self.assertRaises(translator.NotRegistered,
|
||||
translator.translator.unregister, User)
|
||||
translator.translator.unregister, models.BasePage)
|
||||
|
||||
# Try to get options for a model that is not registered
|
||||
self.assertRaises(translator.NotRegistered,
|
||||
translator.translator.get_options_for_model, User)
|
||||
|
||||
# Ensure that a base can't be registered after a subclass.
|
||||
self.assertRaises(translator.DescendantRegistered,
|
||||
translator.translator.register, models.BasePage)
|
||||
|
||||
# Or unregistered before it.
|
||||
self.assertRaises(translator.DescendantRegistered,
|
||||
translator.translator.unregister, models.Slugged)
|
||||
|
||||
def test_fields(self):
|
||||
field_names = dir(models.TestModel())
|
||||
self.failUnless('id' in field_names)
|
||||
|
|
@ -400,11 +408,11 @@ class ModeltranslationTest(ModeltranslationTestBase):
|
|||
def _test_constructor(self, keywords):
|
||||
n = models.TestModel(**keywords)
|
||||
m = models.TestModel.objects.create(**keywords)
|
||||
fields = translator.translator.get_options_for_model(models.TestModel).localized_fieldnames
|
||||
for base_field, trans_fields in fields.iteritems():
|
||||
opts = translator.translator.get_options_for_model(models.TestModel)
|
||||
for base_field, trans_fields in opts.fields.iteritems():
|
||||
self._compare_instances(n, m, base_field)
|
||||
for lang_field in trans_fields:
|
||||
self._compare_instances(n, m, lang_field)
|
||||
self._compare_instances(n, m, lang_field.name)
|
||||
|
||||
def test_constructor(self):
|
||||
"""
|
||||
|
|
@ -1213,6 +1221,45 @@ class ModelInheritanceTest(ModeltranslationTestBase):
|
|||
self.failUnless('titleb_en' in field_names_d)
|
||||
self.failUnless('titled' in field_names_d)
|
||||
|
||||
def test_inheritance(self):
|
||||
def assertLocalFields(model, local_fields):
|
||||
# Proper fields are inherited.
|
||||
opts = translator.translator.get_options_for_model(model)
|
||||
self.assertEqual(set(opts.local_fields.keys()), set(local_fields))
|
||||
# Local translation fields are created on the model.
|
||||
model_local_fields = [f.name for f in model._meta.local_fields]
|
||||
for field in local_fields:
|
||||
for lang in mt_settings.AVAILABLE_LANGUAGES:
|
||||
translation_field = build_localized_fieldname(field, lang)
|
||||
self.assertTrue(translation_field in model_local_fields)
|
||||
|
||||
def assertFields(model, fields):
|
||||
# The given fields are inherited.
|
||||
opts = translator.translator.get_options_for_model(model)
|
||||
self.assertEqual(set(opts.fields.keys()), set(fields))
|
||||
# Inherited translation fields are available on the model.
|
||||
model_fields = model._meta.get_all_field_names()
|
||||
for field in fields:
|
||||
for lang in mt_settings.AVAILABLE_LANGUAGES:
|
||||
translation_field = build_localized_fieldname(field, lang)
|
||||
self.assertTrue(translation_field in model_fields)
|
||||
|
||||
# Translation fields can be declared on abstract classes.
|
||||
assertLocalFields(models.Slugged, ('slug',))
|
||||
assertLocalFields(models.MetaData, ('keywords',))
|
||||
assertLocalFields(models.RichText, ('content',))
|
||||
# Local fields are inherited from abstract superclasses.
|
||||
assertLocalFields(models.Displayable, ('slug', 'keywords',))
|
||||
assertLocalFields(models.Page, ('slug', 'keywords', 'title',))
|
||||
# But not from concrete superclasses.
|
||||
assertLocalFields(models.RichTextPage, ('content',))
|
||||
|
||||
# Fields inherited from concrete models are also available.
|
||||
assertFields(models.Slugged, ('slug',))
|
||||
assertFields(models.Page, ('slug', 'keywords', 'title',))
|
||||
assertFields(models.RichTextPage, ('slug', 'keywords', 'title',
|
||||
'content',))
|
||||
|
||||
|
||||
class ModelInheritanceFieldAggregationTest(ModeltranslationTestBase):
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -87,6 +87,47 @@ class AbstractModelB(AbstractModelA):
|
|||
titleb = models.CharField(ugettext_lazy('title b'), max_length=255)
|
||||
|
||||
|
||||
########## Fields inheritance testing
|
||||
|
||||
class Slugged(models.Model):
|
||||
slug = models.CharField(max_length=255)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class MetaData(models.Model):
|
||||
keywords = models.CharField(max_length=255)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class Displayable(Slugged, MetaData):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class BasePage(Displayable):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class Page(BasePage):
|
||||
title = models.CharField(max_length=255)
|
||||
|
||||
|
||||
class RichText(models.Model):
|
||||
content = models.CharField(max_length=255)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class RichTextPage(Page, RichText):
|
||||
pass
|
||||
|
||||
|
||||
########## Admin testing
|
||||
|
||||
class DataModel(models.Model):
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ from modeltranslation.translator import translator, TranslationOptions
|
|||
from modeltranslation.tests.models import (
|
||||
TestModel, FallbackModel, FallbackModel2,
|
||||
FileFieldsModel, OtherFieldsModel, AbstractModelA, AbstractModelB,
|
||||
Slugged, MetaData, Displayable, BasePage, Page, RichText, RichTextPage,
|
||||
MultitableModelA, MultitableBModelA, MultitableModelC,
|
||||
ManagerTestModel, CustomManagerTestModel, CustomManager2TestModel)
|
||||
|
||||
|
|
@ -75,6 +76,33 @@ class AbstractModelBTranslationOptions(TranslationOptions):
|
|||
translator.register(AbstractModelB, AbstractModelBTranslationOptions)
|
||||
|
||||
|
||||
########## Fields inheritance testing
|
||||
|
||||
class SluggedTranslationOptions(TranslationOptions):
|
||||
fields = ('slug',)
|
||||
|
||||
|
||||
class MetaDataTranslationOptions(TranslationOptions):
|
||||
fields = ('keywords',)
|
||||
|
||||
|
||||
class RichTextTranslationOptions(TranslationOptions):
|
||||
fields = ('content',)
|
||||
|
||||
|
||||
class PageTranslationOptions(TranslationOptions):
|
||||
fields = ('title',)
|
||||
|
||||
|
||||
# BasePage left unregistered intentionally.
|
||||
translator.register(Slugged, SluggedTranslationOptions)
|
||||
translator.register(MetaData, MetaDataTranslationOptions)
|
||||
translator.register(RichText, RichTextTranslationOptions)
|
||||
translator.register(Displayable)
|
||||
translator.register(Page, PageTranslationOptions)
|
||||
translator.register(RichTextPage)
|
||||
|
||||
|
||||
########## Manager testing
|
||||
|
||||
class ManagerTestModelTranslationOptions(TranslationOptions):
|
||||
|
|
|
|||
Loading…
Reference in a new issue