Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django测试夹具拆卸中的完整性错误_Python_Django_Factory Boy - Fatal编程技术网

Python Django测试夹具拆卸中的完整性错误

Python Django测试夹具拆卸中的完整性错误,python,django,factory-boy,Python,Django,Factory Boy,我开始使用这个包,所以我建立了一些工厂,希望测试所创建的对象是否不会引起任何验证错误 这里是我使用的mixin,它基本上从一个模块中获取每个工厂,创建一个实例,然后测试.full\u clean()中是否没有错误。加载的用户装置是ID为1到10的10个实例 class FactoriesTestCaseMixin: fixtures = [ 'user/tests/fixtures/user.json', ] module = None def

我开始使用这个包,所以我建立了一些工厂,希望测试所创建的对象是否不会引起任何验证错误

这里是我使用的mixin,它基本上从一个模块中获取每个工厂,创建一个实例,然后测试
.full\u clean()
中是否没有错误。加载的用户装置是ID为1到10的10个实例

class FactoriesTestCaseMixin:
    fixtures = [
        'user/tests/fixtures/user.json',
    ]
    module = None

    def test_factories(self):
        err_msg = '{name} caused errors:\n{errors}'
        factories = [
            (name, obj) for name, obj in inspect.getmembers(self.module, inspect.isclass)
            if obj.__module__ == self.module.__name__
            and not obj._meta.abstract
        ]
        for factory in factories:
            name = factory[0]
            instance = factory[1]()

            errors = None
            try:
                instance.full_clean()
            except ValidationError as e:
                errors = e

            self.assertTrue(errors is None, err_msg.format(name=name, errors=errors))
混合器会像这样使用

from django.test import TestCase
from order import factories

class OrderFactoriesTestCase(FactoriesTestCaseMixin, TestCase):
    module = factories
但是在夹具拆卸测试成功通过后,我不断得到一个
IntegrityError
(下面是回溯),我不知道如何绕过它,所以我的测试通过时没有错误

如果我为每个应用程序运行测试,则不会出现错误。我的其他型号的装置中也有一个由创建的
字段,我从未遇到过任何问题

django.db.utils.IntegrityError: insert or update on table "product_product" violates foreign key constraint "product_product_created_by_id_96713f93_fk_user_user_id"
DETAIL:  Key (created_by_id)=(13) is not present in table "user_user".
我认为正在发生的是,以前的测试正在创建一个新用户,而factory boy正在选择一个新用户ID。。仍然不确定这在成功通过测试后为什么会导致错误

created_by = factory.Iterator(User.objects.all())
导致此问题的模块始终与
ProductFactory

product = factory.SubFactory(ProductFactory)
对如何解决这个问题有什么建议吗

Traceback (most recent call last):
  File "/home/Development/project/venv/lib/python3.7/site-packages/django/test/testcases.py", line 274, in __call__
    self._post_teardown()
  File "/home/Development/project/venv/lib/python3.7/site-packages/django/test/testcases.py", line 1009, in _post_teardown
    self._fixture_teardown()
  File "/home/Development/project/venv/lib/python3.7/site-packages/django/test/testcases.py", line 1177, in _fixture_teardown
    connections[db_name].check_constraints()
  File "/home/Development/project/venv/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 246, in check_constraints
    self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
  File "/home/Development/project/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/Development/project/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/Development/project/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/Development/project/venv/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/Development/project/venv/lib/python3.7/site-packages/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
django.db.utils.IntegrityError: insert or update on table "product_product" violates foreign key constraint "product_product_created_by_id_96713f93_fk_user_user_id"
DETAIL:  Key (created_by_id)=(12) is not present in table "user_user".

我有一个相关的问题,我通过在我的测试用例类中专门化
\u fixture\u teardown
解决了这个问题

\u fixture\u teardown
django.test.testcases
中实现,以调用django命令“flush”,尝试从数据库中删除所有数据。我不知道这对你是否有用。在我持久化测试数据库并使用
--keepdb
的场景中,它导致了问题


由于我不需要(或不想)刷新测试数据库,所以我只是将该方法专门化,不做任何事情。这解决了我的问题,也可能有助于解决你的问题。

嗨!你找到解决办法了吗?@tjespe我没有