Fixed a regression in router initialization

Regression was introduced in 6a6bb168b. Thanks Bas Peschier for the
report.
This commit is contained in:
Claude Paroz 2013-05-24 20:45:03 +02:00
parent fbab3209fc
commit 7e95d7a930
2 changed files with 30 additions and 2 deletions

View File

@ -224,13 +224,14 @@ class ConnectionRouter(object):
def routers(self): def routers(self):
if self._routers is None: if self._routers is None:
self._routers = settings.DATABASE_ROUTERS self._routers = settings.DATABASE_ROUTERS
routers = []
for r in self._routers: for r in self._routers:
if isinstance(r, six.string_types): if isinstance(r, six.string_types):
router = import_by_path(r)() router = import_by_path(r)()
else: else:
router = r router = r
self._routers.append(router) routers.append(router)
return self._routers return routers
def _router_func(action): def _router_func(action):
def _route_db(self, model, **hints): def _route_db(self, model, **hints):

View File

@ -5,11 +5,13 @@ import pickle
from operator import attrgetter from operator import attrgetter
import warnings import warnings
from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core import management from django.core import management
from django.db import connections, router, DEFAULT_DB_ALIAS from django.db import connections, router, DEFAULT_DB_ALIAS
from django.db.models import signals from django.db.models import signals
from django.db.utils import ConnectionRouter
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings from django.test.utils import override_settings
from django.utils.six import StringIO from django.utils.six import StringIO
@ -918,6 +920,7 @@ class QueryTestCase(TestCase):
published=datetime.date(2009, 5, 4), published=datetime.date(2009, 5, 4),
extra_arg=True) extra_arg=True)
class TestRouter(object): class TestRouter(object):
# A test router. The behavior is vaguely master/slave, but the # A test router. The behavior is vaguely master/slave, but the
# databases aren't assumed to propagate changes. # databases aren't assumed to propagate changes.
@ -972,6 +975,30 @@ class WriteRouter(object):
def db_for_write(self, model, **hints): def db_for_write(self, model, **hints):
return 'writer' 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): class RouterTestCase(TestCase):
multi_db = True multi_db = True