How to automatically create a postgis database for testing Django?

I am trying to test my Django applications that run in the PostGIS database, following the information in the Django docs .

Usually I create a new database by copying a template:

(as postgres user)

createdb -T template_postgis -O lizard test_geodjango2 

When I run ./manage.py test , I get the following message:

Creating a test database ... An error occurred while creating a test database: permission was denied for creating a database

Enter β€œyes” if you want to try deleting the test database β€œtest_geodjango2” or β€œno” to cancel:

What is the best way to let the system create a database?

+4
source share
3 answers

Perhaps your DATABASE_USER does not have permission to create a new database / schema.


Edit

If you read the source for the Django test command, you will see that it always creates a test database. In addition, it changes your settings to link to this test database.

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

What you need to do is use lights . This is how we do it.

  • From the template database, create a "binding". Use the manage.py dumpdata to create a JSON file with all of your template data. [Hint, option --indent=2 provides readable JSON that you can edit and modify.]

  • Put this in the fixtures directory in your application.

  • A reference to the instrument file in the definition of the TestCase class. This will load the instrument before starting the test.

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

Lights replace your template database. You no longer need a template if you have lights.

+4
source

As S. Lott mentioned, use the standard test command.

Using geodjango with postgis, you need to add the following settings to the correct valid spatial template templates.

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

I have not studied it yet, but when I do this, I get a database password request when it tries to install the required sql.

+2
source

Starting with Django 1.11, Django only supports setting for Postgres settings.DATABASE[whatever]['TEST']['TEMPLATE'] , which determines which template the test database is created from:

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

0
source

Source: https://habr.com/ru/post/1286675/


All Articles