From f0933092a31c8c463c58cffca747b84fb4569f00 Mon Sep 17 00:00:00 2001
From: Jonas Obrist <ojiidotch@gmail.com>
Date: Sun, 25 Dec 2011 14:01:40 +0100
Subject: [PATCH 1/2] Added test and fix for #17047
---
django/db/utils.py | 11 ++++++++++-
tests/regressiontests/backends/tests.py | 10 ++++++++--
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/django/db/utils.py b/django/db/utils.py
index 41ad6df..f5a6ac6 100644
a
|
b
|
def load_backend(backend_name):
|
32 | 32 | and not f.startswith('.')] |
33 | 33 | except EnvironmentError: |
34 | 34 | available_backends = [] |
35 | | if backend_name.startswith('django.db.backends.'): |
| 35 | new_style_import = backend_name.startswith('django.db.backends.') |
| 36 | if new_style_import: |
36 | 37 | backend_name = backend_name[19:] # See #15621. |
37 | 38 | if backend_name not in available_backends: |
38 | 39 | error_msg = ("%r isn't an available database backend. \n" + |
… |
… |
def load_backend(backend_name):
|
40 | 41 | "Error was: %s") % \ |
41 | 42 | (backend_name, ", ".join(map(repr, sorted(available_backends))), e_user) |
42 | 43 | raise ImproperlyConfigured(error_msg) |
| 44 | elif not new_style_import: |
| 45 | # user tried to use database backend using old-school non-qualified |
| 46 | # import path (eg 'sqlite3' instead of 'django.db.backends.sqlite3') |
| 47 | error_msg = ("%r isn't an available database backend.\n" |
| 48 | "The backend you are probably looking for is django.db.backends.%s\n" |
| 49 | "Error was: %s" |
| 50 | ) % (backend_name, backend_name, e_user) |
| 51 | raise ImproperlyConfigured(error_msg) |
43 | 52 | else: |
44 | 53 | raise # If there's some other error, this must be an error in Django itself. |
45 | 54 | |
diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
index bda604e..d54c344 100644
a
|
b
|
|
7 | 7 | |
8 | 8 | from django.conf import settings |
9 | 9 | from django.core.management.color import no_style |
| 10 | from django.core.exceptions import ImproperlyConfigured |
10 | 11 | from django.db import (backend, connection, connections, DEFAULT_DB_ALIAS, |
11 | 12 | IntegrityError, transaction) |
12 | 13 | from django.db.backends.signals import connection_created |
13 | 14 | from django.db.backends.postgresql_psycopg2 import version as pg_version |
14 | | from django.db.utils import ConnectionHandler, DatabaseError |
| 15 | from django.db.utils import ConnectionHandler, DatabaseError, load_backend |
15 | 16 | from django.test import TestCase, skipUnlessDBFeature, TransactionTestCase |
16 | 17 | from django.utils import unittest |
17 | 18 | |
… |
… |
def runner2(other_thread_connection):
|
582 | 583 | t1.start() |
583 | 584 | t1.join() |
584 | 585 | # No exception was raised |
585 | | self.assertEqual(len(exceptions), 0) |
586 | | No newline at end of file |
| 586 | self.assertEqual(len(exceptions), 0) |
| 587 | |
| 588 | |
| 589 | class BackendLoadingTests(TestCase): |
| 590 | def test_old_style_backends_raise_useful_exception(self): |
| 591 | self.assertRaises(ImproperlyConfigured, load_backend, 'sqlite3') |
--
1.7.5.4
From 8975ef175b223431064d81be9d50d3a19f255922 Mon Sep 17 00:00:00 2001
From: Jonas Obrist <ojiidotch@gmail.com>
Date: Sun, 25 Dec 2011 14:07:58 +0100
Subject: [PATCH 2/2] improved variable names in my patch and the test case
---
django/db/utils.py | 6 +++---
tests/regressiontests/backends/tests.py | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/django/db/utils.py b/django/db/utils.py
index f5a6ac6..e2e31d9 100644
a
|
b
|
def load_backend(backend_name):
|
32 | 32 | and not f.startswith('.')] |
33 | 33 | except EnvironmentError: |
34 | 34 | available_backends = [] |
35 | | new_style_import = backend_name.startswith('django.db.backends.') |
36 | | if new_style_import: |
| 35 | new_style_backend_name = backend_name.startswith('django.db.backends.') |
| 36 | if new_style_backend_name: |
37 | 37 | backend_name = backend_name[19:] # See #15621. |
38 | 38 | if backend_name not in available_backends: |
39 | 39 | error_msg = ("%r isn't an available database backend. \n" + |
… |
… |
def load_backend(backend_name):
|
41 | 41 | "Error was: %s") % \ |
42 | 42 | (backend_name, ", ".join(map(repr, sorted(available_backends))), e_user) |
43 | 43 | raise ImproperlyConfigured(error_msg) |
44 | | elif not new_style_import: |
| 44 | elif not new_style_backend_name: |
45 | 45 | # user tried to use database backend using old-school non-qualified |
46 | 46 | # import path (eg 'sqlite3' instead of 'django.db.backends.sqlite3') |
47 | 47 | error_msg = ("%r isn't an available database backend.\n" |
diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
index d54c344..2fb6efc 100644
a
|
b
|
def runner2(other_thread_connection):
|
588 | 588 | |
589 | 589 | class BackendLoadingTests(TestCase): |
590 | 590 | def test_old_style_backends_raise_useful_exception(self): |
591 | | self.assertRaises(ImproperlyConfigured, load_backend, 'sqlite3') |
| 591 | self.assertRaisesRegexp(ImproperlyConfigured, "The backend you are probably looking for is django.db.backends.sqlite3", load_backend, 'sqlite3') |