簡體   English   中英

使用PostgreSQL 9.3運行Django單元測試,這甚至可能嗎?

[英]Running Django unittests with postgresql 9.3, is this even possible?

在sqlite上運行Django 1.7的單元測試是沒有道理的。 幾行配置行,您一切順利。 使用Postgres,幾乎似乎是不可能的。

我創建了測試數據庫,將數據庫的所有者和Schema設置為用戶測試,將數據庫上的所有內容都授予了測試,我們應該很好嗎? 但不是!

postgres=# create user testing with password '*****';
postgres=# create database project_test;
postgres=# grant all on database project_test to testing;
postgres=# alter database project_test owner to testing;
postgres=# \c project_test;
postgres=# alter schema public owner to testing;

(project) $python manage.py test
Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database

Type 'yes' if you would like to try deleting the test database 'project_test', or 'no' to cancel: yes
Destroying old test database 'default'...
Got an error recreating the test database: permission denied to create database

postgres=# grant create on tablespace pg_default to testing;

帶我回到

Type 'yes' if you would like to try deleting the test database 'project_test', or 'no' to cancel: yes
Destroying old test database 'default'...
Got an error recreating the test database: database "project_test" does not exist

django刪除數據庫的願望似乎使扳手發揮了作用。

我有一個腳本來擦除數據庫並重新創建如下所示的結構

set -e
python manage.py dbshell <<EOF
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
EOF

這樣很好。 我知道最佳實踐是在生產環境中運行的同一引擎上進行測試,但是在所有地方看來,所有測試都是在sqlite上完成的,因為我現在使用的是citext列並希望測試其行為,因此對我不起作用該列類型的代碼。

如何達到可以使用postgres測試的地步?

我有一個類似您所談論的問題的設置。

DATABASES = {
'default': {
    #'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'ENGINE': 'django.contrib.gis.db.backends.postgis',
    'NAME': '#####',
    'USER': '#####',
    'PASSWORD': DJANGO_DB_PASSWORD

它在測試中使用postgres數據庫,但是,您需要記住,它僅使用架構,不能使用實際數據庫值運行單元測試。

我確實必須在postgres中創建數據庫才能正常工作。

不知道這是否對您有幫助。

暫無
暫無

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

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