Django 对多个数据库进行单元测试

发布于 2024-10-02 18:25:53 字数 273 浏览 0 评论 0原文

我正在开发一个 Django 项目,其中我的所有单元测试用例都运行良好。

一旦我引入了第二个数据库,所有从 TestCase 继承的测试用例都被破坏了。在这个阶段,我还没有为第二个数据库构建任何测试用例,但我的路由器工作正常。

当我运行测试时,出现错误

“KeyError:'SUPPORTS_TRANSACTIONS'”

在我看来,它试图检查我设置的所有数据库是否都支持事务,但从未创建第二个数据库。

关于如何使用测试脚本构建第二个数据库的任何想法。

I'm working on a django project where all my unit test cases were working perfectly.

Ass soon as I introduced a second database all my test cases that inherit from TestCase are broken. At this stage I haven't build any test case for that second database but my router is working fine.

When I run the tests I get the error,

"KeyError: 'SUPPORTS_TRANSACTIONS'"

It appears to me that is trying to check that that all the databases that I've got setup support transactions but the second database is never created.

Any ideas on how to have the test script to build the second database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

阳光下的泡沫是彩色的 2024-10-09 18:25:53

我意识到这是一个相当古老的线程,但我遇到了同样的问题,我的解决方案是将 multi_db = True 标志添加到我的测试用例中,例如:

class TestThingWithMultipleDatabases(TestCase):
     multi_db = True

     def test_thing(self):
         pass

Source https://github.com/django/django/blob/master/django/test/ testcases.py#L861

这会导致 django 在所有数据库上调用 flush (或者如果它们支持事务则回滚)

我也在使用数据库路由器

恐怕我在 Django 中找不到这个文档,所以没有链接

I realise this is quite an old thread, but I ran into it with the same issue, and my resolve was adding the multi_db = True flag to my testcase, e.g:

class TestThingWithMultipleDatabases(TestCase):
     multi_db = True

     def test_thing(self):
         pass

Source https://github.com/django/django/blob/master/django/test/testcases.py#L861

This causes django to call flush on all databases (or rollback if they support transactions)

I too am using a db router

I'm afraid I cant find this in Django's documentation, so no link for that

差↓一点笑了 2024-10-09 18:25:53

是的,我有一个类似的问题...我的解决方法是设置 'SUPPORTS_TRANSACTIONS': True 对于设置文件中的每个数据库连接。不确定这是否是修复它的正确方法,但它对我有用。

yes I had a similar problem... my fix was to set 'SUPPORTS_TRANSACTIONS': True for each of the database connections in the settings file. Not sure if this is the correct way to fix it, but it worked for me.

橘亓 2024-10-09 18:25:53

参考该链接
Django 文档多数据库
你可以:

from django.test import TransactionTestCase

class TestMyViews(TransactionTestCase):
    databases = {'default', 'other'} # '__all__' should work too

    def test_index_page_view(self):
        call_some_test_code()

感谢

@sih4sing5hog5

Referring to that link
Django doc Multi-Db
you can:

from django.test import TransactionTestCase

class TestMyViews(TransactionTestCase):
    databases = {'default', 'other'} # '__all__' should work too

    def test_index_page_view(self):
        call_some_test_code()

thanks to

@sih4sing5hog5

ゞ花落谁相伴 2024-10-09 18:25:53

'SUPPORTS_TRANSACTIONS':True 也对我有用。
然而,我有一种使用数据库路由器的奇怪的多数据库设置。
@user298404:你的多数据库设置是什么样的?

附注对不起;积分不够,无法发表评论...

'SUPPORTS_TRANSACTIONS':True worked for me too.
However I have a kind of weird multiple db setup using database routers.
@user298404: how does your multiple db setup look like?

ps. sorry; not enough points for comment...

东风软 2024-10-09 18:25:53

这是我目前在生产中使用的多数据库设置:

DATABASES = {
    # 'default' is used as the WRITE (master) connection
    DB_PRIMARY_MASTER: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'main',
        'USER': 'main_write',
        'PASSWORD': 'XXXX',
        'HOST': 'db-master',
        'PORT': '3306',
        'SUPPORTS_TRANSACTIONS': True,
    },

    # Slave connections are READONLY
    DB_PRIMARY_SLAVE: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'main',
        'USER': 'main_read',
        'PASSWORD': 'XXXX',
        'HOST': 'db-slave',
        'PORT': '3306',
        'TEST_MIRROR': DB_PRIMARY_MASTER,
        'SUPPORTS_TRANSACTIONS': True,
    },

    # 'mail_default' is used as the WRITE (master) connection for the mail database
    DB_MAIL_MASTER: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail',
        'USER': 'dbmail_write',
        'PASSWORD': 'XXXX',
        'HOST': 'db-mail-master',
        'PORT': '3306',
        'SUPPORTS_TRANSACTIONS': True,
    },

    # Slave connections are READONLY
    DB_MAIL_SLAVE: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail',
        'USER': 'dbmail_read',
        'PASSWORD': 'XXXX',
        'HOST': 'db-mail-slave',
        'PORT': '3306',
        'TEST_MIRROR': DB_MAIL_MASTER,
        'SUPPORTS_TRANSACTIONS': True,
    },
}

DB_PRIMARY_MASTER、DB_PRIMARY_SLAVE、DB_MAIL_MASTER 和 DB_MAIL_SLAVE 都是字符串常量,以便它们可以在我的数据库路由器中使用。
提示:DB_PRIMARY_MASTER='default'

我希望这会有所帮助!

Here is a multiple db setup that I currently have in production:

DATABASES = {
    # 'default' is used as the WRITE (master) connection
    DB_PRIMARY_MASTER: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'main',
        'USER': 'main_write',
        'PASSWORD': 'XXXX',
        'HOST': 'db-master',
        'PORT': '3306',
        'SUPPORTS_TRANSACTIONS': True,
    },

    # Slave connections are READONLY
    DB_PRIMARY_SLAVE: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'main',
        'USER': 'main_read',
        'PASSWORD': 'XXXX',
        'HOST': 'db-slave',
        'PORT': '3306',
        'TEST_MIRROR': DB_PRIMARY_MASTER,
        'SUPPORTS_TRANSACTIONS': True,
    },

    # 'mail_default' is used as the WRITE (master) connection for the mail database
    DB_MAIL_MASTER: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail',
        'USER': 'dbmail_write',
        'PASSWORD': 'XXXX',
        'HOST': 'db-mail-master',
        'PORT': '3306',
        'SUPPORTS_TRANSACTIONS': True,
    },

    # Slave connections are READONLY
    DB_MAIL_SLAVE: {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail',
        'USER': 'dbmail_read',
        'PASSWORD': 'XXXX',
        'HOST': 'db-mail-slave',
        'PORT': '3306',
        'TEST_MIRROR': DB_MAIL_MASTER,
        'SUPPORTS_TRANSACTIONS': True,
    },
}

DB_PRIMARY_MASTER, DB_PRIMARY_SLAVE, DB_MAIL_MASTER, and DB_MAIL_SLAVE are all string constants so that they can be used in my database router.
Hint: DB_PRIMARY_MASTER='default'

I hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文