From 67df1a5002f4cb136403f124d3db52d6fc805eb8 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 28 Sep 2010 08:17:36 +0000 Subject: [PATCH] Migrated null_fk doctests. Thanks to Stephan Jaekel. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13934 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/null_fk/models.py | 34 -------------------- tests/regressiontests/null_fk/tests.py | 42 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 tests/regressiontests/null_fk/tests.py diff --git a/tests/regressiontests/null_fk/models.py b/tests/regressiontests/null_fk/models.py index 1f191daaad..3cce319496 100644 --- a/tests/regressiontests/null_fk/models.py +++ b/tests/regressiontests/null_fk/models.py @@ -31,37 +31,3 @@ class Comment(models.Model): def __unicode__(self): return self.comment_text - -__test__ = {'API_TESTS':""" - ->>> d = SystemDetails.objects.create(details='First details') ->>> s = SystemInfo.objects.create(system_name='First forum', system_details=d) ->>> f = Forum.objects.create(system_info=s, forum_name='First forum') ->>> p = Post.objects.create(forum=f, title='First Post') ->>> c1 = Comment.objects.create(post=p, comment_text='My first comment') ->>> c2 = Comment.objects.create(comment_text='My second comment') - -# Starting from comment, make sure that a .select_related(...) with a specified -# set of fields will properly LEFT JOIN multiple levels of NULLs (and the things -# that come after the NULLs, or else data that should exist won't). Regression -# test for #7369. ->>> c = Comment.objects.select_related().get(id=1) ->>> c.post - ->>> c = Comment.objects.select_related().get(id=2) ->>> print c.post -None - ->>> comments = Comment.objects.select_related('post__forum__system_info').all() ->>> [(c.id, c.comment_text, c.post) for c in comments] -[(1, u'My first comment', ), (2, u'My second comment', None)] - -# Regression test for #7530, #7716. ->>> Comment.objects.select_related('post').filter(post__isnull=True)[0].post is None -True - ->>> comments = Comment.objects.select_related('post__forum__system_info__system_details') ->>> [(c.id, c.comment_text, c.post) for c in comments] -[(1, u'My first comment', ), (2, u'My second comment', None)] - -"""} diff --git a/tests/regressiontests/null_fk/tests.py b/tests/regressiontests/null_fk/tests.py new file mode 100644 index 0000000000..449f3438a4 --- /dev/null +++ b/tests/regressiontests/null_fk/tests.py @@ -0,0 +1,42 @@ +from django.test import TestCase + +from regressiontests.null_fk.models import * + +class NullFkTests(TestCase): + + def test_null_fk(self): + d = SystemDetails.objects.create(details='First details') + s = SystemInfo.objects.create(system_name='First forum', system_details=d) + f = Forum.objects.create(system_info=s, forum_name='First forum') + p = Post.objects.create(forum=f, title='First Post') + c1 = Comment.objects.create(post=p, comment_text='My first comment') + c2 = Comment.objects.create(comment_text='My second comment') + + # Starting from comment, make sure that a .select_related(...) with a specified + # set of fields will properly LEFT JOIN multiple levels of NULLs (and the things + # that come after the NULLs, or else data that should exist won't). Regression + # test for #7369. + c = Comment.objects.select_related().get(id=1) + self.assertEquals(c.post, p) + self.assertEquals(Comment.objects.select_related().get(id=2).post, None) + + self.assertQuerysetEqual( + Comment.objects.select_related('post__forum__system_info').all(), + [ + (1, u'My first comment', ''), + (2, u'My second comment', 'None') + ], + transform = lambda c: (c.id, c.comment_text, repr(c.post)) + ) + + # Regression test for #7530, #7716. + self.assertTrue(Comment.objects.select_related('post').filter(post__isnull=True)[0].post is None) + + self.assertQuerysetEqual( + Comment.objects.select_related('post__forum__system_info__system_details'), + [ + (1, u'My first comment', ''), + (2, u'My second comment', 'None') + ], + transform = lambda c: (c.id, c.comment_text, repr(c.post)) + )