Skip to content

Instantly share code, notes, and snippets.

@loic
Created May 29, 2014 13:48
Show Gist options
  • Save loic/ba42ab22acf9c7d23f38 to your computer and use it in GitHub Desktop.
Save loic/ba42ab22acf9c7d23f38 to your computer and use it in GitHub Desktop.
diff --git a/tests/contenttypes_tests/tests.py b/tests/contenttypes_tests/tests.py
index 2d86dfa..9e5eaf5 100644
--- a/tests/contenttypes_tests/tests.py
+++ b/tests/contenttypes_tests/tests.py
@@ -7,12 +7,10 @@ from django.contrib.contenttypes.fields import (
)
from django.contrib.contenttypes.models import ContentType
from django.core import checks
-from django.db import models, router, connections
-from django.db.utils import ConnectionRouter
+from django.db import connections, models, router
from django.test import TestCase
from django.test.utils import override_settings
from django.utils.encoding import force_str
-from django.utils.functional import cached_property
from .models import Author, Article, SchemeIncludedURL
@@ -339,7 +337,7 @@ class GenericRelationshipTests(IsolatedModelsTestCase):
self.assertEqual(errors, expected)
-class MasterSlaveRouter(object):
+class TestRouter(object):
def db_for_read(self, model, **hints):
return 'other'
@@ -350,26 +348,6 @@ class MasterSlaveRouter(object):
class ContentTypesMultidbTestCase(TestCase):
def setUp(self):
- """
- We need to override the database routers; this is not possible with
- override_settings, because they are cached. The cache is not supposed
- to be overriden, hence the slightly complicated hack.
- """
- self.orig_routers = router._routers
- router._routers = ['contenttypes_tests.tests.MasterSlaveRouter']
- cached_property(ConnectionRouter.routers.func).__get__(router)
-
- def tearDown(self):
- router._routers = self.orig_routers
- cached_property(ConnectionRouter.routers.func).__get__(router)
-
- def test_multidb(self):
- """
- Test that, when using multiple databases, we use the db_for_read (see
- #20401).
- """
- ContentType.objects.clear_cache()
-
# Whenever a test starts executing, only the "default" database is
# connected. We explicitly connect to the "other" database here. If we
# don't do it, then it will be implicitly connected further below when
@@ -379,6 +357,18 @@ class ContentTypesMultidbTestCase(TestCase):
# assertNumQueries().
connections['other'].ensure_connection()
- with self.assertNumQueries(0, using='default'), \
- self.assertNumQueries(1, using='other'):
- ContentType.objects.get_for_model(Author)
+ self.old_routers = router.routers
+ router.routers = [TestRouter()]
+
+ def tearDown(self):
+ router.routers = self.old_routers
+
+ def test_multidb(self):
+ """
+ Test that, when using multiple databases, we use the db_for_read (see
+ #20401).
+ """
+ ContentType.objects.clear_cache()
+ with self.assertNumQueries(0, using='default'):
+ with self.assertNumQueries(1, using='other'):
+ ContentType.objects.get_for_model(Author)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment