PageRenderTime 207ms CodeModel.GetById 50ms app.highlight 52ms RepoModel.GetById 100ms app.codeStats 0ms

/tests/modeltests/custom_columns/tests.py

https://code.google.com/p/mango-py/
Python | 71 lines | 52 code | 11 blank | 8 comment | 0 complexity | 39eee4767330fa429214b22ffafe9e71 MD5 | raw file
 1from django.core.exceptions import FieldError
 2from django.test import TestCase
 3
 4from models import Author, Article
 5
 6
 7class CustomColumnsTests(TestCase):
 8    def test_db_column(self):
 9        a1 = Author.objects.create(first_name="John", last_name="Smith")
10        a2 = Author.objects.create(first_name="Peter", last_name="Jones")
11
12        art = Article.objects.create(headline="Django lets you build Web apps easily")
13        art.authors = [a1, a2]
14
15        # Although the table and column names on Author have been set to custom
16        # values, nothing about using the Author model has changed...
17
18        # Query the available authors
19        self.assertQuerysetEqual(
20            Author.objects.all(), [
21                "Peter Jones", "John Smith",
22            ],
23            unicode
24        )
25        self.assertQuerysetEqual(
26            Author.objects.filter(first_name__exact="John"), [
27                "John Smith",
28            ],
29            unicode
30        )
31        self.assertEqual(
32            Author.objects.get(first_name__exact="John"),
33            a1,
34        )
35
36        self.assertRaises(FieldError,
37            lambda: Author.objects.filter(firstname__exact="John")
38        )
39
40        a = Author.objects.get(last_name__exact="Smith")
41        a.first_name = "John"
42        a.last_name = "Smith"
43
44        self.assertRaises(AttributeError, lambda: a.firstname)
45        self.assertRaises(AttributeError, lambda: a.last)
46
47        # Although the Article table uses a custom m2m table,
48        # nothing about using the m2m relationship has changed...
49
50        # Get all the authors for an article
51        self.assertQuerysetEqual(
52            art.authors.all(), [
53                "Peter Jones",
54                "John Smith",
55            ],
56            unicode
57        )
58        # Get the articles for an author
59        self.assertQuerysetEqual(
60            a.article_set.all(), [
61                "Django lets you build Web apps easily",
62            ],
63            lambda a: a.headline
64        )
65        # Query the authors across the m2m relation
66        self.assertQuerysetEqual(
67            art.authors.filter(last_name='Jones'), [
68                "Peter Jones"
69            ],
70            unicode
71        )