PageRenderTime 143ms CodeModel.GetById 137ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/custom_columns_regress/tests.py

https://code.google.com/p/mango-py/
Python | 84 lines | 70 code | 13 blank | 1 comment | 2 complexity | 2eacbbbe52779112c1da7629e4e33019 MD5 | raw file
 1from django.test import TestCase
 2from django.core.exceptions import FieldError
 3
 4from models import Author, Article
 5
 6def pks(objects):
 7    """ Return pks to be able to compare lists"""
 8    return [o.pk for o in objects]
 9
10class CustomColumnRegression(TestCase):
11
12    def assertRaisesMessage(self, exc, msg, func, *args, **kwargs):
13        try:
14            func(*args, **kwargs)
15        except Exception, e:
16            self.assertEqual(msg, str(e))
17            self.assertTrue(isinstance(e, exc), "Expected %s, got %s" % (exc, type(e)))
18
19    def setUp(self):
20        self.a1 = Author.objects.create(first_name='John', last_name='Smith')
21        self.a2 = Author.objects.create(first_name='Peter', last_name='Jones')
22        self.authors = [self.a1, self.a2]
23
24    def test_basic_creation(self):
25        art = Article(headline='Django lets you build Web apps easily', primary_author=self.a1)
26        art.save()
27        art.authors = [self.a1, self.a2]
28
29    def test_author_querying(self):
30        self.assertQuerysetEqual(
31            Author.objects.all().order_by('last_name'),
32            ['<Author: Peter Jones>', '<Author: John Smith>']
33        )
34
35    def test_author_filtering(self):
36        self.assertQuerysetEqual(
37            Author.objects.filter(first_name__exact='John'),
38            ['<Author: John Smith>']
39        )
40
41    def test_author_get(self):
42        self.assertEqual(self.a1, Author.objects.get(first_name__exact='John'))
43
44    def test_filter_on_nonexistant_field(self):
45        self.assertRaisesMessage(
46            FieldError,
47            "Cannot resolve keyword 'firstname' into field. Choices are: Author_ID, article, first_name, last_name, primary_set",
48            Author.objects.filter,
49            firstname__exact='John'
50        )
51
52    def test_author_get_attributes(self):
53        a = Author.objects.get(last_name__exact='Smith')
54        self.assertEqual('John', a.first_name)
55        self.assertEqual('Smith', a.last_name)
56        self.assertRaisesMessage(
57            AttributeError,
58            "'Author' object has no attribute 'firstname'",
59            getattr,
60            a, 'firstname'
61        )
62
63        self.assertRaisesMessage(
64            AttributeError,
65            "'Author' object has no attribute 'last'",
66            getattr,
67            a, 'last'
68        )
69
70    def test_m2m_table(self):
71        art = Article.objects.create(headline='Django lets you build Web apps easily', primary_author=self.a1)
72        art.authors = self.authors
73        self.assertQuerysetEqual(
74            art.authors.all().order_by('last_name'),
75            ['<Author: Peter Jones>', '<Author: John Smith>']
76        )
77        self.assertQuerysetEqual(
78            self.a1.article_set.all(),
79            ['<Article: Django lets you build Web apps easily>']
80        )
81        self.assertQuerysetEqual(
82            art.authors.filter(last_name='Jones'),
83            ['<Author: Peter Jones>']
84        )