Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: How to access test database?

Tags:

python

django

I am working in Django 1.8. I have a Django management command to create materialized views on my database, as follows:

python manage.py create_db_matviews $DB_NAME $DB_USER $DB_PASS

Now I want to run the management command from inside my tests, so that I can test the command. (My actual database is extremely large, and the management command is extremely slow, so I would like to test it on test data, rather than real data.)

However calling the management command from inside my test file does not work - unsurprisingly since I have no idea what credentials to use:

def setUpModule():
    management.call_command('loaddata', 'frontend/fixtures/mydata.json',
                        verbosity=0)
    # load fixtures, then... 
    management.call_command('create_db_matviews',
                            'default', 'default', 'randomguess', 
                            verbosity=0)

It fails as follows:

Creating test database for alias 'default' ('test_prescribing')...
... running migrations...  
======================================================================
ERROR: setUpModule (frontend.tests.test_api_views)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/.../tests/test_api_views.py", line 23, in setUpModule
    verbosity=0)
  File "/Users/.../.virtualenvs/openprescribing/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "/Users/.../.virtualenvs/openprescribing/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/.../frontend/management/commands/create_indexes_and_matviews.py", line 19, in handle
    password=db_pass)
  File "/Users/.../.virtualenvs/openprescribing/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
OperationalError: FATAL:  role "default" does not exist

What credentials should I use to get access to the test database?

like image 982
Richard Avatar asked May 12 '15 11:05

Richard


People also ask

What database does Django test use?

To help you get started, Django provides and uses a sample settings module that uses the SQLite database. See Using another settings module to learn how to use a different settings module to run the tests with a different database.

How do I run tests PY in Django?

Open /catalog/tests/test_models.py.from django. test import TestCase # Create your tests here. Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality.

How do I test a Django project?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.


1 Answers

This section of the Django docs has the details.

Basically the name of the test database is as per your settings file but with test_ prefixed, and the user and password are as your settings file.

like image 115
Richard Avatar answered Oct 07 '22 04:10

Richard