繁体   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