简体   繁体   English

如何为Django测试自动创建postgis数据库?

[英]How to automatically create postgis database for Django testing?

I'm trying to test my Django apps which run on a PostGIS database, by following the info in the Django testing docs . 我正在尝试通过遵循Django测试文档中的信息来测试在PostGIS数据库上运行的Django应用程序。

Normally I create a new database by copying a template: 通常我会通过复制模板来创建一个新数据库:

(as user postgres) (作为用户postgres)

createdb -T template_postgis -O lizard test_geodjango2

When I run ./manage.py test , I get the following message: 当我运行./manage.py test ,我收到以下消息:

Creating test database... Got an error creating the test database: permission denied to create database 创建测试数据库...创建测试数据库时出错:拒绝创建数据库的权限

Type 'yes' if you would like to try deleting the test database 'test_geodjango2', or 'no' to > cancel: 如果您想尝试删除测试数据库'test_geodjango2',或者'否'>取消,请键入“是”:

What's the best way to let the system create the database? 让系统创建数据库的最佳方法是什么?

It may be that your DATABASE_USER doesn't have permissions to create a new database/schema. 可能是您的DATABASE_USER无权创建新的数据库/架构。


Edit 编辑

If you read the source for the Django test command, you'll see that it always creates a test database. 如果您阅读Django test命令的源代码,您将看到它始终创建一个测试数据库。 Further, it modifies your settings to reference this test database. 此外,它会修改您的设置以引用此测试数据库。

See this: http://docs.djangoproject.com/en/dev/topics/testing/#id1 请参阅: http//docs.djangoproject.com/en/dev/topics/testing/#id1

What you should do is use fixtures . 你应该做的是使用固定装置 Here's how we do it. 这是我们如何做到的。

  1. From your template database, create a "fixture". 从模板数据库中,创建一个“fixture”。 Use the manage.py dumpdata command to create a JSON file with all of your template data. 使用manage.py dumpdata命令创建包含所有模板数据的JSON文件。 [Hint, the --indent=2 option gives you readable JSON that you can edit and modify.] [提示, - --indent=2选项为您提供可编辑和修改的可读JSON。]

  2. Put this in a fixtures directory under your application. 把它放在你的应用程序下的fixtures目录中。

  3. Reference the fixtures file in your TestCase class definition. 在TestCase类定义中引用fixtures文件。 This will load the fixture prior to running the test. 这将在运行测试之前加载夹具。

     class AnimalTestCase(TestCase): fixtures = ['mammals.json', 'birds'] def testFluffyAnimals(self): etc. 

The fixtures replace your template database. 灯具会替换您的模板数据库。 You don't need the template anymore once you have the fixtures. 拥有灯具后,您不再需要模板。

As S.Lott mentioned, use the standard test command. 正如S.Lott所提到的,使用标准测试命令。

Using geodjango with postgis you'll need to add the following to your settings for the spatial templates to be created properly. 将geodjango与postgis一起使用时,您需要将以下内容添加到您的设置中,以便正确创建空间模板。

settings.py settings.py

POSTGIS_SQL_PATH = 'C:\\Program Files\\PostgreSQL\\8.3\\share\\contrib'
TEST_RUNNER='django.contrib.gis.tests.run_tests'

console 安慰

manage.py test

Described here: http://geodjango.org/docs/testing.html?highlight=testing#testing-geodjango-apps 这里描述: http//geodjango.org/docs/testing.html?highlight = tesing #testing-geodjango-apps

I haven't looked into it yet, but when I do this I get prompted for the database password when it attempts to install the necessary sql. 我还没有调查过,但是当我这样做时,我会在尝试安装必要的sql时提示输入数据库密码。

As of Django 1.11, Django supports the Postgres-only setting settings.DATABASE[whatever]['TEST']['TEMPLATE'] that dictates what template the test database is created from: 从Django 1.11开始,Django支持仅Postgres设置settings.DATABASE[whatever]['TEST']['TEMPLATE'] ,指示测试数据库的创建模板:

https://docs.djangoproject.com/en/dev/ref/settings/#template https://docs.djangoproject.com/en/dev/ref/settings/#template

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

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