简体   繁体   English

使用PostgreSQL 9.3运行Django单元测试,这甚至可能吗?

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

Running unittests with Django 1.7 on sqlite is a no brainer. 在sqlite上运行Django 1.7的单元测试是没有道理的。 Couple of config lines and you are good to go. 几行配置行,您一切顺利。 With Postgres, it almost seems impossible. 使用Postgres,几乎似乎是不可能的。

I create the test database, set the owner of the DB and the Schema to the user testing, grant all on database to testing and we should be good to go right? 我创建了测试数据库,将数据库的所有者和Schema设置为用户测试,将数据库上的所有内容都授予了测试,我们应该很好吗? BUT NO! 但不是!

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;

brings me back around to 带我回到

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

It seems django's desire to drop the database puts a spanner in the works. django删除数据库的愿望似乎使扳手发挥了作用。

I have a script to wipe the db and recreate the structure that looks like this 我有一个脚本来擦除数据库并重新创建如下所示的结构

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

This works just fine. 这样很好。 I know best practices are to test on the same engine you run in production, but everywhere I look all the testing is being done on sqlite which won't work for me because I'm now using a citext column and want to test the behavior of the code with that column type. 我知道最佳实践是在生产环境中运行的同一引擎上进行测试,但是在所有地方看来,所有测试都是在sqlite上完成的,因为我现在使用的是citext列并希望测试其行为,因此对我不起作用该列类型的代码。

How do I get to the point I can test with postgres? 如何达到可以使用postgres测试的地步?

I have a setup like the one you are talking about in your question. 我有一个类似您所谈论的问题的设置。

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

this uses the postgres database in testing, but, you need to remember that it only uses the schema, you cannot run unit tests using real database values. 它在测试中使用postgres数据库,但是,您需要记住,它仅使用架构,不能使用实际数据库值运行单元测试。

I did have to create the database in postgres for it to work. 我确实必须在postgres中创建数据库才能正常工作。

Not sure if this will help you. 不知道这是否对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM