Django在夹具拆卸中测试IntegrityError

bdo*_*leu 10 python django factory-boy

我开始使用这个factory_boy包,所以我建立了一些工厂,并想测试创建的对象不会引发任何验证错误。

这是我正在使用的 mixin,它基本上从一个模块中获取每个工厂,创建一个实例,然后测试从.full_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))
Run Code Online (Sandbox Code Playgroud)

mixin 将像这样使用

from django.test import TestCase
from order import factories

class OrderFactoriesTestCase(FactoriesTestCaseMixin, TestCase):
    module = factories
Run Code Online (Sandbox Code Playgroud)

但是IntegrityError在测试成功通过有关夹具拆卸的测试后,我不断收到(下面的回溯),我不知道如何解决它,因此我的测试通过而没有错误。

如果我为每个单独的应用程序运行测试,则没有错误。我的任何其他模型的固定装置也有一个created_by场,我从来没有遇到过问题。

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".
Run Code Online (Sandbox Code Playgroud)

我认为正在发生的事情是之前的测试正在创建一个新用户,而工厂男孩Iterator正在选择一个新用户 ID.. 仍然不确定为什么在成功通过测试后会导致错误。

created_by = factory.Iterator(User.objects.all())
Run Code Online (Sandbox Code Playgroud)

导致此问题,总是有一个模块SubFactoryProductFactory

product = factory.SubFactory(ProductFactory)
Run Code Online (Sandbox Code Playgroud)

有关如何解决此问题的任何建议?

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".
Run Code Online (Sandbox Code Playgroud)

Pau*_*ipp 1

我有一个相关的问题,我通过专门研究_fixture_teardown我的测试用例类来解决这个问题。

_fixture_teardown是通过django.test.testcases调用 django 命令“flush”来实现的,该命令尝试从数据库中删除所有数据。我不知道这对你是否有用。在我保留测试数据库并使用的场景中--keepdb,它导致了问题。

由于我不需要(或不想)刷新测试数据库,因此我只是专门设计了不执行任何操作的方法。这解决了我的问题,也可能有助于解决您的问题。