使用pytest-django测试期间,Django连接对象看不到第二个数据库的表 [英] Django connections object does not see the tables of a second database during testing with pytest-django

查看:176
本文介绍了使用pytest-django测试期间,Django连接对象看不到第二个数据库的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

底线:在使用pytest-django测试期间,我的Django连接对象看不到第二个数据库的表关系.

Bottom Line: My Django connections object does not see the table relations of a second database during testing with pytest-django.

概述: 我有一个问题,我的Django连接对象似乎获得了错误的数据库信息.当我在客户"数据库中的一个表上查询时,我偶然发现了这个问题,而Django告诉我该关系不存在.像下面这样设置了settings.py数据库部分:

Overview: I have a problem where my Django connections object seems to get the wrong database information. I stumbled upon this issue when I was querying on a table in the 'customers' DB and Django told me the relation does not exist. With the settings.py database section was set up like below:

DATABASES = {
    'default': {
        'NAME': 'user_data',
        'ENGINE': 'django.db.backends.postgres',
        'USER': 'postgres_1',
        'PASSWORD': 'superS3cret'
    },
    'customers': {
        'NAME': 'customer_data',
        'ENGINE': 'django.db.backends.postgres',
        'USER': 'postgres_1',
        'PASSWORD': 'superS3cret'
    }
}

当我使用以下命令在目录上运行"pytest"时,下面的两个游标都从默认"数据库中获取信息:

Both cursors below get the information from the 'default' database when I run 'pytest' on the directory with:

sql = """SELECT table_name FROM information_schema.tables WHERE table_nameschema='public'"""

default = connections["default"].cursor()
default.execute(sql)
raw_data = default.fetchall()
sql_columns = [col[0] for col in default.description]
df1 = pd.DataFrame(raw_data, columns=sql_columns)

customers = connections["customers"].cursor()
customers.execute(sql)
raw_data = customers.fetchall()
sql_columns = [col[0] for col in customers.description]
df2 = pd.DataFrame(raw_data, columns=sql_columns)

df1和df2的结果完全相同:只有默认"数据库中的表名.

使用pytest-django并使用另一个Postgres数据库(,但有时是)会发生这种情况.

This happens with pytest-django and using a second Postgres database, but only sometimes.

在上面的查询中,我希望df1和df2有所不同,只要'default'和'customers'数据库不同即可.但是,有时连接游标无法正确地查看"第二个数据库中的所有信息.

In the query above I would expect df1 and df2 to be different, so far as the 'default' and 'customers' databases are different. However, on occasion, the connections cursor does not properly 'see' all the info in the second database.

奇怪的是,当我打印时,连接设置以不同的方式显示:

The strange thing is that the connection settings show up differently when I print:

print(connections.databases)

连接"对象包含两个不同的数据库,但一个是测试"数据库.打印语句产生一个字典,但请注意" test_customers ":

the 'connections' object contains two different DBs, but one is a "test" DB. The print statement yields a dictionary, but note the "test_customers":

(pdb) { 'default': { <conn info>}, 'test_customers': { <conn info> } }

似乎Django正在尝试建立测试数据库,失败并且没有通过测试,因为'test_customers'中的表并不以与生产中相同的方式存在.

It seems as though Django is trying to set up a test database, failing, and not passing tests because the tables in 'test_customers' do not exist in the same way as in production.

如何解决此问题,以便pytest-django在测试期间始终能看到第二个数据库(客户)中的表?我在设置和拆卸数据库时是否做错了什么?

How do I fix this so that pytest-django ALWAYS sees the tables in the second database (customers) during testing? Am I doing something wrong with setup and teardown of the DB?

更新:阅读 pytest-django文档有关数据库创建/重用的信息为我指明了正确的方向.但是,我对文档的这一部分有些不满意:

UPDATE: Reading the pytest-django docs on DB creation/re-use has pointed me in the right direction. However, I am a little perturbed by this section of the docs:

目前pytest-django不专门支持Django的 多数据库支持.但是,您可以使用常规的Django TestCase 实例以使用其multi_db支持.

Currently pytest-django does not specifically support Django’s multi-database support. You can, however, use normal Django TestCase instances to use its multi_db support.

如果您对支持多个数据库的最佳API有任何想法 直接在pytest-django中,请与我们联系,我们对 最终支持了这一点,但不确定是否仅遵循Django的 方法.

If you have any ideas about the best API to support multiple databases directly in pytest-django please get in touch, we are interested in eventually supporting this but unsure about simply following Django’s approach.

推荐答案

pytest-django 不支持多个数据库.当我尝试多个数据库和参数时, --reuse-db / --create-db 结果是,有时它可以工作(所有数据库都可以创建并且可以正确使用),有时不起作用(要么没有创建数据库,要么Django抱怨数据库已经存在.

pytest-django does not support multiple databases. When I was experimenting with multiple databases and parameters --reuse-db/--create-db the outcome is, that sometimes it works (all databases are created and can be used correctly) and sometimes it does not work (either the database is not created or Django is complaining that the database already exists).

恕我直言,有两种选择: 1)请勿将pytest与Django一起使用; 2)简化了测试,因此您不需要多个数据库.对于选项2),我正在使用此设置:

IMHO there are two options: 1) do not use pytest with Django; 2) simplify your tests so you do not need multiple databases. For option 2) I'm using this setup:

正常settings:

DATABASES = {
    'default': ...,
    'secondary': ...,
}

pytest.ini:

[pytest]
...
DJANGO_SETTINGS_MODULE=my_app.settings.test
...

test.py:

# Import all from normal settings
from .base import *

DATABASES.pop('secondary')
# This will route all queries and migrations to the default DB
DATABASE_ROUTERS = []

这篇关于使用pytest-django测试期间,Django连接对象看不到第二个数据库的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