Ticket #17047: 17047.patch

File 17047.patch, 5.3 KB (added by Jonas Obrist, 12 years ago)

patch and tests

  • django/db/utils.py

    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):  
    3232                    and not f.startswith('.')]
    3333        except EnvironmentError:
    3434            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:
    3637            backend_name = backend_name[19:] # See #15621.
    3738        if backend_name not in available_backends:
    3839            error_msg = ("%r isn't an available database backend. \n" +
    def load_backend(backend_name):  
    4041                "Error was: %s") % \
    4142                (backend_name, ", ".join(map(repr, sorted(available_backends))), e_user)
    4243            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)
    4352        else:
    4453            raise # If there's some other error, this must be an error in Django itself.
    4554
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index bda604e..d54c344 100644
    a b  
    77
    88from django.conf import settings
    99from django.core.management.color import no_style
     10from django.core.exceptions import ImproperlyConfigured
    1011from django.db import (backend, connection, connections, DEFAULT_DB_ALIAS,
    1112    IntegrityError, transaction)
    1213from django.db.backends.signals import connection_created
    1314from django.db.backends.postgresql_psycopg2 import version as pg_version
    14 from django.db.utils import ConnectionHandler, DatabaseError
     15from django.db.utils import ConnectionHandler, DatabaseError, load_backend
    1516from django.test import TestCase, skipUnlessDBFeature, TransactionTestCase
    1617from django.utils import unittest
    1718
    def runner2(other_thread_connection):  
    582583        t1.start()
    583584        t1.join()
    584585        # 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
     589class BackendLoadingTests(TestCase):
     590    def test_old_style_backends_raise_useful_exception(self):
     591        self.assertRaises(ImproperlyConfigured, load_backend, 'sqlite3')
  • django/db/utils.py

    -- 
    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):  
    3232                    and not f.startswith('.')]
    3333        except EnvironmentError:
    3434            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:
    3737            backend_name = backend_name[19:] # See #15621.
    3838        if backend_name not in available_backends:
    3939            error_msg = ("%r isn't an available database backend. \n" +
    def load_backend(backend_name):  
    4141                "Error was: %s") % \
    4242                (backend_name, ", ".join(map(repr, sorted(available_backends))), e_user)
    4343            raise ImproperlyConfigured(error_msg)
    44         elif not new_style_import:
     44        elif not new_style_backend_name:
    4545            # user tried to use database backend using old-school non-qualified
    4646            # import path (eg 'sqlite3' instead of 'django.db.backends.sqlite3')
    4747            error_msg = ("%r isn't an available database backend.\n"
  • tests/regressiontests/backends/tests.py

    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):  
    588588
    589589class BackendLoadingTests(TestCase):
    590590    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')
Back to Top