简体   繁体   中英

Django: How to access test database?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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