Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Gerbik committed Jun 9, 2022
1 parent a5ec6a4 commit 92e2de8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
22 changes: 13 additions & 9 deletions dj_database_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
DJANGO_VERSION = None


# https://docs.djangoproject.com/en/2.0/releases/2.0/#id1
if DJANGO_VERSION and DJANGO_VERSION < (2, 0):
POSTGRES_BACKEND = "django.db.backends.postgresql_psycopg2"
else:
POSTGRES_BACKEND = "django.db.backends.postgresql"
def get_postgres_backend():
# Django deprecated the `django.db.backends.postgresql_psycopg2` in 2.0.
# https://docs.djangoproject.com/en/stable/releases/2.0/#id1
if DJANGO_VERSION and DJANGO_VERSION < (2, 0):
return "django.db.backends.postgresql_psycopg2"
return "django.db.backends.postgresql"


POSTGRES_BACKEND = get_postgres_backend()


class ParseError(ValueError):
Expand Down Expand Up @@ -137,10 +141,10 @@ def address_deprecated_arguments(backend, settings):
"The `ssl_require` argument is deprecated."
" Use `OPTIONS={'sslmode': 'require'}` instead."
)
if settings.pop("ssl_require"):
options = settings.pop("OPTIONS", {})
options["sslmode"] = "require"
settings["OPTIONS"] = options
settings.pop("ssl_require")
options = settings.pop("OPTIONS", {})
options["sslmode"] = "require"
settings["OPTIONS"] = options
return backend


Expand Down
37 changes: 25 additions & 12 deletions test_dj_database_url.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import os
import re
import unittest

try:
from django import VERSION as DJANGO_VERSION
except ImportError:
DJANGO_VERSION = None
from unittest.mock import patch

import dj_database_url

URL = "postgres://user:password@localhost/db-name"
# Django deprecated the `django.db.backends.postgresql_psycopg2` in 2.0.
# https://docs.djangoproject.com/en/2.0/releases/2.0/#id1
EXPECTED_POSTGRES_ENGINE = "django.db.backends.postgresql"
if DJANGO_VERSION and DJANGO_VERSION < (2, 0):
EXPECTED_POSTGRES_ENGINE = "django.db.backends.postgresql_psycopg2"
EXPECTED_POSTGRES_ENGINE = dj_database_url.get_postgres_backend()


class DeprecatedArgumentsTestSuite(unittest.TestCase):
Expand Down Expand Up @@ -67,14 +60,24 @@ def test_parse_engine_setting(self):

assert url["ENGINE"] == engine

def test_pass_ssl_require__handle_and_issue_warning(self):
message = (
"The `ssl_require` argument is deprecated."
" Use `OPTIONS={'sslmode': 'require'}` instead."
)
with self.assertWarnsRegex(Warning, re.escape(message)):
config = dj_database_url.parse(URL, ssl_require=True)

assert config["OPTIONS"] == {'sslmode': 'require'}


class DatabaseTestSuite(unittest.TestCase):
def test_credentials_unquoted__raise_value_error(self):
expected_message = (
"This string is not a valid url, possibly because some of its parts "
r"is not properly urllib.parse.quote()'ed."
)
with self.assertRaises(ValueError, msg=expected_message):
with self.assertRaisesRegex(ValueError, re.escape(expected_message)):
dj_database_url.parse("postgres://user:passw#ord!@localhost/foobar")

def test_credentials_quoted__ok(self):
Expand All @@ -85,7 +88,7 @@ def test_credentials_quoted__ok(self):

def test_unknown_scheme__raise_value_error(self):
expected_message = "Scheme 'unknown-scheme://' is unknown. Did you forget to register custom backend?"
with self.assertRaises(ValueError, msg=expected_message):
with self.assertRaisesRegex(ValueError, re.escape(expected_message)):
dj_database_url.parse("unknown-scheme://user:password@localhost/foobar")

def test_provide_test_settings__add_them_to_final_config(self):
Expand Down Expand Up @@ -116,6 +119,16 @@ def test_provide_conn_max_age__use_it_in_final_config(self):
config = dj_database_url.parse(URL, CONN_MAX_AGE=600)
assert config["CONN_MAX_AGE"] == 600

@patch("dj_database_url.DJANGO_VERSION", (1, 11, 0, "final", 1))
def test_django_version_pre_2__use_postgresql_psycopg2_backend(self):
expected = "django.db.backends.postgresql_psycopg2"
assert dj_database_url.get_postgres_backend() == expected

@patch("dj_database_url.DJANGO_VERSION", (3, 2, 0, "final", 0))
def test_django_version_post_2__use_postgresql_backend(self):
expected = "django.db.backends.postgresql"
assert dj_database_url.get_postgres_backend() == expected

def test_postgres_parsing(self):
url = "postgres://uf07k1i6d8ia0v:[email protected]:5431/d8r82722r2kuvn"
url = dj_database_url.parse(url)
Expand Down

0 comments on commit 92e2de8

Please sign in to comment.