如何在DRF项目中设置测试数据库配置?

huangapple go评论58阅读模式
英文:

How to set up test db settings in a DRF project?

问题

以下是翻译好的内容:

"我正在在一个使用MongoDB和Djongo连接器的DRF项目中运行单元测试。当我逐个运行测试时,它们都能够成功编译,但当我使用 python3 manage.py test test.path 运行它们时,除了第一个测试之外,其余都失败。日志中的以下行指示存在关于删除测试数据库的问题。

FAILED SQL: DROP DATABASE "test_my_project_name"

以下是数据库配置。

DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'my_project_name',
'CLIENT': {
'host': os.environ[DB_HOST],
}
},
'test': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test_mytestdatabase',
'USER': 'mytestdatabaseuser',
'PASSWORD': 'mytestpassword',
'HOST': 'localhost',
'PORT': '5432',
}
}

我需要做什么来确保测试期间使用测试数据库而不是主数据库?还需要设置其他任何内容来进行测试吗?"

英文:

I'm running unit tests in a drf project with a mongo db and djongo connector. When I run tests one by one they compile successfully but when I run all of them with python3 manage.py test test.path they all fail except the first test. The following line from the logs indicates that there is an issue with dropping the test database.

FAILED SQL: DROP DATABASE "test_my_project_name"

Here's the database configuration.

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'my_project_name',
        'CLIENT': {
           'host': os.environ[DB_HOST],
        }
    },
    'test': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'test_mytestdatabase',
        'USER': 'mytestdatabaseuser',
        'PASSWORD': 'mytestpassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

What do I need to do to make the test db be used during tests and not the main db.
Also do I need to set up anything else for the testing?

答案1

得分: 0

During tests, Django使用默认配置。唯一的区别是,在每次测试之前和之后,它会创建和销毁一个以test_前缀命名的测试数据库,该数据库名称与您的默认数据库名称相同,只是多了前缀。您的错误表明它无法删除test_my_project_name数据库 - 您的默认数据库名称是my_project_name,而您的“test”数据库名称是test_mytestdatabase

您需要采用不同的方法,可以在这个Stack Overflow答案中找到详细说明。

简而言之,您必须为测试使用完全不同的DATABASES配置:

import sys
if 'test' in sys.argv:
    DATABASES['default'] = {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase'
    }

我建议将此条件存储为设置变量之一,例如:

TEST_MODE = 'test' in sys.argv

然后,您可以在运行时用它来执行许多操作(日志记录、存根、跳过某些验证等)。

英文:

During tests django use default config. Only difference is that before and after each test, it creates and destroys test db named after your default db with test_ prefix.
Your error confirms that by saying it cannot drop test_my_project_name database - your default db name is my_project_name while your "test" db name is test_mytestdatabase.

You'll have to use different approach explained in this SO answer.

In short, you must use entirely different DATABASES config for testing:

import sys
if 'test' in sys.argv:
    DATABASES['default'] = {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase'
    }

I would suggest storing that condition as one of the settings variables, for example

TEST_MODE = 'test' in sys.argv

and then you can use it for many things in runtime (logging, stubs, skip some validation, etc...)

huangapple
  • 本文由 发表于 2023年4月10日 21:14:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定