summaryrefslogtreecommitdiff
path: root/tests/many_to_one
diff options
context:
space:
mode:
authorHasan <hasan.r67@gmail.com>2016-01-17 14:56:39 +0330
committerTim Graham <timograham@gmail.com>2016-01-29 12:32:18 -0500
commit3d0dcd7f5af378d3ab6adb303b913e6c7b2e0ee5 (patch)
tree0d1074cc65a72096e44a4165611fddfc5b7ef7fb /tests/many_to_one
parent575706331bec4bf58ce36a9540c4c61fca49025b (diff)
downloaddjango-3d0dcd7f5af378d3ab6adb303b913e6c7b2e0ee5.tar.gz
Refs #26022 -- Used context manager version of assertRaises in tests.
Diffstat (limited to 'tests/many_to_one')
-rw-r--r--tests/many_to_one/tests.py22
1 files changed, 14 insertions, 8 deletions
diff --git a/tests/many_to_one/tests.py b/tests/many_to_one/tests.py
index 79caec7177..692a3e15e4 100644
--- a/tests/many_to_one/tests.py
+++ b/tests/many_to_one/tests.py
@@ -455,8 +455,8 @@ class ManyToOneTests(TestCase):
self.assertEqual(a3.reporter.id, self.r2.id)
# Get should respect explicit foreign keys as well.
- self.assertRaises(MultipleObjectsReturned,
- Article.objects.get, reporter_id=self.r.id)
+ with self.assertRaises(MultipleObjectsReturned):
+ Article.objects.get(reporter_id=self.r.id)
self.assertEqual(repr(a3),
repr(Article.objects.get(reporter_id=self.r2.id,
pub_date=datetime.date(2011, 5, 7))))
@@ -537,15 +537,19 @@ class ManyToOneTests(TestCase):
self.assertIsNone(p.bestchild)
# Assigning None fails: Child.parent is null=False.
- self.assertRaises(ValueError, setattr, c, "parent", None)
+ with self.assertRaises(ValueError):
+ setattr(c, "parent", None)
# You also can't assign an object of the wrong type here
- self.assertRaises(ValueError, setattr, c, "parent", First(id=1, second=1))
+ with self.assertRaises(ValueError):
+ setattr(c, "parent", First(id=1, second=1))
# Nor can you explicitly assign None to Child.parent during object
# creation (regression for #9649).
- self.assertRaises(ValueError, Child, name='xyzzy', parent=None)
- self.assertRaises(ValueError, Child.objects.create, name='xyzzy', parent=None)
+ with self.assertRaises(ValueError):
+ Child(name='xyzzy', parent=None)
+ with self.assertRaises(ValueError):
+ Child.objects.create(name='xyzzy', parent=None)
# Creation using keyword argument should cache the related object.
p = Parent.objects.get(name="Parent")
@@ -602,7 +606,8 @@ class ManyToOneTests(TestCase):
p = Parent.objects.create(name="Parent")
c = Child.objects.create(name="Child", parent=p)
- self.assertRaises(ValueError, Child.objects.create, name="Grandchild", parent=c)
+ with self.assertRaises(ValueError):
+ Child.objects.create(name="Grandchild", parent=c)
def test_fk_instantiation_outside_model(self):
# Regression for #12190 -- Should be able to instantiate a FK outside
@@ -650,7 +655,8 @@ class ManyToOneTests(TestCase):
School.objects.use_for_related_fields = True
try:
private_student = Student.objects.get(pk=private_student.pk)
- self.assertRaises(School.DoesNotExist, lambda: private_student.school)
+ with self.assertRaises(School.DoesNotExist):
+ private_student.school
finally:
School.objects.use_for_related_fields = False