diff options
author | Hasan <hasan.r67@gmail.com> | 2016-01-17 14:56:39 +0330 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2016-01-29 12:32:18 -0500 |
commit | 3d0dcd7f5af378d3ab6adb303b913e6c7b2e0ee5 (patch) | |
tree | 0d1074cc65a72096e44a4165611fddfc5b7ef7fb /tests/get_object_or_404 | |
parent | 575706331bec4bf58ce36a9540c4c61fca49025b (diff) | |
download | django-3d0dcd7f5af378d3ab6adb303b913e6c7b2e0ee5.tar.gz |
Refs #26022 -- Used context manager version of assertRaises in tests.
Diffstat (limited to 'tests/get_object_or_404')
-rw-r--r-- | tests/get_object_or_404/tests.py | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/tests/get_object_or_404/tests.py b/tests/get_object_or_404/tests.py index 05bea661ed..55d1de7c2c 100644 --- a/tests/get_object_or_404/tests.py +++ b/tests/get_object_or_404/tests.py @@ -13,7 +13,8 @@ class GetObjectOr404Tests(TestCase): a2 = Author.objects.create(name="Patsy") # No Articles yet, so we should get a Http404 error. - self.assertRaises(Http404, get_object_or_404, Article, title="Foo") + with self.assertRaises(Http404): + get_object_or_404(Article, title="Foo") article = Article.objects.create(title="Run away!") article.authors.set([a1, a2]) @@ -30,10 +31,8 @@ class GetObjectOr404Tests(TestCase): ) # No articles containing "Camelot". This should raise a Http404 error. - self.assertRaises( - Http404, - get_object_or_404, a1.article_set, title__contains="Camelot" - ) + with self.assertRaises(Http404): + get_object_or_404(a1.article_set, title__contains="Camelot") # Custom managers can be used too. self.assertEqual( @@ -50,16 +49,12 @@ class GetObjectOr404Tests(TestCase): # Just as when using a get() lookup, you will get an error if more than # one object is returned. - self.assertRaises( - Author.MultipleObjectsReturned, - get_object_or_404, Author.objects.all() - ) + with self.assertRaises(Author.MultipleObjectsReturned): + get_object_or_404(Author.objects.all()) # Using an empty QuerySet raises a Http404 error. - self.assertRaises( - Http404, - get_object_or_404, Article.objects.none(), title__contains="Run" - ) + with self.assertRaises(Http404): + get_object_or_404(Article.objects.none(), title__contains="Run") # get_list_or_404 can be used to get lists of objects self.assertEqual( @@ -68,10 +63,8 @@ class GetObjectOr404Tests(TestCase): ) # Http404 is returned if the list is empty. - self.assertRaises( - Http404, - get_list_or_404, a1.article_set, title__icontains="Shrubbery" - ) + with self.assertRaises(Http404): + get_list_or_404(a1.article_set, title__icontains="Shrubbery") # Custom managers can be used too. self.assertEqual( |