简体   繁体   English

Django:在 Django 测试套件中使用只读数据库

[英]Django: Use a read-only database within django test suite

In my Django project, I'm using two databases, one of which is my own PostgreSQL database where I have the read and write rights, and the other one is an external PostgreSQL database in which I only have read-only rights.在我的 Django 项目中,我使用了两个数据库,其中一个是我自己拥有读写权限的 PostgreSQL 数据库,另一个是我只有只读权限的外部 PostgreSQL 数据库。 By the way, both databases have their own respective models, they aren't the same in any way.顺便说一句,两个数据库都有各自的模型,它们在任何方面都不相同。

It works perfectly in the context of the project, I can access both databases.它在项目的上下文中完美运行,我可以访问两个数据库。 However when I use the Django test suite using ./manage.py test , Django is trying to create a test database for the external database.但是,当我使用./manage.py test使用 Django 测试套件时,Django 正在尝试为外部数据库创建一个测试数据库。

I don't want that, I want to be still able to access the external PostgreSQL database within the test suite without needing to create a test database on this external PostgreSQL database.我不希望这样,我希望仍然能够访问测试套件中的外部 PostgreSQL 数据库,而无需在此外部 PostgreSQL 数据库上创建测试数据库。

It also gives me this error:它也给了我这个错误:

/usr/local/lib/python3.10/site-packages/django/db/backends/postgresql/base.py:323: RuntimeWarning:
Normally Django will use a connection to the 'postgres' database to avoid
running initialization queries against the production database when it's not needed
(for example, when running tests).
Django was unable to create a connection to the 'postgres' database
and will use the first PostgreSQL database instead.

But I don't have access to the 'postgres' database in the external database and I don't want to run initialization queries against it.但我无权访问外部数据库中的“postgres”数据库,我不想对它运行初始化查询。

Here is the configuration for the external read-only database connection:以下是外部只读数据库连接的配置:

DATABASES["aact"] = {
    "ENGINE": "django.db.backends.postgresql_psycopg2",
    "OPTIONS": {"options": "-c search_path=ctgov"},
    "NAME": AACT_DATABASE_NAME,
    "USER": AACT_DATABASE_USER,
    "PASSWORD": AACT_DATABASE_PASS,
    "HOST": AACT_DATABASE_HOST,
    "PORT": AACT_DATABASE_PORT,
    "TEST": {"NAME": AACT_DATABASE_NAME, "MIGRATE": False},
}

on test you can choose any database which you want.在测试中,您可以选择任何您想要的数据库。

in settings.py, or, much better, local_settings.py:在 settings.py 中,或者更好的是 local_settings.py:

import sys

DATABASES["aact"] = {...}
if 'test' in sys.argv:
   DATABASES["aact"] = {settings for fake database with write permissions}

other way - define your DbRouter.其他方式 - 定义您的 DbRouter。 https://docs.djangoproject.com/en/4.0/topics/db/multi-db/#database-routers https://docs.djangoproject.com/en/4.0/topics/db/multi-db/#database-routers

class BaseRouter:
    
    def db_for_read(self, model, **__):
        return "db for read"

    def db_for_write(self, model, **__):
        return "db for write"

after that in settings.py:之后在settings.py中:

DATABASE_ROUTERS = ['somethere.whereis.BaseRouter']

The first example if fast to start.第一个例子如果快速启动。 The second help you to make tests better.第二个帮助您更好地进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM