0

as the title says, I'm restricted in my development environment. I cannot create extra databases for testing purposes and only create local files.

Using the environ package for django, the configuration of the database is

DATABASES = {
    'default': env.db('DATABASE_URL', default='postgres://name@localhost'),
}

As found in this thread, all I need is a "TEST" key in this configuration. It doesn't have one per default so I've bypassed this by writing

db = env.db('DATABASE_URL', default='postgres://name@localhost')

db["TEST"] = {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}

This doesn't work however. python manage.py test gives me the following output:

Creating test database for alias 'default'...
/usr/lib/python2.7/dist-packages/django/db/backends/postgresql/base.py:267: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.
  RuntimeWarning
Got an error creating the test database: permission denied to create database

Type 'yes' if you would like to try deleting the test database '/home/<closed environment>/<project>/config/db.sqlite3', or 'no' to cancel: 

It still tries to create a database inside an environment in which I do not have the power to alter my own privileges, something that of course fails. I did specify a testing database as a local file but it somehow doesn't use that? Am I overseeing something or am I doing something completely wrong?

Albert
  • 417
  • 3
  • 14

1 Answers1

0

After toying around I've found a workaround.

It's not great but it works :\

is_test = "manage.py" in sys.argv and "test" in sys.argv

if is_test:
    db = {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
else:
    db = env.db('DATABASE_URL', default='postgres://name@localhost')

Not gr8, not terrible.

Albert
  • 417
  • 3
  • 14