#31074: Errors model abstract testing
-------------------------------------+-------------------------------------
Reporter: Artem | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:
Keywords: test, | Triage Stage:
ManyToManyField, abstract, model | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Artem:
Old description:
> I have models:
> {{{
> class Country(models.Model):
> name = models.CharField(_("Country name"), max_length=50)
> code = models.SlugField(_("Country iso-2 code"), max_length=2,
> unique=True)
> }}}
>
> {{{
> class CountryModelMixin(models.Model):
> countries_admin = models.ManyToManyField(
> Country,
> verbose_name="Страны",
> related_name="%(app_label)s_%(class)s_related",
> related_query_name="%(app_label)s_%(class)ss",
> )
> countries = ArrayField(
> models.SlugField(verbose_name=_("Country iso-2 code"),
> max_length=2),
> verbose_name=_("Countries"),
> blank=True, null=True,
> )
>
> class Meta(object):
> abstract = True
>
> @staticmethod
> def update_hidden(sender, instance, **kwargs):
> if issubclass(instance.__class__, CountryModelMixin):
> instance.countries =
> countries.countries_admin.values_list('code', flat=True)
>
> m2m_changed.connect(CountryModelMixin.update_hidden)
> }}}
>
> {{{
> class Orgaization(CountryModelMixin):
> name = models.CharField(_("Name"), max_length=127)
> slug = models.SlugField(_("Slug"), max_length=127, unique=True)
> active = models.BooleanField(_("Active"), default=True)
>
> address = models.CharField(_("Address"), max_length=256, null=True,
> blank=True)
> }}}
>
> And I need testing CountryModelMixin models:
> I create test:
>
> {{{
> class ModelMixinTestCase(TestCase):
> mixin= CountryModelMixin
>
> @classmethod
> def setUpClass(cls):
> if not cls.mixin:
> return
> if isinstance(cls.mixin, list):
> name = ""
> for mxn in cls.mixin:
> name += mxn.__name__
> name = hashlib.md5(name.encode('UTF-8')).hexdigest()
> cls.model = ModelBase('__TestModel__' + name,
> tuple(cls.mixin),
> {'__module__':
> cls.mixin[0].__module__})
> else:
> cls.model = ModelBase('__TestModel__' +
> cls.mixin.__name__, (cls.mixin,),
> {'__module__': cls.mixin.__module__}
> )
>
> # Create the schema for our test model
> with connection.schema_editor() as schema_editor:
> schema_editor.create_model(cls.model)
> super(ModelMixinTestCase, cls).setUpClass()
>
> @classmethod
> def tearDownClass(cls):
> if not cls.mixin:
> return
> # Delete the schema for the test model
> with connection.schema_editor() as schema_editor:
> schema_editor.delete_model(cls.model)
> super(ModelMixinTestCase, cls).tearDownClass()
>
> def setUp(self):
> self.country = Country.objects.create(name="Test",
> code="EN")
>
> def test_go(self):
> myModelElement = self.model.objects.create()
> myModelElement.countries_admin.set([self.country.pk, ])
> }}}
>
> Test return errors: Cannot resolve keyword '' into field.
> And delete myModelElement.delete() - don't work
>
> Test abstract models without ManyToManyField work goods.
New description:
I have models:
{{{
class Country(models.Model):
name = models.CharField(_("Country name"), max_length=50)
code = models.SlugField(_("Country iso-2 code"), max_length=2,
unique=True)
}}}
{{{
class CountryModelMixin(models.Model):
countries_admin = models.ManyToManyField(
Country,
verbose_name="Страны",
related_name="%(app_label)s_%(class)s_related",
related_query_name="%(app_label)s_%(class)ss",
)
countries = ArrayField(
models.SlugField(verbose_name=_("Country iso-2 code"),
max_length=2),
verbose_name=_("Countries"),
blank=True, null=True,
)
class Meta(object):
abstract = True
@staticmethod
def update_hidden(sender, instance, **kwargs):
if issubclass(instance.__class__, CountryModelMixin):
instance.countries =
countries.countries_admin.values_list('code', flat=True)
m2m_changed.connect(CountryModelMixin.update_hidden)
}}}
{{{
class Orgaization(CountryModelMixin):
name = models.CharField(_("Name"), max_length=127)
slug = models.SlugField(_("Slug"), max_length=127, unique=True)
active = models.BooleanField(_("Active"), default=True)
address = models.CharField(_("Address"), max_length=256, null=True,
blank=True)
}}}
And I need testing CountryModelMixin models:
I create test:
{{{
class ModelMixinTestCase(TestCase):
mixin= CountryModelMixin
@classmethod
def setUpClass(cls):
if not cls.mixin:
return
if isinstance(cls.mixin, list):
name = ""
for mxn in cls.mixin:
name += mxn.__name__
name = hashlib.md5(name.encode('UTF-8')).hexdigest()
cls.model = ModelBase('__TestModel__' + name,
tuple(cls.mixin),
{'__module__': cls.mixin[0].__module__})
else:
cls.model = ModelBase('__TestModel__' +
cls.mixin.__name__, (cls.mixin,),
{'__module__': cls.mixin.__module__}
)
# Create the schema for our test model
with connection.schema_editor() as schema_editor:
schema_editor.create_model(cls.model)
super(ModelMixinTestCase, cls).setUpClass()
@classmethod
def tearDownClass(cls):
if not cls.mixin:
return
# Delete the schema for the test model
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(cls.model)
super(ModelMixinTestCase, cls).tearDownClass()
def setUp(self):
self.country = Country.objects.create(name="Test",
code="EN")
def test_go(self):
myModelElement = self.model.objects.create()
myModelElement.countries_admin.set([self.country.pk, ])
}}}
return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
AttributeError: '__TestModel__35ecfa746aba30e3344f3534dd7552ac' object has
no attribute '_prefetched_objects_cache'
Test return errors: Cannot resolve keyword "" into field.
And delete myModelElement.delete() - don't work
Test abstract models without ManyToManyField work goods.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/31074#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/063.598a283b093bb3b9bf248a03a0f82742%40djangoproject.com.