PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/tests/regressiontests/null_fk/tests.py

https://code.google.com/p/mango-py/
Python | 42 lines | 30 code | 7 blank | 5 comment | 0 complexity | 89261fd2856913af7b48f67690a23a98 MD5 | raw file
 1from django.test import TestCase
 2
 3from regressiontests.null_fk.models import *
 4
 5class NullFkTests(TestCase):
 6
 7    def test_null_fk(self):
 8        d = SystemDetails.objects.create(details='First details')
 9        s = SystemInfo.objects.create(system_name='First forum', system_details=d)
10        f = Forum.objects.create(system_info=s, forum_name='First forum')
11        p = Post.objects.create(forum=f, title='First Post')
12        c1 = Comment.objects.create(post=p, comment_text='My first comment')
13        c2 = Comment.objects.create(comment_text='My second comment')
14
15        # Starting from comment, make sure that a .select_related(...) with a specified
16        # set of fields will properly LEFT JOIN multiple levels of NULLs (and the things
17        # that come after the NULLs, or else data that should exist won't). Regression
18        # test for #7369.
19        c = Comment.objects.select_related().get(id=c1.id)
20        self.assertEqual(c.post, p)
21        self.assertEqual(Comment.objects.select_related().get(id=c2.id).post, None)
22
23        self.assertQuerysetEqual(
24            Comment.objects.select_related('post__forum__system_info').all(),
25            [
26                (c1.id, u'My first comment', '<Post: First Post>'),
27                (c2.id, u'My second comment', 'None')
28            ],
29            transform = lambda c: (c.id, c.comment_text, repr(c.post))
30        )
31
32        # Regression test for #7530, #7716.
33        self.assertTrue(Comment.objects.select_related('post').filter(post__isnull=True)[0].post is None)
34
35        self.assertQuerysetEqual(
36            Comment.objects.select_related('post__forum__system_info__system_details'),
37            [
38                (c1.id, u'My first comment', '<Post: First Post>'),
39                (c2.id, u'My second comment', 'None')
40            ],
41            transform = lambda c: (c.id, c.comment_text, repr(c.post))
42        )