~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/get_object_or_404/tests.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import absolute_import
 
2
 
 
3
from django.http import Http404
 
4
from django.shortcuts import get_object_or_404, get_list_or_404
 
5
from django.test import TestCase
 
6
 
 
7
from .models import Author, Article
 
8
 
 
9
 
 
10
class GetObjectOr404Tests(TestCase):
 
11
    def test_get_object_or_404(self):
 
12
        a1 = Author.objects.create(name="Brave Sir Robin")
 
13
        a2 = Author.objects.create(name="Patsy")
 
14
 
 
15
        # No Articles yet, so we should get a Http404 error.
 
16
        self.assertRaises(Http404, get_object_or_404, Article, title="Foo")
 
17
 
 
18
        article = Article.objects.create(title="Run away!")
 
19
        article.authors = [a1, a2]
 
20
        # get_object_or_404 can be passed a Model to query.
 
21
        self.assertEqual(
 
22
            get_object_or_404(Article, title__contains="Run"),
 
23
            article
 
24
        )
 
25
 
 
26
        # We can also use the Article manager through an Author object.
 
27
        self.assertEqual(
 
28
            get_object_or_404(a1.article_set, title__contains="Run"),
 
29
            article
 
30
        )
 
31
 
 
32
        # No articles containing "Camelot".  This should raise a Http404 error.
 
33
        self.assertRaises(Http404,
 
34
            get_object_or_404, a1.article_set, title__contains="Camelot"
 
35
        )
 
36
 
 
37
        # Custom managers can be used too.
 
38
        self.assertEqual(
 
39
            get_object_or_404(Article.by_a_sir, title="Run away!"),
 
40
            article
 
41
        )
 
42
 
 
43
        # QuerySets can be used too.
 
44
        self.assertEqual(
 
45
            get_object_or_404(Article.objects.all(), title__contains="Run"),
 
46
            article
 
47
        )
 
48
 
 
49
        # Just as when using a get() lookup, you will get an error if more than
 
50
        # one object is returned.
 
51
 
 
52
        self.assertRaises(Author.MultipleObjectsReturned,
 
53
            get_object_or_404, Author.objects.all()
 
54
        )
 
55
 
 
56
        # Using an empty QuerySet raises a Http404 error.
 
57
        self.assertRaises(Http404,
 
58
            get_object_or_404, Article.objects.none(), title__contains="Run"
 
59
        )
 
60
 
 
61
        # get_list_or_404 can be used to get lists of objects
 
62
        self.assertEqual(
 
63
            get_list_or_404(a1.article_set, title__icontains="Run"),
 
64
            [article]
 
65
        )
 
66
 
 
67
        # Http404 is returned if the list is empty.
 
68
        self.assertRaises(Http404,
 
69
            get_list_or_404, a1.article_set, title__icontains="Shrubbery"
 
70
        )
 
71
 
 
72
        # Custom managers can be used too.
 
73
        self.assertEqual(
 
74
            get_list_or_404(Article.by_a_sir, title__icontains="Run"),
 
75
            [article]
 
76
        )
 
77
 
 
78
        # QuerySets can be used too.
 
79
        self.assertEqual(
 
80
            get_list_or_404(Article.objects.all(), title__icontains="Run"),
 
81
            [article]
 
82
        )
 
83
 
 
84
    def test_bad_class(self):
 
85
        # Given an argument klass that is not a Model, Manager, or Queryset
 
86
        # raises a helpful ValueError message
 
87
        self.assertRaisesMessage(ValueError,
 
88
            "Object is of type 'str', but must be a Django Model, Manager, "
 
89
            "or QuerySet",
 
90
            get_object_or_404, "Article", title__icontains="Run"
 
91
        )
 
92
 
 
93
        class CustomClass(object):
 
94
            pass
 
95
 
 
96
        self.assertRaisesMessage(ValueError,
 
97
            "Object is of type 'CustomClass', but must be a Django Model, "
 
98
            "Manager, or QuerySet",
 
99
            get_object_or_404, CustomClass, title__icontains="Run"
 
100
        )
 
101
 
 
102
        # Works for lists too
 
103
        self.assertRaisesMessage(ValueError,
 
104
            "Object is of type 'list', but must be a Django Model, Manager, "
 
105
            "or QuerySet",
 
106
            get_list_or_404, [Article], title__icontains="Run"
 
107
        )