How to automatically create postgis database for Django testing?
django, python, unit-testing
Solution
It may be that your `DATABASE_USER` doesn't have permissions to create a new database/schema.
Edit
If you read the source for the Django `test` command, you'll see that it always creates a test database. Further, it modifies your settings to reference this test database.
See this: http://docs.djangoproject.com/en/dev/topics/testing/#id1
What you should do is use fixtures. Here's how we do it.
From your template database, create a "fixture". Use the `manage.py dumpdata` command to create a JSON file with all of your template data. [Hint, the `--indent=2` option gives you readable JSON that you can edit and modify.]
Put this in a `fixtures` directory under your application.
Reference the fixtures file in your TestCase class definition. 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.
Problem
I'm trying to test my Django apps which run on a PostGIS database, by following the info in the Django testing docs. Normally I create a new database by copying a template: (as user postgres) ``` createdb -T template_postgis -O lizard test_geodjango2 ``` When I run `./manage.py test`, I get the following message: 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: What's the best way to let the system create the database?