Opened 4 years ago

Closed 4 years ago

#31074 closed Uncategorized (invalid)

Errors model abstract testing.

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 Artem)

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: 'TestModel35ecfa746aba30e3344f3534dd7552ac' 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.

Change History (3)

comment:1 by Artem, 4 years ago

Description: modified (diff)

comment:2 by Artem, 4 years ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 4 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: invalid
Status: newclosed
Summary: Errors model abstract testingErrors model abstract testing.

As far as I'm concerned this is not an issue in Django. IMO you should use Model instead of BaseModel, nevertheless please don't use Trac as a support channel.

Closing per TicketClosingReasons/UseSupportChannels.

Note: See TracTickets for help on using tickets.
Back to Top