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

« back to all changes in this revision

Viewing changes to tests/modeltests/or_lookups/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 datetime import datetime
4
 
from operator import attrgetter
5
 
 
6
 
from django.db.models import Q
7
 
from django.test import TestCase
8
 
 
9
 
from .models import Article
10
 
 
11
 
 
12
 
class OrLookupsTests(TestCase):
13
 
 
14
 
    def setUp(self):
15
 
        self.a1 = Article.objects.create(
16
 
            headline='Hello', pub_date=datetime(2005, 11, 27)
17
 
        ).pk
18
 
        self.a2 = Article.objects.create(
19
 
            headline='Goodbye', pub_date=datetime(2005, 11, 28)
20
 
        ).pk
21
 
        self.a3 = Article.objects.create(
22
 
            headline='Hello and goodbye', pub_date=datetime(2005, 11, 29)
23
 
        ).pk
24
 
 
25
 
    def test_filter_or(self):
26
 
        self.assertQuerysetEqual(
27
 
            Article.objects.filter(headline__startswith='Hello') |  Article.objects.filter(headline__startswith='Goodbye'), [
28
 
                'Hello',
29
 
                'Goodbye',
30
 
                'Hello and goodbye'
31
 
            ],
32
 
            attrgetter("headline")
33
 
        )
34
 
 
35
 
        self.assertQuerysetEqual(
36
 
            Article.objects.filter(headline__contains='Hello') | Article.objects.filter(headline__contains='bye'), [
37
 
                'Hello',
38
 
                'Goodbye',
39
 
                'Hello and goodbye'
40
 
            ],
41
 
            attrgetter("headline")
42
 
        )
43
 
 
44
 
        self.assertQuerysetEqual(
45
 
            Article.objects.filter(headline__iexact='Hello') | Article.objects.filter(headline__contains='ood'), [
46
 
                'Hello',
47
 
                'Goodbye',
48
 
                'Hello and goodbye'
49
 
            ],
50
 
            attrgetter("headline")
51
 
        )
52
 
 
53
 
        self.assertQuerysetEqual(
54
 
            Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__startswith='Goodbye')), [
55
 
                'Hello',
56
 
                'Goodbye',
57
 
                'Hello and goodbye'
58
 
            ],
59
 
            attrgetter("headline")
60
 
        )
61
 
 
62
 
 
63
 
    def test_stages(self):
64
 
        # You can shorten this syntax with code like the following,  which is
65
 
        # especially useful if building the query in stages:
66
 
        articles = Article.objects.all()
67
 
        self.assertQuerysetEqual(
68
 
            articles.filter(headline__startswith='Hello') & articles.filter(headline__startswith='Goodbye'),
69
 
            []
70
 
        )
71
 
        self.assertQuerysetEqual(
72
 
            articles.filter(headline__startswith='Hello') & articles.filter(headline__contains='bye'), [
73
 
                'Hello and goodbye'
74
 
            ],
75
 
            attrgetter("headline")
76
 
        )
77
 
 
78
 
    def test_pk_q(self):
79
 
        self.assertQuerysetEqual(
80
 
            Article.objects.filter(Q(pk=self.a1) | Q(pk=self.a2)), [
81
 
                'Hello',
82
 
                'Goodbye'
83
 
            ],
84
 
            attrgetter("headline")
85
 
        )
86
 
 
87
 
        self.assertQuerysetEqual(
88
 
            Article.objects.filter(Q(pk=self.a1) | Q(pk=self.a2) | Q(pk=self.a3)), [
89
 
                'Hello',
90
 
                'Goodbye',
91
 
                'Hello and goodbye'
92
 
            ],
93
 
            attrgetter("headline"),
94
 
        )
95
 
 
96
 
    def test_pk_in(self):
97
 
        self.assertQuerysetEqual(
98
 
            Article.objects.filter(pk__in=[self.a1, self.a2, self.a3]), [
99
 
                'Hello',
100
 
                'Goodbye',
101
 
                'Hello and goodbye'
102
 
            ],
103
 
            attrgetter("headline"),
104
 
        )
105
 
 
106
 
        self.assertQuerysetEqual(
107
 
            Article.objects.filter(pk__in=(self.a1, self.a2, self.a3)), [
108
 
                'Hello',
109
 
                'Goodbye',
110
 
                'Hello and goodbye'
111
 
            ],
112
 
            attrgetter("headline"),
113
 
        )
114
 
 
115
 
        self.assertQuerysetEqual(
116
 
            Article.objects.filter(pk__in=[self.a1, self.a2, self.a3, 40000]), [
117
 
                'Hello',
118
 
                'Goodbye',
119
 
                'Hello and goodbye'
120
 
            ],
121
 
            attrgetter("headline"),
122
 
        )
