缺乏内的TestCase ROLLBACK会导致独特contraint违反多分贝Django应用程序

2019-09-22 00:18发布

我刚开始接触的factory_boy Django的库测试工厂,并且具有与重复键约束冲突的问题。

test_member_programme.py

from datetime import date, timedelta

from django.test import TestCase

from app.test.factories import MemberFactory, ProgrammeFactory
from app.models.member_programme import MemberProgramme


class MemberProgrammeTestCase(TestCase):

    def member_programme(self):
        yesterday = date.today() - timedelta(days=1)
        return MemberProgramme.objects.create(
                mem=MemberFactory(),
                prg=ProgrammeFactory(),
                date_registered=yesterday)

    def date_registered_should_be_defined_test(self):
        # This test passes
        memprg = self.member_programme()
        assert hasattr(memprg, 'date_registered')

    def date_registered_should_be_in_past_test(self):
        # This test fails
        memprg = self.member_programme()
        assert memprg.date_registered < date.today()

factories.py

class CountryOfOriginFactory(factory.Factory):
    """ Factory class for app.models.CountryOfOrigin
    """
    FACTORY_FOR = CountryOfOrigin

    code = 'UK'
    the_country = 'United Kingdom'

class MemberFactory(factory.Factory):
    """ Factory class for app.models.Member
    """
    FACTORY_FOR = Member

    first_name = 'Test'
    surname = 'User'
    sex = 'M'
    date_of_birth = datetime.date(1990, 1, 1)
    origin = factory.LazyAttribute(lambda a: CountryOfOriginFactory())

运行时,第一测试成功通过,但第二个失败,出现以下错误:

IntegrityError: duplicate key value violates unique constraint "country_of_origin_code_key"

我的理解是,每一个测试用例应该在一个事务中运行,但外键的创建不会出现在第二次测试运行之前已经回滚。 显然,我在做什么根本性的错误,但我有点难倒! 谢谢!


我已经找到了问题所在,但不幸的是不知道如何解决它。 问题是, 回滚发生,但仅限于一个数据库(这个程序有2个数据库)。 对于遗留原因,我们对Django的权威性,的flatpages等单独的数据库和另一个数据库为我们的应用程序。

dba test_app 127.0.0.1 2012-09-04 21:51:50.806 UTC LOG:  duration: 0.038 ms  statement: BEGIN; SET TRANSACTION ISOLATION LEVEL READ COMMITTED
dba test_app 127.0.0.1 2012-09-04 21:51:50.808 UTC LOG:  duration: 0.903 ms  statement: INSERT INTO "member_programme" ("mem_id", "prgm_id", "date_registered", "date_completed", "ordinality") VALUES (1, 1, E'2012-09-04', NULL, 1)
dba test_app 127.0.0.1 2012-09-04 21:51:50.808 UTC LOG:  duration: 0.150 ms  statement: SELECT CURRVAL(pg_get_serial_sequence('"member_programme"','id'))
dba test_app 127.0.0.1 2012-09-04 21:51:50.810 UTC LOG:  duration: 1.796 ms  statement: COMMIT
dba test_app_django 127.0.0.1 2012-09-04 21:51:50.811 UTC LOG:  duration: 0.056 ms  statement: ROLLBACK <---- ROLLBACK ON DJANGO DB ONLY
dba test_app_django 127.0.0.1 2012-09-04 21:51:50.814 UTC LOG:  disconnection: session time: 0:00:21.005 user=dba database=test_app_django host=127.0.0.1 port=60355
dba test_app 127.0.0.1 2012-09-04 21:51:50.818 UTC LOG:  disconnection: session time: 0:00:04.751 user=dba database=test_app host=127.0.0.1 port=60357
dba test_app 127.0.0.1 2012-09-04 21:54:00.796 UTC LOG:  connection authorized: user=dba database=test_app
dba test_app 127.0.0.1 2012-09-04 21:54:00.802 UTC LOG:  duration: 0.243 ms  statement: SET DATESTYLE TO 'ISO'
dba test_app 127.0.0.1 2012-09-04 21:54:00.802 UTC LOG:  duration: 0.156 ms  statement: SHOW client_encoding
dba test_app 127.0.0.1 2012-09-04 21:54:00.803 UTC LOG:  duration: 0.047 ms  statement: SHOW default_transaction_isolation
dba test_app 127.0.0.1 2012-09-04 21:54:00.803 UTC LOG:  duration: 0.068 ms  statement: BEGIN; SET TRANSACTION ISOLATION LEVEL READ COMMITTED
dba test_app 127.0.0.1 2012-09-04 21:54:00.804 UTC LOG:  duration: 0.410 ms  statement: SET TIME ZONE E'Pacific/Auckland'
dba test_app 127.0.0.1 2012-09-04 21:54:00.805 UTC ERROR:  duplicate key value violates unique constraint "country_of_origin_code_key"

有人用类似的问题在这里 。

Answer 1:

Django的 针对多个数据库测试支持自1.2!

添加以下属性到我的TestCase的解决了这个问题:

multi_db = True


Answer 2:

哈! 我想我找到它后重新阅读您的问题。

你的工厂类定义origin作为构建过程中它的默认值的一部分。 但我们不传递任何值的设定,因此增量。 因此,要解决这个问题,我们可以做到以下几点:

工厂类

class CountryOfOriginFactory(factory.Factory):
    """ Factory class for app.models.CountryOfOrigin
    """
    FACTORY_FOR = CountryOfOrigin

    code = 'UK'
    the_country = 'United Kingdom'

class MemberFactory(factory.Factory):
    """ Factory class for app.models.Member
    """
    FACTORY_FOR = Member

    first_name = 'Test'
    surname = 'User'
    sex = 'M'
    date_of_birth = datetime.date(1990, 1, 1)
    origin = factory.LazyAttribute(lambda a: CountryOfOriginFactory())

class MemberProgramme(factory.Factory):
    FACTORY_FOR = MemberProgramme

    mem = factory.LazyAttribute(lambda a: MemberFactory())
    prg = factory.LazyAttribute(lambda a: ProgrammeFactory())
    date_registered = date.today() - timedelta(days=1)

测试

class MemberProgrammeTestCase(TestCase):


    def setUp(self):
        self.prog = ProgrammeFactory()
        self.country_of_origin = CountryOfOriginFactory()
        self.member = MemberFactory(origin=self.country_of_origin)
        self.member_programme = MemberProgramme(mem=self.mem, prg=self.prog)


    def date_registered_should_be_defined_test(self):
        # This test passes
        memprg = self.member_programme
        assert hasattr(memprg, 'date_registered')

    def date_registered_should_be_in_past_test(self):
        # This test fails
        memprg = self.member_programme
        assert memprg.date_registered < date.today()


文章来源: Lack of ROLLBACK within TestCase causes unique contraint violation in multi-db django app