用工厂男孩测试django的外键不起作用

时间:2017-09-27 06:30:09

标签: python django testing factory-boy

我在图书馆工厂男孩的外键有问题。我的测试没有执行 我认为外键的问题。

我尝试测试user.models中的用户模型,这是我的代码的样子

docker build [PROD] -t mytag .

我用工厂男孩测试它,这是我工厂男生班的样子

class Task(models.Model):
    title = models.CharField(max_length=255, verbose_name='Заголовок')
    description = models.CharField(max_length=255, verbose_name='Описание')
    cost = models.DecimalField(max_digits=7, decimal_places=2, default=0, verbose_name='Цена')
    assignee = models.ForeignKey('users.User', related_name='assignee', null=True, verbose_name='Исполнитель')
    created_by = models.ForeignKey('users.User', related_name='created_by', verbose_name='Кем был создан')

    def __str__(self):
        return self.title

这是我的测试的例子

class UserFactoryCustomer(factory.Factory):

    class Meta:
        model = User

    first_name = 'Ahmed'
    last_name = 'Asadov'
    username = factory.LazyAttribute(lambda o: slugify(o.first_name + '.' + o.last_name))
    email = factory.LazyAttribute(lambda a: '{0}.{1}@example.com'.format(a.first_name, a.last_name).lower())
    user_type = 1
    balance = 10000.00

class UserFactoryExecutor(factory.Factory):

    class Meta:
        model = User

    first_name = 'Uluk'
    last_name = 'Djunusov'
    username = factory.LazyAttribute(lambda o: slugify(o.first_name + '.' + o.last_name))
    email = factory.LazyAttribute(lambda a: '{0}.{1}@example.com'.format(a.first_name, a.last_name).lower())
    user_type = 2
    balance = 5000.00


class TaskFactory(factory.Factory):

    class Meta:
        model = Task

    title = factory.Sequence(lambda n: 'Title {}'.format(n))
    description = factory.Sequence(lambda d: 'Description {}'.format(d))
    cost = 5000.00
    assignee = factory.SubFactory(UserFactoryExecutor)
    created_by = factory.SubFactory(UserFactoryCustomer)

这就是我的错误

class ApiModelTestCase(TestCase):

     def test_creating_models_instance(self):
         executor = factories.UserFactoryExecutor()
         customer = factories.UserFactoryCustomer()
         Task.objects.create(title="Simple Task", description="Simple Description", cost="5000.00",
                            assignee=executor, created_by=customer)

1 个答案:

答案 0 :(得分:2)

该错误表示未保存受理人实例。试试这个

class ApiModelTestCase(TestCase):

     def test_creating_models_instance(self):
         executor = factories.UserFactoryExecutor.create()
         customer = factories.UserFactoryCustomer.create()
         Task.objects.create(title="Simple Task", description="Simple Description", cost="5000.00",
                            assignee=executor, created_by=customer)

更新

  

如果你使用Django模型,你需要使用factory.django.DjangoModelFactory - 否则,factory_boy使用"简单的Python对象"做法。   https://github.com/FactoryBoy/factory_boy/issues/329

所以它应该是

from factory.django import DjangoModelFactory

class UserFactoryCustomer(DjangoModelFactory):

    class Meta:
        model = User

    first_name = 'Ahmed'
    last_name = 'Asadov'
    username = factory.LazyAttribute(lambda o: slugify(o.first_name + '.' + o.last_name))
    email = factory.LazyAttribute(lambda a: '{0}.{1}@example.com'.format(a.first_name, a.last_name).lower())
    user_type = 1
    balance = 10000.00

class UserFactoryExecutor(DjangoModelFactory):

    class Meta:
        model = User

    first_name = 'Uluk'
    last_name = 'Djunusov'
    username = factory.LazyAttribute(lambda o: slugify(o.first_name + '.' + o.last_name))
    email = factory.LazyAttribute(lambda a: '{0}.{1}@example.com'.format(a.first_name, a.last_name).lower())
    user_type = 2
    balance = 5000.00