V
V
Vampre2019-08-31 06:49:42
Django
Vampre, 2019-08-31 06:49:42

Why isn't setUpTestData called?

I want to write a test for my abstract model. For example, I take the answer from here https://stackoverflow.com/questions/4281670/django...
from django.test import TestCase

# tests/mixins.py

class AbstractModelMixinTestCase(TestCase):
    """
    Base class for tests of model mixins/abstract models.
    To use, subclass and specify the mixin class variable.
    A model using the mixin will be made available in self.model
    """

@classmethod
def setUpTestData(cls):
    import pdb; pdb.set_trace()
    # Create a dummy model which extends the mixin. A RuntimeWarning will
    # occur if the model is registered twice
    if not hasattr(cls, 'model'):
        cls.model = ModelBase(
            '__TestModel__' +
            cls.mixin.__name__, (cls.mixin,),
            {'__module__': cls.mixin.__module__}
        )

    # Create the schema for our test model. If the table already exists,
    # will pass
    try:
        with connection.schema_editor() as schema_editor:
            schema_editor.create_model(cls.model)
        super(AbstractModelMixinTestCase, cls).setUpClass()
    except ProgrammingError as ex:
        pass

@classmethod
def tearDownClass(cls):
    # Delete the schema for the test model. If no table, will pass
    try:
        with connection.schema_editor() as schema_editor:
            schema_editor.delete_model(cls.model)
        super(AbstractModelMixinTestCase, cls).tearDownClass()
    except ProgrammingError:
        pass

# tests/test_models.py
from django.db import connection
from django.db.models.base import ModelBase
from django.db.utils import OperationalError
from django.test import TestCase
from .mixins import AbstractModelMixinTestCase
from ..models import OnOffStatusModel

class TestOnOffStatusModel(AbstractModelMixinTestCase):
    
    mixin = OnOffStatusModel

    def setUp(self):
        self.model_on = self.model.objects.create()
        self.model_off = self.model.objects.create(status=self.model.ON)

    def test_correct_status_fiels(self):
        self.assertEqual(model_on.status, self.model.ON)
        self.assertEqual(model_off.status, self.model.OFF)

#models.py
class ObjectsOnManager(models.Manager):
    def get_queryset(self):
        return super(ObjectsOnManager, self).get_queryset().filter(status='on')

class OnOffStatusModel(models.Model):
    ON = 'on'
    OFF = 'off'
    STATUS_CHOICES = (
        (ON, 'Показывать'),
        (OFF, 'Не показывать'),
    )
    status = models.CharField("Статус", max_length=15, choices=STATUS_CHOICES, default=ON)
    objects_on = ObjectsOnManager()

    class Meta:
        abstract = True

I run tests and as a result setUpTestData does not run and I just get an error AttributeError: 'TestOnOffStatusModel' object has no attribute 'model'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2019-08-31
@Vampre

so there is no indent there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question