diff --git a/README.rst b/README.rst index 71f0c74..6573f4c 100644 --- a/README.rst +++ b/README.rst @@ -143,6 +143,11 @@ and should instead be passed as: postgres://user:p%23ssword!@localhost/foobar +`TEST `_ settings can be configured using the ``test_options`` attribute:: + + DATABASES['default'] = dj_database_url.config(default='postgres://...', test_options={'NAME': 'mytestdatabase'}) + + URL schema ---------- diff --git a/dj_database_url.py b/dj_database_url.py index 286b575..ae795b1 100644 --- a/dj_database_url.py +++ b/dj_database_url.py @@ -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.""" @@ -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 = {} diff --git a/test_dj_database_url.py b/test_dj_database_url.py index 077622b..c76c816 100644 --- a/test_dj_database_url.py +++ b/test_dj_database_url.py @@ -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)