Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed a regression in router initialization

Regression was introduced in 6a6bb16. Thanks Bas Peschier for the
report.
  • Loading branch information...
commit 7e95d7a93088675f984d1079e4e3c3ddc1723b0f 1 parent fbab320
@claudep claudep authored
Showing with 30 additions and 2 deletions.
  1. +3 −2 django/db/utils.py
  2. +27 −0 tests/multiple_database/tests.py
View
5 django/db/utils.py
@@ -224,13 +224,14 @@ def __init__(self, routers=None):
def routers(self):
if self._routers is None:
self._routers = settings.DATABASE_ROUTERS
+ routers = []
for r in self._routers:
if isinstance(r, six.string_types):
router = import_by_path(r)()
else:
router = r
- self._routers.append(router)
- return self._routers
+ routers.append(router)
+ return routers
def _router_func(action):
def _route_db(self, model, **hints):
View
27 tests/multiple_database/tests.py
@@ -5,11 +5,13 @@
from operator import attrgetter
import warnings
+from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.core import management
from django.db import connections, router, DEFAULT_DB_ALIAS
from django.db.models import signals
+from django.db.utils import ConnectionRouter
from django.test import TestCase
from django.test.utils import override_settings
from django.utils.six import StringIO
@@ -918,6 +920,7 @@ def test_related_manager(self):
published=datetime.date(2009, 5, 4),
extra_arg=True)
+
class TestRouter(object):
# A test router. The behavior is vaguely master/slave, but the
# databases aren't assumed to propagate changes.
@@ -972,6 +975,30 @@ class WriteRouter(object):
def db_for_write(self, model, **hints):
return 'writer'
+
+class ConnectionRouterTestCase(TestCase):
+ @override_settings(DATABASE_ROUTERS=[
+ 'multiple_database.tests.TestRouter',
+ 'multiple_database.tests.WriteRouter'])
+ def test_router_init_default(self):
+ router = ConnectionRouter()
+ self.assertListEqual([r.__class__.__name__ for r in router.routers],
+ ['TestRouter', 'WriteRouter'])
+
+ def test_router_init_arg(self):
+ router = ConnectionRouter([
+ 'multiple_database.tests.TestRouter',
+ 'multiple_database.tests.WriteRouter'
+ ])
+ self.assertListEqual([r.__class__.__name__ for r in router.routers],
+ ['TestRouter', 'WriteRouter'])
+
+ # Init with instances instead of strings
+ router = ConnectionRouter([TestRouter(), WriteRouter()])
+ self.assertListEqual([r.__class__.__name__ for r in router.routers],
+ ['TestRouter', 'WriteRouter'])
+
+
class RouterTestCase(TestCase):
multi_db = True
Please sign in to comment.
Something went wrong with that request. Please try again.