(self)
| 6 | |
| 7 | class NullFkTests(TestCase): |
| 8 | def test_null_fk(self): |
| 9 | d = SystemDetails.objects.create(details="First details") |
| 10 | s = SystemInfo.objects.create(system_name="First forum", system_details=d) |
| 11 | f = Forum.objects.create(system_info=s, forum_name="First forum") |
| 12 | p = Post.objects.create(forum=f, title="First Post") |
| 13 | c1 = Comment.objects.create(post=p, comment_text="My first comment") |
| 14 | c2 = Comment.objects.create(comment_text="My second comment") |
| 15 | |
| 16 | # Starting from comment, make sure that a .select_related(...) with a |
| 17 | # specified set of fields will properly LEFT JOIN multiple levels of |
| 18 | # NULLs (and the things that come after the NULLs, or else data that |
| 19 | # should exist won't). Regression test for #7369. |
| 20 | c = Comment.objects.select_related("post").get(id=c1.id) |
| 21 | self.assertEqual(c.post, p) |
| 22 | self.assertIsNone(Comment.objects.select_related("post").get(id=c2.id).post) |
| 23 | |
| 24 | self.assertQuerySetEqual( |
| 25 | Comment.objects.select_related("post__forum__system_info").all(), |
| 26 | [ |
| 27 | (c1.id, "My first comment", "<Post: First Post>"), |
| 28 | (c2.id, "My second comment", "None"), |
| 29 | ], |
| 30 | transform=lambda c: (c.id, c.comment_text, repr(c.post)), |
| 31 | ) |
| 32 | |
| 33 | # Regression test for #7530, #7716. |
| 34 | self.assertIsNone( |
| 35 | Comment.objects.select_related("post").filter(post__isnull=True)[0].post |
| 36 | ) |
| 37 | |
| 38 | self.assertQuerySetEqual( |
| 39 | Comment.objects.select_related("post__forum__system_info__system_details"), |
| 40 | [ |
| 41 | (c1.id, "My first comment", "<Post: First Post>"), |
| 42 | (c2.id, "My second comment", "None"), |
| 43 | ], |
| 44 | transform=lambda c: (c.id, c.comment_text, repr(c.post)), |
| 45 | ) |
| 46 | |
| 47 | def test_combine_isnull(self): |
| 48 | item = Item.objects.create(title="Some Item") |
nothing calls this directly
no test coverage detected