Cleaned up tests for multiple databases.

This commit is contained in:
Loic Bistuer 2014-03-30 01:56:08 +07:00
parent f356b6ecc3
commit 975337e5c3
1 changed files with 16 additions and 55 deletions

View File

@ -289,39 +289,25 @@ class QueryTestCase(TestCase):
mark = Person.objects.using('other').create(name="Mark Pilgrim") mark = Person.objects.using('other').create(name="Mark Pilgrim")
# Set a foreign key set with an object from a different database # Set a foreign key set with an object from a different database
try: with self.assertRaises(ValueError):
marty.book_set = [pro, dive] with transaction.atomic(using='default'):
self.fail("Shouldn't be able to assign across databases") marty.edited = [pro, dive]
except ValueError:
pass
# Add to an m2m with an object from a different database # Add to an m2m with an object from a different database
try: with self.assertRaises(ValueError):
marty.book_set.add(dive) marty.book_set.add(dive)
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
# Set a m2m with an object from a different database # Set a m2m with an object from a different database
try: with self.assertRaises(ValueError):
marty.book_set = [pro, dive] marty.book_set = [pro, dive]
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
# Add to a reverse m2m with an object from a different database # Add to a reverse m2m with an object from a different database
try: with self.assertRaises(ValueError):
dive.authors.add(marty) dive.authors.add(marty)
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
# Set a reverse m2m with an object from a different database # Set a reverse m2m with an object from a different database
try: with self.assertRaises(ValueError):
dive.authors = [mark, marty] dive.authors = [mark, marty]
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
def test_m2m_deletion(self): def test_m2m_deletion(self):
"Cascaded deletions of m2m relations issue queries on the right database" "Cascaded deletions of m2m relations issue queries on the right database"
@ -489,28 +475,18 @@ class QueryTestCase(TestCase):
mark = Person.objects.using('other').create(name="Mark Pilgrim") mark = Person.objects.using('other').create(name="Mark Pilgrim")
# Set a foreign key with an object from a different database # Set a foreign key with an object from a different database
try: with self.assertRaises(ValueError):
with transaction.atomic(using='default'): dive.editor = marty
dive.editor = marty
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
# Set a foreign key set with an object from a different database # Set a foreign key set with an object from a different database
try: with self.assertRaises(ValueError):
with transaction.atomic(using='default'): with transaction.atomic(using='default'):
marty.edited = [pro, dive] marty.edited = [pro, dive]
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
# Add to a foreign key set with an object from a different database # Add to a foreign key set with an object from a different database
try: with self.assertRaises(ValueError):
with transaction.atomic(using='default'): with transaction.atomic(using='default'):
marty.edited.add(dive) marty.edited.add(dive)
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
# BUT! if you assign a FK object when the base object hasn't # BUT! if you assign a FK object when the base object hasn't
# been saved yet, you implicitly assign the database for the # been saved yet, you implicitly assign the database for the
@ -634,11 +610,8 @@ class QueryTestCase(TestCase):
# Set a one-to-one relation with an object from a different database # Set a one-to-one relation with an object from a different database
alice_profile = UserProfile.objects.using('default').create(user=alice, flavor='chocolate') alice_profile = UserProfile.objects.using('default').create(user=alice, flavor='chocolate')
try: with self.assertRaises(ValueError):
bob.userprofile = alice_profile bob.userprofile = alice_profile
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
# BUT! if you assign a FK object when the base object hasn't # BUT! if you assign a FK object when the base object hasn't
# been saved yet, you implicitly assign the database for the # been saved yet, you implicitly assign the database for the
@ -784,18 +757,12 @@ class QueryTestCase(TestCase):
Review.objects.using('other').create(source="Python Weekly", content_object=dive) Review.objects.using('other').create(source="Python Weekly", content_object=dive)
# Set a foreign key with an object from a different database # Set a foreign key with an object from a different database
try: with self.assertRaises(ValueError):
review1.content_object = dive review1.content_object = dive
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
# Add to a foreign key set with an object from a different database # Add to a foreign key set with an object from a different database
try: with self.assertRaises(ValueError):
dive.reviews.add(review1) dive.reviews.add(review1)
self.fail("Shouldn't be able to assign across databases")
except ValueError:
pass
# BUT! if you assign a FK object when the base object hasn't # BUT! if you assign a FK object when the base object hasn't
# been saved yet, you implicitly assign the database for the # been saved yet, you implicitly assign the database for the
@ -892,12 +859,9 @@ class QueryTestCase(TestCase):
self.assertRaises(ValueError, str, qs.query) self.assertRaises(ValueError, str, qs.query)
# Evaluating the query shouldn't work, either # Evaluating the query shouldn't work, either
try: with self.assertRaises(ValueError):
for obj in qs: for obj in qs:
pass pass
self.fail('Iterating over query should raise ValueError')
except ValueError:
pass
def test_related_manager(self): def test_related_manager(self):
"Related managers return managers, not querysets" "Related managers return managers, not querysets"
@ -1041,12 +1005,9 @@ class RouterTestCase(TestCase):
# An update query will be routed to the default database # An update query will be routed to the default database
Book.objects.filter(title='Pro Django').update(pages=200) Book.objects.filter(title='Pro Django').update(pages=200)
try: with self.assertRaises(Book.DoesNotExist):
# By default, the get query will be directed to 'other' # By default, the get query will be directed to 'other'
Book.objects.get(title='Pro Django') Book.objects.get(title='Pro Django')
self.fail("Shouldn't be able to find the book")
except Book.DoesNotExist:
pass
# But the same query issued explicitly at a database will work. # But the same query issued explicitly at a database will work.
pro = Book.objects.using('default').get(title='Pro Django') pro = Book.objects.using('default').get(title='Pro Django')