mirror of
https://github.com/jazzband/dj-database-url.git
synced 2026-03-16 22:20:24 +00:00
Add flake8 and black config, re-format
* Adds a basic black and flake8 compatible config * Reformat the code with black using skip string normalization
This commit is contained in:
parent
a42c0ba71e
commit
973b0bcf35
4 changed files with 49 additions and 29 deletions
5
.flake8
Normal file
5
.flake8
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
[flake8]
|
||||
max-line-length = 88
|
||||
extend-ignore = E203
|
||||
per-file-ignores=
|
||||
test_dj_database_url.py: E501, E265
|
||||
|
|
@ -56,7 +56,9 @@ else:
|
|||
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
|
||||
):
|
||||
"""Returns configured DATABASE dictionary from DATABASE_URL."""
|
||||
|
||||
config = {}
|
||||
|
|
@ -76,10 +78,7 @@ def parse(url, engine=None, conn_max_age=0, ssl_require=False):
|
|||
# this is a special case, because if we pass this URL into
|
||||
# urlparse, urlparse will choke trying to interpret "memory"
|
||||
# as a port number
|
||||
return {
|
||||
'ENGINE': SCHEMES['sqlite'],
|
||||
'NAME': ':memory:'
|
||||
}
|
||||
return {'ENGINE': SCHEMES['sqlite'], 'NAME': ':memory:'}
|
||||
# note: no other settings are required for sqlite
|
||||
|
||||
# otherwise parse the url as normal
|
||||
|
|
@ -114,18 +113,23 @@ def parse(url, engine=None, conn_max_age=0, ssl_require=False):
|
|||
# Lookup specified engine.
|
||||
engine = SCHEMES[url.scheme] if engine is None else engine
|
||||
|
||||
port = (str(url.port) if url.port and engine in [SCHEMES['oracle'], SCHEMES['mssql']]
|
||||
else url.port)
|
||||
port = (
|
||||
str(url.port)
|
||||
if url.port and engine in [SCHEMES['oracle'], SCHEMES['mssql']]
|
||||
else url.port
|
||||
)
|
||||
|
||||
# Update with environment configuration.
|
||||
config.update({
|
||||
'NAME': urlparse.unquote(path or ''),
|
||||
'USER': urlparse.unquote(url.username or ''),
|
||||
'PASSWORD': urlparse.unquote(url.password or ''),
|
||||
'HOST': hostname,
|
||||
'PORT': port or '',
|
||||
'CONN_MAX_AGE': conn_max_age,
|
||||
})
|
||||
config.update(
|
||||
{
|
||||
'NAME': urlparse.unquote(path or ''),
|
||||
'USER': urlparse.unquote(url.username or ''),
|
||||
'PASSWORD': urlparse.unquote(url.password or ''),
|
||||
'HOST': hostname,
|
||||
'PORT': port or '',
|
||||
'CONN_MAX_AGE': conn_max_age,
|
||||
}
|
||||
)
|
||||
|
||||
# Pass the query string into OPTIONS.
|
||||
options = {}
|
||||
|
|
|
|||
2
pyproject.toml
Normal file
2
pyproject.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[tool.black]
|
||||
skip-string-normalization = 1
|
||||
|
|
@ -19,7 +19,6 @@ if DJANGO_VERSION < (2, 0):
|
|||
|
||||
|
||||
class DatabaseTestSuite(unittest.TestCase):
|
||||
|
||||
def test_postgres_parsing(self):
|
||||
url = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
|
||||
url = dj_database_url.parse(url)
|
||||
|
|
@ -146,7 +145,9 @@ class DatabaseTestSuite(unittest.TestCase):
|
|||
a = dj_database_url.config()
|
||||
assert not a
|
||||
|
||||
os.environ['DATABASE_URL'] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
|
||||
os.environ[
|
||||
'DATABASE_URL'
|
||||
] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn'
|
||||
|
||||
url = dj_database_url.config()
|
||||
|
||||
|
|
@ -180,7 +181,9 @@ class DatabaseTestSuite(unittest.TestCase):
|
|||
|
||||
def test_config_engine_setting(self):
|
||||
engine = 'django_mysqlpool.backends.mysqlpool'
|
||||
os.environ['DATABASE_URL'] = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
|
||||
os.environ[
|
||||
'DATABASE_URL'
|
||||
] = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
|
||||
url = dj_database_url.config(engine=engine)
|
||||
|
||||
assert url['ENGINE'] == engine
|
||||
|
|
@ -194,14 +197,18 @@ class DatabaseTestSuite(unittest.TestCase):
|
|||
|
||||
def test_config_conn_max_age_setting(self):
|
||||
conn_max_age = 600
|
||||
os.environ['DATABASE_URL'] = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
|
||||
os.environ[
|
||||
'DATABASE_URL'
|
||||
] = 'mysql://bea6eb025ca0d8:69772142@us-cdbr-east.cleardb.com/heroku_97681db3eff7580?reconnect=true'
|
||||
url = dj_database_url.config(conn_max_age=conn_max_age)
|
||||
|
||||
assert url['CONN_MAX_AGE'] == conn_max_age
|
||||
|
||||
def test_database_url_with_options(self):
|
||||
# Test full options
|
||||
os.environ['DATABASE_URL'] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?sslrootcert=rds-combined-ca-bundle.pem&sslmode=verify-full'
|
||||
os.environ[
|
||||
'DATABASE_URL'
|
||||
] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?sslrootcert=rds-combined-ca-bundle.pem&sslmode=verify-full'
|
||||
url = dj_database_url.config()
|
||||
|
||||
assert url['ENGINE'] == EXPECTED_POSTGRES_ENGINE
|
||||
|
|
@ -212,16 +219,20 @@ class DatabaseTestSuite(unittest.TestCase):
|
|||
assert url['PORT'] == 5431
|
||||
assert url['OPTIONS'] == {
|
||||
'sslrootcert': 'rds-combined-ca-bundle.pem',
|
||||
'sslmode': 'verify-full'
|
||||
'sslmode': 'verify-full',
|
||||
}
|
||||
|
||||
# Test empty options
|
||||
os.environ['DATABASE_URL'] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?'
|
||||
os.environ[
|
||||
'DATABASE_URL'
|
||||
] = 'postgres://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:5431/d8r82722r2kuvn?'
|
||||
url = dj_database_url.config()
|
||||
assert 'OPTIONS' not in url
|
||||
|
||||
def test_mysql_database_url_with_sslca_options(self):
|
||||
os.environ['DATABASE_URL'] = 'mysql://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:3306/d8r82722r2kuvn?ssl-ca=rds-combined-ca-bundle.pem'
|
||||
os.environ[
|
||||
'DATABASE_URL'
|
||||
] = 'mysql://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:3306/d8r82722r2kuvn?ssl-ca=rds-combined-ca-bundle.pem'
|
||||
url = dj_database_url.config()
|
||||
|
||||
assert url['ENGINE'] == 'django.db.backends.mysql'
|
||||
|
|
@ -230,14 +241,12 @@ class DatabaseTestSuite(unittest.TestCase):
|
|||
assert url['USER'] == 'uf07k1i6d8ia0v'
|
||||
assert url['PASSWORD'] == 'wegauwhgeuioweg'
|
||||
assert url['PORT'] == 3306
|
||||
assert url['OPTIONS'] == {
|
||||
'ssl': {
|
||||
'ca': 'rds-combined-ca-bundle.pem'
|
||||
}
|
||||
}
|
||||
assert url['OPTIONS'] == {'ssl': {'ca': 'rds-combined-ca-bundle.pem'}}
|
||||
|
||||
# Test empty options
|
||||
os.environ['DATABASE_URL'] = 'mysql://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:3306/d8r82722r2kuvn?'
|
||||
os.environ[
|
||||
'DATABASE_URL'
|
||||
] = 'mysql://uf07k1i6d8ia0v:wegauwhgeuioweg@ec2-107-21-253-135.compute-1.amazonaws.com:3306/d8r82722r2kuvn?'
|
||||
url = dj_database_url.config()
|
||||
assert 'OPTIONS' not in url
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue