Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up a test database in Django

I want to create integration tests so that i can create 1000 records of a single model in my database.

for my settings.py file i have specified to use the same database default when running tests.

if 'test' in sys.argv or 'test_coverage' in sys.argv:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'TableName',                      # Or path to database file if using sqlite3.
            'USER': 'postgres',                      # Not used with sqlite3.
            'PASSWORD': 'password',                  # Not used with sqlite3.
            'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
            'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.
        }
    }

When i run the command python manage.py test <app> i get an error stating that i have two sessions running.

 Got an error creating the test database: source database "template1" is being accessed by other users
DETAIL:  There are 1 other session(s) using the database.
like image 921
Warz Avatar asked Sep 03 '25 16:09

Warz


1 Answers

Make sure that python manage.py runserver or any other processes accessing your postgresql (such as pgadmin3 or phppgadmin) is disabled before running your tests.

In other words, make sure that you have killed the running postgresql connections for your application.

You can also determine which are the open connections that an application is making to your postgresql database via

psql -U postgres

postgres=# SELECT * FROM pg_stat_activity;

and if you want to be specific, you can do:-

postgres=# SELECT * FROM pg_stat_activity where datname = 'TaleeboBixin';
like image 187
Calvin Cheng Avatar answered Sep 05 '25 15:09

Calvin Cheng