Opened 6 years ago
Last modified 6 years ago
#31074 closed Uncategorized
Errors model abstract testing — at Version 1
| Reported by: | Artem | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 3.0 |
| Severity: | Normal | Keywords: | test, ManyToManyField, abstract, model |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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.
Note:
See TracTickets
for help on using tickets.