PageRenderTime 138ms CodeModel.GetById 55ms app.highlight 6ms RepoModel.GetById 74ms app.codeStats 0ms

/tests/modeltests/get_object_or_404/tests.py

https://code.google.com/p/mango-py/
Python | 80 lines | 51 code | 16 blank | 13 comment | 0 complexity | 3685f1b238aa946b26f80e75347e114e MD5 | raw file
 1from django.http import Http404
 2from django.shortcuts import get_object_or_404, get_list_or_404
 3from django.test import TestCase
 4
 5from models import Author, Article
 6
 7
 8class GetObjectOr404Tests(TestCase):
 9    def test_get_object_or_404(self):
10        a1 = Author.objects.create(name="Brave Sir Robin")
11        a2 = Author.objects.create(name="Patsy")
12
13        # No Articles yet, so we should get a Http404 error.
14        self.assertRaises(Http404, get_object_or_404, Article, title="Foo")
15
16        article = Article.objects.create(title="Run away!")
17        article.authors = [a1, a2]
18        # get_object_or_404 can be passed a Model to query.
19        self.assertEqual(
20            get_object_or_404(Article, title__contains="Run"),
21            article
22        )
23
24        # We can also use the Article manager through an Author object.
25        self.assertEqual(
26            get_object_or_404(a1.article_set, title__contains="Run"),
27            article
28        )
29
30        # No articles containing "Camelot".  This should raise a Http404 error.
31        self.assertRaises(Http404,
32            get_object_or_404, a1.article_set, title__contains="Camelot"
33        )
34
35        # Custom managers can be used too.
36        self.assertEqual(
37            get_object_or_404(Article.by_a_sir, title="Run away!"),
38            article
39        )
40
41        # QuerySets can be used too.
42        self.assertEqual(
43            get_object_or_404(Article.objects.all(), title__contains="Run"),
44            article
45        )
46
47        # Just as when using a get() lookup, you will get an error if more than
48        # one object is returned.
49
50        self.assertRaises(Author.MultipleObjectsReturned,
51            get_object_or_404, Author.objects.all()
52        )
53
54        # Using an EmptyQuerySet raises a Http404 error.
55        self.assertRaises(Http404,
56            get_object_or_404, Article.objects.none(), title__contains="Run"
57        )
58
59        # get_list_or_404 can be used to get lists of objects
60        self.assertEqual(
61            get_list_or_404(a1.article_set, title__icontains="Run"),
62            [article]
63        )
64
65        # Http404 is returned if the list is empty.
66        self.assertRaises(Http404,
67            get_list_or_404, a1.article_set, title__icontains="Shrubbery"
68        )
69
70        # Custom managers can be used too.
71        self.assertEqual(
72            get_list_or_404(Article.by_a_sir, title__icontains="Run"),
73            [article]
74        )
75
76        # QuerySets can be used too.
77        self.assertEqual(
78            get_list_or_404(Article.objects.all(), title__icontains="Run"),
79            [article]
80        )