簡體   English   中英

Django 在受限環境中測試數據庫

[英]Django testing database in a restricted environment

正如標題所說,我在我的開發環境中受到限制。 我無法為測試目的創建額外的數據庫,只能創建本地文件。

使用environ package for django,數據庫的配置為

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

正如在這個線程中發現的那樣,我只需要這個配置中的“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給了我以下 output:

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: 

它仍然嘗試在我無權更改自己權限的環境中創建數據庫,這當然失敗了。 我確實將測試數據庫指定為本地文件,但不知何故不使用它? 我是在監督什么,還是在做完全錯誤的事情?

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

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

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,不可怕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM