Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaned up tests for multiple databases.

  • Loading branch information...
commit 975337e5c348a770df5a1925ce256ab54cb8c529 1 parent f356b6e
@loic loic authored
Showing with 16 additions and 55 deletions.
  1. +16 −55 tests/multiple_database/tests.py
View
71 tests/multiple_database/tests.py
@@ -289,39 +289,25 @@ def test_m2m_cross_database_protection(self):
mark = Person.objects.using('other').create(name="Mark Pilgrim")
# Set a foreign key set with an object from a different database
- try:
- marty.book_set = [pro, dive]
- self.fail("Shouldn't be able to assign across databases")
- except ValueError:
- pass
+ with self.assertRaises(ValueError):
+ with transaction.atomic(using='default'):
+ marty.edited = [pro, dive]
# Add to an m2m with an object from a different database
- try:
+ with self.assertRaises(ValueError):
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
- try:
+ with self.assertRaises(ValueError):
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
- try:
+ with self.assertRaises(ValueError):
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
- try:
+ with self.assertRaises(ValueError):
dive.authors = [mark, marty]
- self.fail("Shouldn't be able to assign across databases")
- except ValueError:
- pass
def test_m2m_deletion(self):
"Cascaded deletions of m2m relations issue queries on the right database"
@@ -489,28 +475,18 @@ def test_foreign_key_cross_database_protection(self):
mark = Person.objects.using('other').create(name="Mark Pilgrim")
# Set a foreign key with an object from a different database
- try:
- with transaction.atomic(using='default'):
- dive.editor = marty
- self.fail("Shouldn't be able to assign across databases")
- except ValueError:
- pass
+ with self.assertRaises(ValueError):
+ dive.editor = marty
# Set a foreign key set with an object from a different database
- try:
+ with self.assertRaises(ValueError):
with transaction.atomic(using='default'):
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
- try:
+ with self.assertRaises(ValueError):
with transaction.atomic(using='default'):
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
# been saved yet, you implicitly assign the database for the
@@ -634,11 +610,8 @@ def test_o2o_cross_database_protection(self):
# Set a one-to-one relation with an object from a different database
alice_profile = UserProfile.objects.using('default').create(user=alice, flavor='chocolate')
- try:
+ with self.assertRaises(ValueError):
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
# been saved yet, you implicitly assign the database for the
@@ -784,18 +757,12 @@ def test_generic_key_cross_database_protection(self):
Review.objects.using('other').create(source="Python Weekly", content_object=dive)
# Set a foreign key with an object from a different database
- try:
+ with self.assertRaises(ValueError):
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
- try:
+ with self.assertRaises(ValueError):
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
# been saved yet, you implicitly assign the database for the
@@ -892,12 +859,9 @@ def test_subquery(self):
self.assertRaises(ValueError, str, qs.query)
# Evaluating the query shouldn't work, either
- try:
+ with self.assertRaises(ValueError):
for obj in qs:
pass
- self.fail('Iterating over query should raise ValueError')
- except ValueError:
- pass
def test_related_manager(self):
"Related managers return managers, not querysets"
@@ -1041,12 +1005,9 @@ def test_database_routing(self):
# An update query will be routed to the default database
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'
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.
pro = Book.objects.using('default').get(title='Pro Django')
Please sign in to comment.
Something went wrong with that request. Please try again.