-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Gerbik
committed
Jun 9, 2022
1 parent
a5ec6a4
commit 92e2de8
Showing
2 changed files
with
38 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
|
@@ -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): | ||
|
@@ -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): | ||
|
@@ -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) | ||
|