diff options
Diffstat (limited to 'tests/get_object_or_404/tests.py')
| -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( |
