~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/modeltests/distinct_on_fields/tests.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from __future__ import absolute_import
2
 
 
3
 
from django.db.models import Max
4
 
from django.test import TestCase, skipUnlessDBFeature
5
 
from django.test.utils import str_prefix
6
 
 
7
 
from .models import Tag, Celebrity, Fan, Staff, StaffTag
8
 
 
9
 
class DistinctOnTests(TestCase):
10
 
    def setUp(self):
11
 
        t1 = Tag.objects.create(name='t1')
12
 
        t2 = Tag.objects.create(name='t2', parent=t1)
13
 
        t3 = Tag.objects.create(name='t3', parent=t1)
14
 
        t4 = Tag.objects.create(name='t4', parent=t3)
15
 
        t5 = Tag.objects.create(name='t5', parent=t3)
16
 
 
17
 
        p1_o1 = Staff.objects.create(id=1, name="p1", organisation="o1")
18
 
        p2_o1 = Staff.objects.create(id=2, name="p2", organisation="o1")
19
 
        p3_o1 = Staff.objects.create(id=3, name="p3", organisation="o1")
20
 
        p1_o2 = Staff.objects.create(id=4, name="p1", organisation="o2")
21
 
        p1_o1.coworkers.add(p2_o1, p3_o1)
22
 
        StaffTag.objects.create(staff=p1_o1, tag=t1)
23
 
        StaffTag.objects.create(staff=p1_o1, tag=t1)
24
 
 
25
 
        celeb1 = Celebrity.objects.create(name="c1")
26
 
        celeb2 = Celebrity.objects.create(name="c2")
27
 
 
28
 
        self.fan1 = Fan.objects.create(fan_of=celeb1)
29
 
        self.fan2 = Fan.objects.create(fan_of=celeb1)
30
 
        self.fan3 = Fan.objects.create(fan_of=celeb2)
31
 
 
32
 
    @skipUnlessDBFeature('can_distinct_on_fields')
33
 
    def test_basic_distinct_on(self):
34
 
        """QuerySet.distinct('field', ...) works"""
35
 
        # (qset, expected) tuples
36
 
        qsets = (
37
 
            (
38
 
                Staff.objects.distinct().order_by('name'),
39
 
                ['<Staff: p1>', '<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
40
 
            ),
41
 
            (
42
 
                Staff.objects.distinct('name').order_by('name'),
43
 
                ['<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
44
 
            ),
45
 
            (
46
 
                Staff.objects.distinct('organisation').order_by('organisation', 'name'),
47
 
                ['<Staff: p1>', '<Staff: p1>'],
48
 
            ),
49
 
            (
50
 
                Staff.objects.distinct('name', 'organisation').order_by('name', 'organisation'),
51
 
                ['<Staff: p1>', '<Staff: p1>', '<Staff: p2>', '<Staff: p3>'],
52
 
            ),
53
 
            (
54
 
                Celebrity.objects.filter(fan__in=[self.fan1, self.fan2, self.fan3]).\
55
 
                    distinct('name').order_by('name'),
56
 
                ['<Celebrity: c1>', '<Celebrity: c2>'],
57
 
            ),
58
 
            # Does combining querysets work?
59
 
            (
60
 
                (Celebrity.objects.filter(fan__in=[self.fan1, self.fan2]).\
61
 
                    distinct('name').order_by('name')
62
 
                |Celebrity.objects.filter(fan__in=[self.fan3]).\
63
 
                    distinct('name').order_by('name')),
64
 
                ['<Celebrity: c1>', '<Celebrity: c2>'],
65
 
            ),
66
 
            (
67
 
                StaffTag.objects.distinct('staff','tag'),
68
 
                ['<StaffTag: t1 -> p1>'],
69
 
            ),
70
 
            (
71
 
                Tag.objects.order_by('parent__pk', 'pk').distinct('parent'),
72
 
                ['<Tag: t2>', '<Tag: t4>', '<Tag: t1>'],
73
 
            ),
74
 
            (
75
 
                StaffTag.objects.select_related('staff').distinct('staff__name').order_by('staff__name'),
76
 
                ['<StaffTag: t1 -> p1>'],
77
 
            ),
78
 
            # Fetch the alphabetically first coworker for each worker
79
 
            (
80
 
                (Staff.objects.distinct('id').order_by('id', 'coworkers__name').
81
 
                               values_list('id', 'coworkers__name')),
82
 
                [str_prefix("(1, %(_)s'p2')"), str_prefix("(2, %(_)s'p1')"),
83
 
                 str_prefix("(3, %(_)s'p1')"), "(4, None)"]
84
 
            ),
85
 
        )
86
 
        for qset, expected in qsets:
87
 
            self.assertQuerysetEqual(qset, expected)
88
 
            self.assertEqual(qset.count(), len(expected))
89
 
 
90
 
        # Combining queries with different distinct_fields is not allowed.
91
 
        base_qs = Celebrity.objects.all()
92
 
        self.assertRaisesMessage(
93
 
            AssertionError,
94
 
            "Cannot combine queries with different distinct fields.",
95
 
            lambda: (base_qs.distinct('id') & base_qs.distinct('name'))
96
 
        )
97
 
 
98
 
        # Test join unreffing
99
 
        c1 = Celebrity.objects.distinct('greatest_fan__id', 'greatest_fan__fan_of')
100
 
        self.assertIn('OUTER JOIN', str(c1.query))
101
 
        c2 = c1.distinct('pk')
102
 
        self.assertNotIn('OUTER JOIN', str(c2.query))
103
 
 
104
 
    @skipUnlessDBFeature('can_distinct_on_fields')
105
 
    def test_distinct_not_implemented_checks(self):
106
 
        # distinct + annotate not allowed
107
 
        with self.assertRaises(NotImplementedError):
108
 
            Celebrity.objects.annotate(Max('id')).distinct('id')[0]
109
 
        with self.assertRaises(NotImplementedError):
110
 
            Celebrity.objects.distinct('id').annotate(Max('id'))[0]
111
 
 
112
 
        # However this check is done only when the query executes, so you
113
 
        # can use distinct() to remove the fields before execution.
114
 
        Celebrity.objects.distinct('id').annotate(Max('id')).distinct()[0]
115
 
        # distinct + aggregate not allowed
116
 
        with self.assertRaises(NotImplementedError):
117
 
            Celebrity.objects.distinct('id').aggregate(Max('id'))
118