【问题标题】:Django testing database in a restricted environment受限制环境中的 Django 测试数据库
【发布时间】:2021-03-29 20:46:31
【问题描述】:

正如标题所说,我的开发环境受到限制。我无法为测试目的创建额外的数据库,只能创建本地文件。

使用django的environ包,数据库的配置是

DATABASES = {
    'default': env.db('DATABASE_URL', default='postgres://name@localhost'),
}

正如在this thread 中找到的,我所需要的只是此配置中的“TEST”键。它没有一个默认值,所以我通过写作绕过了这一点

db = env.db('DATABASE_URL', default='postgres://name@localhost')

db["TEST"] = {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}

但这不起作用。 python manage.py test 给我以下输出:

Creating test database for alias 'default'...
/usr/lib/python2.7/dist-packages/django/db/backends/postgresql/base.py:267: 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 default database instead.
  RuntimeWarning
Got an error creating the test database: permission denied to create database

Type 'yes' if you would like to try deleting the test database '/home/<closed environment>/<project>/config/db.sqlite3', or 'no' to cancel: 

它仍然尝试在我无权更改自己权限的环境中创建数据库,这当然失败了。我确实将测试数据库指定为本地文件,但不知何故不使用它?我是在监督什么,还是我做错了什么?

【问题讨论】:

  • config/db.sqlite3 已经存在了吗?如果你输入“是”会发生什么?
  • 作为一个快速修复,也许试试python manage.py test --keepdb ?仅供参考,不测试数据库的迁移。
  • 死亡.... django.db.utils.OperationalError: FATAL: database "/home/&lt;name&gt;/&lt;project&gt;/config/db.sqlite3" does not exist :\
  • 您的用户是否有权在该路径创建其他文件? touch /config/test-file ?
  • 试试db["default"]["TEST"]?因为TESTdefault 字典上的一个键。

标签: python django postgresql testing


【解决方案1】:

玩弄之后,我找到了解决方法。

这不是很好,但它有效:\

is_test = "manage.py" in sys.argv and "test" in sys.argv

if is_test:
    db = {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
else:
    db = env.db('DATABASE_URL', default='postgres://name@localhost')

不是 gr8,不可怕。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2010-10-19
    相关资源
    最近更新 更多