123
 
 
124
 
    def test_q_negated(self):
125
 
        # Q objects can be negated
126
 
        self.assertQuerysetEqual(
127
 
            Article.objects.filter(Q(pk=self.a1) | ~Q(pk=self.a2)), [
128
 
                'Hello',
129
 
                'Hello and goodbye'
130
 
            ],
131
 
            attrgetter("headline")
132
 
        )
133
 
 
134
 
        self.assertQuerysetEqual(
135
 
            Article.objects.filter(~Q(pk=self.a1) & ~Q(pk=self.a2)), [
136
 
                'Hello and goodbye'
137
 
            ],
138
 
            attrgetter("headline"),
139
 
        )
140
 
        # This allows for more complex queries than filter() and exclude()
141
 
        # alone would allow
142
 
        self.assertQuerysetEqual(
143
 
            Article.objects.filter(Q(pk=self.a1) & (~Q(pk=self.a2) | Q(pk=self.a3))), [
144
 
                'Hello'
145
 
            ],
146
 
            attrgetter("headline"),
147
 
        )
148
 
 
149
 
    def test_complex_filter(self):
150
 
        # The 'complex_filter' method supports framework features such as
151
 
        # 'limit_choices_to' which normally take a single dictionary of lookup
152
 
        # arguments but need to support arbitrary queries via Q objects too.
153
 
        self.assertQuerysetEqual(
154
 
            Article.objects.complex_filter({'pk': self.a1}), [
155
 
                'Hello'
156
 
            ],
157
 
            attrgetter("headline"),
158
 
        )
159
 
 
160
 
        self.assertQuerysetEqual(
161
 
            Article.objects.complex_filter(Q(pk=self.a1) | Q(pk=self.a2)), [
162
 
                'Hello',
163
 
                'Goodbye'
164
 
            ],
165
 
            attrgetter("headline"),
166
 
        )
167
 
 
168
 
    def test_empty_in(self):
169
 
        # Passing "in" an empty list returns no results ...
170
 
        self.assertQuerysetEqual(
171
 
            Article.objects.filter(pk__in=[]),
172
 
            []
173
 
        )
174
 
        # ... but can return results if we OR it with another query.
175
 
        self.assertQuerysetEqual(
176
 
            Article.objects.filter(Q(pk__in=[]) | Q(headline__icontains='goodbye')), [
177
 
                'Goodbye',
178
 
                'Hello and goodbye'
179
 
            ],
180
 
            attrgetter("headline"),
181
 
        )
182
 
 
183
 
    def test_q_and(self):
184
 
        # Q arg objects are ANDed
185
 
        self.assertQuerysetEqual(
186
 
            Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')), [
187
 
                'Hello and goodbye'
188
 
            ],
189
 
            attrgetter("headline")
190
 
        )
191
 
        # Q arg AND order is irrelevant
192
 
        self.assertQuerysetEqual(
193
 
            Article.objects.filter(Q(headline__contains='bye'), headline__startswith='Hello'), [
194
 
                'Hello and goodbye'
195
 
            ],
196
 
            attrgetter("headline"),
197
 
        )
198
 
 
199
 
        self.assertQuerysetEqual(
200
 
            Article.objects.filter(Q(headline__startswith='Hello') & Q(headline__startswith='Goodbye')),
201
 
            []
202
 
        )
203
 
 
204
 
    def test_q_exclude(self):
205
 
        self.assertQuerysetEqual(
206
 
            Article.objects.exclude(Q(headline__startswith='Hello')), [
207
 
                'Goodbye'
208
 
            ],
209
 
            attrgetter("headline")
210
 
        )
211
 
 
212
 
    def test_other_arg_queries(self):
213
 
        # Try some arg queries with operations other than filter.
214
 
        self.assertEqual(
215
 
            Article.objects.get(Q(headline__startswith='Hello'), Q(headline__contains='bye')).headline,
216
 
            'Hello and goodbye'
217
 
        )
218
 
 
219
 
        self.assertEqual(
220
 
            Article.objects.filter(Q(headline__startswith='Hello') | Q(headline__contains='bye')).count(),
221
 
            3
222
 
        )
223
 
 
224
 
        self.assertQuerysetEqual(
225
 
            Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye')).values(), [
226
 
                {"headline": "Hello and goodbye", "id": self.a3, "pub_date": datetime(2005, 11, 29)},
227
 
            ],
228
 
            lambda o: o,
229
 
        )
230
 
 
231
 
        self.assertEqual(
232
 
            Article.objects.filter(Q(headline__startswith='Hello')).in_bulk([self.a1, self.a2]),
233
 
            {self.a1: Article.objects.get(pk=self.a1)}
234
 
        )