diff options
| author | Antonis Christofides <antonis@wikical.com> | 2013-11-24 18:12:46 +0100 |
|---|---|---|
| committer | Loic Bistuer <loic.bistuer@gmail.com> | 2014-05-29 23:01:55 +0700 |
| commit | 62f9508ade5233fc7bf8af5b3afcd2f5b57d8288 (patch) | |
| tree | a897162c21381e21f9554210b664495db7cf6cd0 /tests/contenttypes_tests/tests.py | |
| parent | d240b29c086f434c87c2a5be7af539ceb8c0f55f (diff) | |
Fixed #20401 -- ContentTypeManager.get_for_model reads from db_for_read.
Thanks Simon Charette and Tim Graham for the reviews.
Diffstat (limited to 'tests/contenttypes_tests/tests.py')
| -rw-r--r-- | tests/contenttypes_tests/tests.py | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/tests/contenttypes_tests/tests.py b/tests/contenttypes_tests/tests.py index 0f260a0b9b..18c542bf4b 100644 --- a/tests/contenttypes_tests/tests.py +++ b/tests/contenttypes_tests/tests.py @@ -7,7 +7,7 @@ from django.contrib.contenttypes.fields import ( ) from django.contrib.contenttypes.models import ContentType from django.core import checks -from django.db import models +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 @@ -335,3 +335,40 @@ class GenericRelationshipTests(IsolatedModelsTestCase): ) ] self.assertEqual(errors, expected) + + +class TestRouter(object): + def db_for_read(self, model, **hints): + return 'other' + + def db_for_write(self, model, **hints): + return 'default' + + +class ContentTypesMultidbTestCase(TestCase): + + def setUp(self): + self.old_routers = router.routers + router.routers = [TestRouter()] + + # 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 later when we query + # it, but in that case some database backends may automatically perform + # extra queries upon connecting (notably mysql executes + # "SET SQL_AUTO_IS_NULL = 0"), which will affect assertNumQueries(). + connections['other'].ensure_connection() + + 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'), \ + self.assertNumQueries(1, using='other'): + ContentType.objects.get_for_model(Author) |
