From a026e480da89cb99c9dc6c954fb5a37ded0f9315 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Thu, 22 Nov 2012 20:09:40 +0100 Subject: Fixed #16039 -- Made post_syncdb handlers multi-db aware. Also reverted 8fb7a9002669fb7ba7bec7df90b465b92e1ed3c2. Refs #17055. --- tests/regressiontests/multiple_database/tests.py | 55 ++++++++++++++---------- 1 file changed, 33 insertions(+), 22 deletions(-) (limited to 'tests/regressiontests/multiple_database') diff --git a/tests/regressiontests/multiple_database/tests.py b/tests/regressiontests/multiple_database/tests.py index 79fe6beebd..a1d9e93524 100644 --- a/tests/regressiontests/multiple_database/tests.py +++ b/tests/regressiontests/multiple_database/tests.py @@ -16,16 +16,6 @@ from django.utils.six import StringIO from .models import Book, Person, Pet, Review, UserProfile -def copy_content_types_from_default_to_other(): - # On post_syncdb, content types are created in the 'default' database. - # However, tests of generic foreign keys require them in 'other' too. - # The problem is masked on backends that defer constraints checks: at the - # end of each test, there's a rollback, and constraints are never checked. - # It only appears on MySQL + InnoDB. - for ct in ContentType.objects.using('default').all(): - ct.save(using='other') - - class QueryTestCase(TestCase): multi_db = True @@ -705,8 +695,6 @@ class QueryTestCase(TestCase): def test_generic_key_separation(self): "Generic fields are constrained to a single database" - copy_content_types_from_default_to_other() - # Create a book and author on the default database pro = Book.objects.create(title="Pro Django", published=datetime.date(2008, 12, 16)) @@ -734,8 +722,6 @@ class QueryTestCase(TestCase): def test_generic_key_reverse_operations(self): "Generic reverse manipulations are all constrained to a single DB" - copy_content_types_from_default_to_other() - dive = Book.objects.using('other').create(title="Dive into Python", published=datetime.date(2009, 5, 4)) @@ -780,8 +766,6 @@ class QueryTestCase(TestCase): def test_generic_key_cross_database_protection(self): "Operations that involve sharing generic key objects across databases raise an error" - copy_content_types_from_default_to_other() - # Create a book and author on the default database pro = Book.objects.create(title="Pro Django", published=datetime.date(2008, 12, 16)) @@ -833,8 +817,6 @@ class QueryTestCase(TestCase): def test_generic_key_deletion(self): "Cascaded deletions of Generic Key relations issue queries on the right database" - copy_content_types_from_default_to_other() - dive = Book.objects.using('other').create(title="Dive into Python", published=datetime.date(2009, 5, 4)) review = Review.objects.using('other').create(source="Python Weekly", content_object=dive) @@ -1402,8 +1384,6 @@ class RouterTestCase(TestCase): def test_generic_key_cross_database_protection(self): "Generic Key operations can span databases if they share a source" - copy_content_types_from_default_to_other() - # Create a book and author on the default database pro = Book.objects.using('default' ).create(title="Pro Django", published=datetime.date(2008, 12, 16)) @@ -1515,8 +1495,6 @@ class RouterTestCase(TestCase): def test_generic_key_managers(self): "Generic key relations are represented by managers, and can be controlled like managers" - copy_content_types_from_default_to_other() - pro = Book.objects.using('other').create(title="Pro Django", published=datetime.date(2008, 12, 16)) @@ -1922,3 +1900,36 @@ class RouterModelArgumentTestCase(TestCase): pet = Pet.objects.create(owner=person, name='Wart') # test related FK collection person.delete() + + +class SyncOnlyDefaultDatabaseRouter(object): + def allow_syncdb(self, db, model): + return db == DEFAULT_DB_ALIAS + + +class SyncDBTestCase(TestCase): + multi_db = True + + def test_syncdb_to_other_database(self): + """Regression test for #16039: syncdb with --database option.""" + count = ContentType.objects.count() + self.assertGreater(count, 0) + + ContentType.objects.using('other').delete() + management.call_command('syncdb', verbosity=0, interactive=False, + load_initial_data=False, database='other') + + self.assertEqual(ContentType.objects.using("other").count(), count) + + def test_syncdb_to_other_database_with_router(self): + """Regression test for #16039: syncdb with --database option.""" + ContentType.objects.using('other').delete() + try: + old_routers = router.routers + router.routers = [SyncOnlyDefaultDatabaseRouter()] + management.call_command('syncdb', verbosity=0, interactive=False, + load_initial_data=False, database='other') + finally: + router.routers = old_routers + + self.assertEqual(ContentType.objects.using("other").count(), 0) -- cgit v1.3