@V-ampre

Почему не вызывается setUpTestData?

Хочу написать тест для своей абстрактной модели. За пример беру ответ отсюда 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


Запускаю тесты в итоге setUpTestData не запускает и просто получаю ошибку AttributeError: 'TestOnOffStatusModel' object has no attribute 'model'
  • Вопрос задан
  • 191 просмотр
Решения вопроса 1
alternativshik
@alternativshik
так там отступа не там
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы