mirror of
https://github.com/django/django.git
synced 2025-08-02 18:13:02 +00:00
Fixed #12252 -- Ensure that queryset unions are commutative. Thanks to benreynwar for the report, and draft patch, and to Karen and Ramiro for the review eyeballs and patch updates.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15726 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
d1290b5b43
commit
b7c41c1fbb
3 changed files with 98 additions and 6 deletions
|
@ -14,7 +14,8 @@ from django.utils.datastructures import SortedDict
|
|||
from models import (Annotation, Article, Author, Celebrity, Child, Cover, Detail,
|
||||
DumbCategory, ExtraInfo, Fan, Item, LeafA, LoopX, LoopZ, ManagedModel,
|
||||
Member, NamedCategory, Note, Number, Plaything, PointerA, Ranking, Related,
|
||||
Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Node)
|
||||
Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Node, ObjectA, ObjectB,
|
||||
ObjectC)
|
||||
|
||||
|
||||
class BaseQuerysetTest(TestCase):
|
||||
|
@ -1658,3 +1659,63 @@ class ConditionalTests(BaseQuerysetTest):
|
|||
Number.objects.filter(num__in=numbers).count(),
|
||||
2500
|
||||
)
|
||||
|
||||
class UnionTests(unittest.TestCase):
|
||||
"""
|
||||
Tests for the union of two querysets. Bug #12252.
|
||||
"""
|
||||
def setUp(self):
|
||||
objectas = []
|
||||
objectbs = []
|
||||
objectcs = []
|
||||
a_info = ['one', 'two', 'three']
|
||||
for name in a_info:
|
||||
o = ObjectA(name=name)
|
||||
o.save()
|
||||
objectas.append(o)
|
||||
b_info = [('un', 1, objectas[0]), ('deux', 2, objectas[0]), ('trois', 3, objectas[2])]
|
||||
for name, number, objecta in b_info:
|
||||
o = ObjectB(name=name, number=number, objecta=objecta)
|
||||
o.save()
|
||||
objectbs.append(o)
|
||||
c_info = [('ein', objectas[2], objectbs[2]), ('zwei', objectas[1], objectbs[1])]
|
||||
for name, objecta, objectb in c_info:
|
||||
o = ObjectC(name=name, objecta=objecta, objectb=objectb)
|
||||
o.save()
|
||||
objectcs.append(o)
|
||||
|
||||
def check_union(self, model, Q1, Q2):
|
||||
filter = model.objects.filter
|
||||
self.assertEqual(set(filter(Q1) | filter(Q2)), set(filter(Q1 | Q2)))
|
||||
self.assertEqual(set(filter(Q2) | filter(Q1)), set(filter(Q1 | Q2)))
|
||||
|
||||
def test_A_AB(self):
|
||||
Q1 = Q(name='two')
|
||||
Q2 = Q(objectb__name='deux')
|
||||
self.check_union(ObjectA, Q1, Q2)
|
||||
|
||||
def test_A_AB2(self):
|
||||
Q1 = Q(name='two')
|
||||
Q2 = Q(objectb__name='deux', objectb__number=2)
|
||||
self.check_union(ObjectA, Q1, Q2)
|
||||
|
||||
def test_AB_ACB(self):
|
||||
Q1 = Q(objectb__name='deux')
|
||||
Q2 = Q(objectc__objectb__name='deux')
|
||||
self.check_union(ObjectA, Q1, Q2)
|
||||
|
||||
def test_BAB_BAC(self):
|
||||
Q1 = Q(objecta__objectb__name='deux')
|
||||
Q2 = Q(objecta__objectc__name='ein')
|
||||
self.check_union(ObjectB, Q1, Q2)
|
||||
|
||||
def test_BAB_BACB(self):
|
||||
Q1 = Q(objecta__objectb__name='deux')
|
||||
Q2 = Q(objecta__objectc__objectb__name='trois')
|
||||
self.check_union(ObjectB, Q1, Q2)
|
||||
|
||||
def test_BA_BCA__BAB_BAC_BCA(self):
|
||||
Q1 = Q(objecta__name='one', objectc__objecta__name='two')
|
||||
Q2 = Q(objecta__objectc__name='ein', objectc__objecta__name='three', objecta__objectb__name='trois')
|
||||
self.check_union(ObjectB, Q1, Q2)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue