1 from __future__
import absolute_import
3 from django
.db
.models
import Q
4 from django
.test
import TestCase
6 from .models
import (SystemDetails
, Item
, PropertyValue
, SystemInfo
, Forum
,
10 class NullFkTests(TestCase
):
12 def test_null_fk(self
):
13 d
= SystemDetails
.objects
.create(details
='First details')
14 s
= SystemInfo
.objects
.create(system_name
='First forum', system_details
=d
)
15 f
= Forum
.objects
.create(system_info
=s
, forum_name
='First forum')
16 p
= Post
.objects
.create(forum
=f
, title
='First Post')
17 c1
= Comment
.objects
.create(post
=p
, comment_text
='My first comment')
18 c2
= Comment
.objects
.create(comment_text
='My second comment')
20 # Starting from comment, make sure that a .select_related(...) with a specified
21 # set of fields will properly LEFT JOIN multiple levels of NULLs (and the things
22 # that come after the NULLs, or else data that should exist won't). Regression
24 c
= Comment
.objects
.select_related().get(id=c1
.id)
25 self
.assertEqual(c
.post
, p
)
26 self
.assertEqual(Comment
.objects
.select_related().get(id=c2
.id).post
, None)
28 self
.assertQuerysetEqual(
29 Comment
.objects
.select_related('post__forum__system_info').all(),
31 (c1
.id, u
'My first comment', '<Post: First Post>'),
32 (c2
.id, u
'My second comment', 'None')
34 transform
= lambda c
: (c
.id, c
.comment_text
, repr(c
.post
))
37 # Regression test for #7530, #7716.
38 self
.assertTrue(Comment
.objects
.select_related('post').filter(post__isnull
=True)[0].post
is None)
40 self
.assertQuerysetEqual(
41 Comment
.objects
.select_related('post__forum__system_info__system_details'),
43 (c1
.id, u
'My first comment', '<Post: First Post>'),
44 (c2
.id, u
'My second comment', 'None')
46 transform
= lambda c
: (c
.id, c
.comment_text
, repr(c
.post
))
49 def test_combine_isnull(self
):
50 item
= Item
.objects
.create(title
='Some Item')
51 pv
= PropertyValue
.objects
.create(label
='Some Value')
52 item
.props
.create(key
='a', value
=pv
)
53 item
.props
.create(key
='b') # value=NULL
54 q1
= Q(props__key
='a', props__value
=pv
)
55 q2
= Q(props__key
='b', props__value__isnull
=True)
57 # Each of these individually should return the item.
58 self
.assertEqual(Item
.objects
.get(q1
), item
)
59 self
.assertEqual(Item
.objects
.get(q2
), item
)
61 # Logically, qs1 and qs2, and qs3 and qs4 should be the same.
62 qs1
= Item
.objects
.filter(q1
) & Item
.objects
.filter(q2
)
63 qs2
= Item
.objects
.filter(q2
) & Item
.objects
.filter(q1
)
64 qs3
= Item
.objects
.filter(q1
) | Item
.objects
.filter(q2
)
65 qs4
= Item
.objects
.filter(q2
) | Item
.objects
.filter(q1
)
67 # Regression test for #15823.
68 self
.assertEqual(list(qs1
), list(qs2
))
69 self
.assertEqual(list(qs3
), list(qs4
))