tests.py 3.99 KB
Newer Older
1
from __future__ import unicode_literals
2

3 4
from django.core.exceptions import FieldError
from django.test import TestCase
5
from django.utils import six
6

7
from .models import Article, Author
8 9 10 11


class CustomColumnsTests(TestCase):

12 13 14 15
    def setUp(self):
        self.a1 = Author.objects.create(first_name="John", last_name="Smith")
        self.a2 = Author.objects.create(first_name="Peter", last_name="Jones")
        self.authors = [self.a1, self.a2]
16

17 18
        self.article = Article.objects.create(headline="Django lets you build Web apps easily", primary_author=self.a1)
        self.article.authors = self.authors
19

20
    def test_query_all_available_authors(self):
21 22 23 24
        self.assertQuerysetEqual(
            Author.objects.all(), [
                "Peter Jones", "John Smith",
            ],
25
            six.text_type
26
        )
27 28 29 30 31 32 33 34

    def test_get_first_name(self):
        self.assertEqual(
            Author.objects.get(first_name__exact="John"),
            self.a1,
        )

    def test_filter_first_name(self):
35 36 37 38
        self.assertQuerysetEqual(
            Author.objects.filter(first_name__exact="John"), [
                "John Smith",
            ],
39
            six.text_type
40 41
        )

42
    def test_field_error(self):
Loic Bistuer's avatar
Loic Bistuer committed
43 44
        self.assertRaises(
            FieldError,
45 46 47
            lambda: Author.objects.filter(firstname__exact="John")
        )

48 49 50
    def test_attribute_error(self):
        with self.assertRaises(AttributeError):
            self.a1.firstname
51

52 53
        with self.assertRaises(AttributeError):
            self.a1.last
54

55
    def test_get_all_authors_for_an_article(self):
56
        self.assertQuerysetEqual(
57
            self.article.authors.all(), [
58 59 60
                "Peter Jones",
                "John Smith",
            ],
61
            six.text_type
62
        )
63 64

    def test_get_all_articles_for_an_author(self):
65
        self.assertQuerysetEqual(
66
            self.a1.article_set.all(), [
67
                "Django lets you build Web apps easily",
68 69 70
            ],
            lambda a: a.headline
        )
71 72

    def test_get_author_m2m_relation(self):
73
        self.assertQuerysetEqual(
74
            self.article.authors.filter(last_name='Jones'), [
75 76
                "Peter Jones"
            ],
77
            six.text_type
78
        )
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    def test_author_querying(self):
        self.assertQuerysetEqual(
            Author.objects.all().order_by('last_name'),
            ['<Author: Peter Jones>', '<Author: John Smith>']
        )

    def test_author_filtering(self):
        self.assertQuerysetEqual(
            Author.objects.filter(first_name__exact='John'),
            ['<Author: John Smith>']
        )

    def test_author_get(self):
        self.assertEqual(self.a1, Author.objects.get(first_name__exact='John'))

95
    def test_filter_on_nonexistent_field(self):
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        self.assertRaisesMessage(
            FieldError,
            "Cannot resolve keyword 'firstname' into field. Choices are: Author_ID, article, first_name, last_name, primary_set",
            Author.objects.filter,
            firstname__exact='John'
        )

    def test_author_get_attributes(self):
        a = Author.objects.get(last_name__exact='Smith')
        self.assertEqual('John', a.first_name)
        self.assertEqual('Smith', a.last_name)
        self.assertRaisesMessage(
            AttributeError,
            "'Author' object has no attribute 'firstname'",
            getattr,
            a, 'firstname'
        )

        self.assertRaisesMessage(
            AttributeError,
            "'Author' object has no attribute 'last'",
            getattr,
            a, 'last'
        )

    def test_m2m_table(self):
        self.assertQuerysetEqual(
            self.article.authors.all().order_by('last_name'),
            ['<Author: Peter Jones>', '<Author: John Smith>']
        )
        self.assertQuerysetEqual(
            self.a1.article_set.all(),
            ['<Article: Django lets you build Web apps easily>']
        )
        self.assertQuerysetEqual(
            self.article.authors.filter(last_name='Jones'),
            ['<Author: Peter Jones>']
        )