Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow TEST settings to be passed into a db config #116

Merged
merged 9 commits into from
Dec 12, 2022
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ and is available in Django 1.6+. If you do not set a value, it will default to `
which is Django's historical behavior of using a new database connection on each
request. Use ``None`` for unlimited persistent connections.

`TEST <https://docs.djangoproject.com/en/stable/ref/settings/#test>`_ settings can be configured using the ``test_options`` attribute::

DATABASES['default'] = dj_database_url.config(default='postgres://...', test_options={'NAME': 'mytestdatabase'})


URL schema
----------

Expand Down
10 changes: 7 additions & 3 deletions dj_database_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@
SCHEMES['pgsql'] = 'django.db.backends.postgresql'


def config(env=DEFAULT_ENV, default=None, engine=None, conn_max_age=0, ssl_require=False):
def config(env=DEFAULT_ENV, default=None, engine=None, conn_max_age=0, ssl_require=False, test_options={}):
"""Returns configured DATABASE dictionary from DATABASE_URL."""

config = {}

s = os.environ.get(env, default)

if s:
config = parse(s, engine, conn_max_age, ssl_require)
config = parse(s, engine, conn_max_age, ssl_require, test_options)

return config


def parse(url, engine=None, conn_max_age=0, ssl_require=False):
def parse(url, engine=None, conn_max_age=0, ssl_require=False, test_options={}):
"""Parses a database URL."""

if url == 'sqlite://:memory:':
Expand Down Expand Up @@ -126,6 +126,10 @@ def parse(url, engine=None, conn_max_age=0, ssl_require=False):
'PORT': port or '',
'CONN_MAX_AGE': conn_max_age,
})
if test_options:
config.update({
'TEST': test_options,
})

# Pass the query string into OPTIONS.
options = {}
Expand Down
8 changes: 8 additions & 0 deletions test_dj_database_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ def test_mysql_connector_parsing(self):
assert url['PASSWORD'] == 'wegauwhgeuioweg'
assert url['PORT'] == 5431

def test_config_test_options(self):
test_db_config = {
'NAME': 'mytestdatabase',
}
url = dj_database_url.config(test_options=test_db_config)

assert url['TEST']['NAME'] == 'mytestdatabase'

def test_cleardb_parsing(self):
url = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
url = dj_database_url.parse(url)
Expand Down