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 @@ -143,6 +143,11 @@ and should instead be passed as:

postgres://user:p%23ssword!@localhost/foobar

`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
25 changes: 22 additions & 3 deletions dj_database_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,32 @@


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

if s:
return parse(s, engine, conn_max_age, ssl_require)
return parse(
s, engine, conn_max_age, conn_health_checks, ssl_require, test_options
)

return {}


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

Expand Down Expand Up @@ -120,6 +133,12 @@ def parse(
"CONN_HEALTH_CHECKS": conn_health_checks,
}
)
if test_options:
parsed_config.update(
{
'TEST': test_options,
}
)

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

def test_config_test_options(self):
os.environ[
"DATABASE_URL"
] = "postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?"
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