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

« back to all changes in this revision

Viewing changes to tests/custom_columns_regress/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.core.exceptions import FieldError
 
4
from django.test import TestCase
 
5
 
 
6
from .models import Author, Article
 
7
 
 
8
 
 
9
def pks(objects):
 
10
    """ Return pks to be able to compare lists"""
 
11
    return [o.pk for o in objects]
 
12
 
 
13
class CustomColumnRegression(TestCase):
 
14
 
 
15
    def setUp(self):
 
16
        self.a1 = Author.objects.create(first_name='John', last_name='Smith')
 
17
        self.a2 = Author.objects.create(first_name='Peter', last_name='Jones')
 
18
        self.authors = [self.a1, self.a2]
 
19
 
 
20
    def test_basic_creation(self):
 
21
        art = Article(headline='Django lets you build Web apps easily', primary_author=self.a1)
 
22
        art.save()
 
23
        art.authors = [self.a1, self.a2]
 
24
 
 
25
    def test_author_querying(self):
 
26
        self.assertQuerysetEqual(
 
27
            Author.objects.all().order_by('last_name'),
 
28
            ['<Author: Peter Jones>', '<Author: John Smith>']
 
29
        )
 
30
 
 
31
    def test_author_filtering(self):
 
32
        self.assertQuerysetEqual(
 
33
            Author.objects.filter(first_name__exact='John'),
 
34
            ['<Author: John Smith>']
 
35
        )
 
36
 
 
37
    def test_author_get(self):
 
38
        self.assertEqual(self.a1, Author.objects.get(first_name__exact='John'))
 
39
 
 
40
    def test_filter_on_nonexistant_field(self):
 
41
        self.assertRaisesMessage(
 
42
            FieldError,
 
43
            "Cannot resolve keyword 'firstname' into field. Choices are: Author_ID, article, first_name, last_name, primary_set",
 
44
            Author.objects.filter,
 
45
            firstname__exact='John'
 
46
        )
 
47
 
 
48
    def test_author_get_attributes(self):
 
49
        a = Author.objects.get(last_name__exact='Smith')
 
50
        self.assertEqual('John', a.first_name)
 
51
        self.assertEqual('Smith', a.last_name)
 
52
        self.assertRaisesMessage(
 
53
            AttributeError,
 
54
            "'Author' object has no attribute 'firstname'",
 
55
            getattr,
 
56
            a, 'firstname'
 
57
        )
 
58
 
 
59
        self.assertRaisesMessage(
 
60
            AttributeError,
 
61
            "'Author' object has no attribute 'last'",
 
62
            getattr,
 
63
            a, 'last'
 
64
        )
 
65
 
 
66
    def test_m2m_table(self):
 
67
        art = Article.objects.create(headline='Django lets you build Web apps easily', primary_author=self.a1)
 
68
        art.authors = self.authors
 
69
        self.assertQuerysetEqual(
 
70
            art.authors.all().order_by('last_name'),
 
71
            ['<Author: Peter Jones>', '<Author: John Smith>']
 
72
        )
 
73
        self.assertQuerysetEqual(
 
74
            self.a1.article_set.all(),
 
75
            ['<Article: Django lets you build Web apps easily>']
 
76
        )
 
77
        self.assertQuerysetEqual(
 
78
            art.authors.filter(last_name='Jones'),
 
79
            ['<Author: Peter Jones>']
 
80
        )