diff options
author | Dražen Odobašić <dodobas@candela-it.com> | 2015-09-11 19:33:12 -0400 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2015-09-12 11:40:50 -0400 |
commit | b1e33ceceda1e75ff68c7deed8f6659683a195d3 (patch) | |
tree | e4e446f69194f2dc3c9c7ee3ecf48290ea8d4d31 /tests/multiple_database | |
parent | 84b0a8d2aad042fb573df5055b6153770d0929ac (diff) | |
download | django-b1e33ceceda1e75ff68c7deed8f6659683a195d3.tar.gz |
Fixed #23395 -- Limited line lengths to 119 characters.
Diffstat (limited to 'tests/multiple_database')
-rw-r--r-- | tests/multiple_database/tests.py | 360 |
1 files changed, 251 insertions, 109 deletions
diff --git a/tests/multiple_database/tests.py b/tests/multiple_database/tests.py index 4354217d57..fd7748e9f9 100644 --- a/tests/multiple_database/tests.py +++ b/tests/multiple_database/tests.py @@ -188,15 +188,23 @@ class QueryTestCase(TestCase): self.assertEqual(Book.authors.through.objects.using('other').count(), 1) # Check that queries work across m2m joins - self.assertEqual(list(Book.objects.using('default').filter(authors__name='Marty Alchin').values_list('title', flat=True)), - ['Pro Django']) - self.assertEqual(list(Book.objects.using('other').filter(authors__name='Marty Alchin').values_list('title', flat=True)), - []) - - self.assertEqual(list(Book.objects.using('default').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), - []) - self.assertEqual(list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), - ['Dive into Python']) + self.assertEqual( + list(Book.objects.using('default').filter(authors__name='Marty Alchin').values_list('title', flat=True)), + ['Pro Django'] + ) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='Marty Alchin').values_list('title', flat=True)), + [] + ) + + self.assertEqual( + list(Book.objects.using('default').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), + [] + ) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), + ['Dive into Python'] + ) # Reget the objects to clear caches dive = Book.objects.using('other').get(title="Dive into Python") @@ -222,35 +230,53 @@ class QueryTestCase(TestCase): # Add a second author john = Person.objects.using('other').create(name="John Smith") - self.assertEqual(list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)), - []) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)), + [] + ) dive.authors.add(john) - self.assertEqual(list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), - ['Dive into Python']) - self.assertEqual(list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)), - ['Dive into Python']) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), + ['Dive into Python'] + ) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)), + ['Dive into Python'] + ) # Remove the second author dive.authors.remove(john) - self.assertEqual(list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), - ['Dive into Python']) - self.assertEqual(list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)), - []) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), + ['Dive into Python'] + ) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)), + [] + ) # Clear all authors dive.authors.clear() - self.assertEqual(list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), - []) - self.assertEqual(list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)), - []) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), + [] + ) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='John Smith').values_list('title', flat=True)), + [] + ) # Create an author through the m2m interface dive.authors.create(name='Jane Brown') - self.assertEqual(list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), - []) - self.assertEqual(list(Book.objects.using('other').filter(authors__name='Jane Brown').values_list('title', flat=True)), - ['Dive into Python']) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='Mark Pilgrim').values_list('title', flat=True)), + [] + ) + self.assertEqual( + list(Book.objects.using('other').filter(authors__name='Jane Brown').values_list('title', flat=True)), + ['Dive into Python'] + ) def test_m2m_reverse_operations(self): "M2M reverse manipulations are all constrained to a single DB" @@ -269,31 +295,53 @@ class QueryTestCase(TestCase): # Add a books to the m2m mark.book_set.add(grease) - self.assertEqual(list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)), - ['Mark Pilgrim']) - self.assertEqual(list(Person.objects.using('other').filter(book__title='Greasemonkey Hacks').values_list('name', flat=True)), - ['Mark Pilgrim']) + self.assertEqual( + list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)), + ['Mark Pilgrim'] + ) + self.assertEqual( + list( + Person.objects.using('other').filter(book__title='Greasemonkey Hacks').values_list('name', flat=True) + ), + ['Mark Pilgrim'] + ) # Remove a book from the m2m mark.book_set.remove(grease) - self.assertEqual(list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)), - ['Mark Pilgrim']) - self.assertEqual(list(Person.objects.using('other').filter(book__title='Greasemonkey Hacks').values_list('name', flat=True)), - []) + self.assertEqual( + list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)), + ['Mark Pilgrim'] + ) + self.assertEqual( + list( + Person.objects.using('other').filter(book__title='Greasemonkey Hacks').values_list('name', flat=True) + ), + [] + ) # Clear the books associated with mark mark.book_set.clear() - self.assertEqual(list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)), - []) - self.assertEqual(list(Person.objects.using('other').filter(book__title='Greasemonkey Hacks').values_list('name', flat=True)), - []) + self.assertEqual( + list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)), + [] + ) + self.assertEqual( + list( + Person.objects.using('other').filter(book__title='Greasemonkey Hacks').values_list('name', flat=True) + ), + [] + ) # Create a book through the m2m interface mark.book_set.create(title="Dive into HTML5", published=datetime.date(2020, 1, 1)) - self.assertEqual(list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)), - []) - self.assertEqual(list(Person.objects.using('other').filter(book__title='Dive into HTML5').values_list('name', flat=True)), - ['Mark Pilgrim']) + self.assertEqual( + list(Person.objects.using('other').filter(book__title='Dive into Python').values_list('name', flat=True)), + [] + ) + self.assertEqual( + list(Person.objects.using('other').filter(book__title='Dive into HTML5').values_list('name', flat=True)), + ['Mark Pilgrim'] + ) def test_m2m_cross_database_protection(self): "Operations that involve sharing M2M objects across databases raise an error" @@ -421,15 +469,27 @@ class QueryTestCase(TestCase): self.assertEqual(dive.editor.name, "Chris Mills") # Check that queries work across foreign key joins - self.assertEqual(list(Person.objects.using('default').filter(edited__title='Pro Django').values_list('name', flat=True)), - ['George Vilches']) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Pro Django').values_list('name', flat=True)), - []) + self.assertEqual( + list(Person.objects.using('default').filter(edited__title='Pro Django').values_list('name', flat=True)), + ['George Vilches'] + ) + self.assertEqual( + list(Person.objects.using('other').filter(edited__title='Pro Django').values_list('name', flat=True)), + [] + ) - self.assertEqual(list(Person.objects.using('default').filter(edited__title='Dive into Python').values_list('name', flat=True)), - []) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True)), - ['Chris Mills']) + self.assertEqual( + list( + Person.objects.using('default').filter(edited__title='Dive into Python').values_list('name', flat=True) + ), + [] + ) + self.assertEqual( + list( + Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True) + ), + ['Chris Mills'] + ) # Reget the objects to clear caches chris = Person.objects.using('other').get(name="Chris Mills") @@ -452,37 +512,65 @@ class QueryTestCase(TestCase): # Add a second book edited by chris html5 = Book.objects.using('other').create(title="Dive into HTML5", published=datetime.date(2010, 3, 15)) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), - []) + self.assertEqual( + list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), + [] + ) chris.edited.add(html5) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), - ['Chris Mills']) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True)), - ['Chris Mills']) + self.assertEqual( + list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), + ['Chris Mills'] + ) + self.assertEqual( + list( + Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True) + ), + ['Chris Mills'] + ) # Remove the second editor chris.edited.remove(html5) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), - []) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True)), - ['Chris Mills']) + self.assertEqual( + list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), + [] + ) + self.assertEqual( + list( + Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True) + ), + ['Chris Mills'] + ) # Clear all edited books chris.edited.clear() - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), - []) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True)), - []) + self.assertEqual( + list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), + [] + ) + self.assertEqual( + list( + Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True) + ), + [] + ) # Create an author through the m2m interface chris.edited.create(title='Dive into Water', published=datetime.date(2010, 3, 15)) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), - []) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Water').values_list('name', flat=True)), - ['Chris Mills']) - self.assertEqual(list(Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True)), - []) + self.assertEqual( + list(Person.objects.using('other').filter(edited__title='Dive into HTML5').values_list('name', flat=True)), + [] + ) + self.assertEqual( + list(Person.objects.using('other').filter(edited__title='Dive into Water').values_list('name', flat=True)), + ['Chris Mills'] + ) + self.assertEqual( + list( + Person.objects.using('other').filter(edited__title='Dive into Python').values_list('name', flat=True) + ), + [] + ) def test_foreign_key_cross_database_protection(self): "Operations that involve sharing FK objects across databases raise an error" @@ -556,15 +644,35 @@ class QueryTestCase(TestCase): self.assertEqual(bob.userprofile.flavor, "crunchy frog") # Check that queries work across joins - self.assertEqual(list(User.objects.using('default').filter(userprofile__flavor='chocolate').values_list('username', flat=True)), - ['alice']) - self.assertEqual(list(User.objects.using('other').filter(userprofile__flavor='chocolate').values_list('username', flat=True)), - []) + self.assertEqual( + list( + User.objects.using('default') + .filter(userprofile__flavor='chocolate').values_list('username', flat=True) + ), + ['alice'] + ) + self.assertEqual( + list( + User.objects.using('other') + .filter(userprofile__flavor='chocolate').values_list('username', flat=True) + ), + [] + ) - self.assertEqual(list(User.objects.using('default').filter(userprofile__flavor='crunchy frog').values_list('username', flat=True)), - []) - self.assertEqual(list(User.objects.using('other').filter(userprofile__flavor='crunchy frog').values_list('username', flat=True)), - ['bob']) + self.assertEqual( + list( + User.objects.using('default') + .filter(userprofile__flavor='crunchy frog').values_list('username', flat=True) + ), + [] + ) + self.assertEqual( + list( + User.objects.using('other') + .filter(userprofile__flavor='crunchy frog').values_list('username', flat=True) + ), + ['bob'] + ) # Reget the objects to clear caches alice_profile = UserProfile.objects.using('default').get(flavor='chocolate') @@ -683,38 +791,58 @@ class QueryTestCase(TestCase): review1 = Review.objects.using('other').create(source="Python Weekly", content_object=dive) review2 = Review.objects.using('other').create(source="Python Monthly", content_object=temp) - self.assertEqual(list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), - []) - self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), - ['Python Weekly']) + self.assertEqual( + list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), + [] + ) + self.assertEqual( + list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), + ['Python Weekly'] + ) # Add a second review dive.reviews.add(review2) - self.assertEqual(list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), - []) - self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), - ['Python Monthly', 'Python Weekly']) + self.assertEqual( + list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), + [] + ) + self.assertEqual( + list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), + ['Python Monthly', 'Python Weekly'] + ) # Remove the second author dive.reviews.remove(review1) - self.assertEqual(list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), - []) - self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), - ['Python Monthly']) + self.assertEqual( + list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), + [] + ) + self.assertEqual( + list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), + ['Python Monthly'] + ) # Clear all reviews dive.reviews.clear() - self.assertEqual(list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), - []) - self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), - []) + self.assertEqual( + list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), + [] + ) + self.assertEqual( + list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), + [] + ) # Create an author through the generic interface dive.reviews.create(source='Python Daily') - self.assertEqual(list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), - []) - self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), - ['Python Daily']) + self.assertEqual( + list(Review.objects.using('default').filter(object_id=dive.pk).values_list('source', flat=True)), + [] + ) + self.assertEqual( + list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), + ['Python Daily'] + ) def test_generic_key_cross_database_protection(self): "Operations that involve sharing generic key objects across databases raise an error" @@ -750,17 +878,25 @@ class QueryTestCase(TestCase): review3.content_object = dive self.assertEqual(review3._state.db, 'other') # ... but it isn't saved yet - self.assertEqual(list(Review.objects.using('default').filter(object_id=pro.pk).values_list('source', flat=True)), - ['Python Monthly']) - self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), - ['Python Weekly']) + self.assertEqual( + list(Review.objects.using('default').filter(object_id=pro.pk).values_list('source', flat=True)), + ['Python Monthly'] + ) + self.assertEqual( + list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), + ['Python Weekly'] + ) # When saved, John goes to 'other' review3.save() - self.assertEqual(list(Review.objects.using('default').filter(object_id=pro.pk).values_list('source', flat=True)), - ['Python Monthly']) - self.assertEqual(list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), - ['Python Daily', 'Python Weekly']) + self.assertEqual( + list(Review.objects.using('default').filter(object_id=pro.pk).values_list('source', flat=True)), + ['Python Monthly'] + ) + self.assertEqual( + list(Review.objects.using('other').filter(object_id=dive.pk).values_list('source', flat=True)), + ['Python Daily', 'Python Weekly'] + ) def test_generic_key_deletion(self): "Cascaded deletions of Generic Key relations issue queries on the right database" @@ -1178,7 +1314,10 @@ class RouterTestCase(TestCase): self.assertEqual(cheesecake._state.db, 'default') # Same goes for get_or_create, regardless of whether getting or creating - cheesecake, created = mark.edited.get_or_create(title='Dive into Cheesecake', published=datetime.date(2010, 3, 15)) + cheesecake, created = mark.edited.get_or_create( + title='Dive into Cheesecake', + published=datetime.date(2010, 3, 15), + ) self.assertEqual(cheesecake._state.db, 'default') puddles, created = mark.edited.get_or_create(title='Dive into Puddles', published=datetime.date(2010, 3, 15)) @@ -1589,7 +1728,10 @@ class FixtureTestCase(TestCase): @override_settings(DATABASE_ROUTERS=[AntiPetRouter()]) def test_pseudo_empty_fixtures(self): - "A fixture can contain entries, but lead to nothing in the database; this shouldn't raise an error (ref #14068)" + """ + A fixture can contain entries, but lead to nothing in the database; + this shouldn't raise an error (#14068). + """ new_io = StringIO() management.call_command('loaddata', 'pets', stdout=new_io, stderr=new_io) command_output = new_io.getvalue().strip() |