Debian Codegraph @

tests/foreign_object/test_tuple_lookups.py

raw
1 import itertools
2
3 from django.db.models import F
4 from django.db.models.fields.tuple_lookups import (
5 TupleExact,
6 TupleGreaterThan,
7 TupleGreaterThanOrEqual,
8 TupleIn,
9 TupleIsNull,
10 TupleLessThan,
11 TupleLessThanOrEqual,
12 )
13 from django.db.models.lookups import In
14 from django.test import TestCase, skipUnlessDBFeature
15
16 from .models import Contact, Customer
17
18
19 class TupleLookupsTests(TestCase):
20 @classmethod
21 def setUpTestData(cls):
22 super().setUpTestData()
23 cls.customer_1 = Customer.objects.create(customer_id=1, company="a")
24 cls.customer_2 = Customer.objects.create(customer_id=1, company="b")
25 cls.customer_3 = Customer.objects.create(customer_id=2, company="c")
26 cls.customer_4 = Customer.objects.create(customer_id=3, company="d")
27 cls.customer_5 = Customer.objects.create(customer_id=1, company="e")
28 cls.contact_1 = Contact.objects.create(customer=cls.customer_1)
29 cls.contact_2 = Contact.objects.create(customer=cls.customer_1)
30 cls.contact_3 = Contact.objects.create(customer=cls.customer_2)
31 cls.contact_4 = Contact.objects.create(customer=cls.customer_3)
32 cls.contact_5 = Contact.objects.create(customer=cls.customer_1)
33 cls.contact_6 = Contact.objects.create(customer=cls.customer_5)
34
35 def test_exact(self):
36 test_cases = (
37 (self.customer_1, (self.contact_1, self.contact_2, self.contact_5)),
38 (self.customer_2, (self.contact_3,)),
39 (self.customer_3, (self.contact_4,)),
40 (self.customer_4, ()),
41 (self.customer_5, (self.contact_6,)),
42 )
43
44 for customer, contacts in test_cases:
45 with self.subTest(
46 "filter(customer=customer)",
47 customer=customer,
48 contacts=contacts,
49 ):
50 self.assertSequenceEqual(
51 Contact.objects.filter(customer=customer).order_by("id"), contacts
52 )
53 with self.subTest(
54 "filter(TupleExact)",
55 customer=customer,
56 contacts=contacts,
57 ):
58 lhs = (F("customer_code"), F("company_code"))
59 rhs = (customer.customer_id, customer.company)
60 lookup = TupleExact(lhs, rhs)
61 self.assertSequenceEqual(
62 Contact.objects.filter(lookup).order_by("id"), contacts
63 )
64
65 def test_exact_subquery(self):
66 msg = (
67 "The QuerySet value for the exact lookup must have 2 selected "
68 "fields (received 1)"
69 )
70 with self.assertRaisesMessage(ValueError, msg):
71 subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
72 self.assertSequenceEqual(
73 Contact.objects.filter(customer=subquery).order_by("id"), ()
74 )
75
76 def test_in(self):
77 cust_1, cust_2, cust_3, cust_4, cust_5 = (
78 self.customer_1,
79 self.customer_2,
80 self.customer_3,
81 self.customer_4,
82 self.customer_5,
83 )
84 c1, c2, c3, c4, c5, c6 = (
85 self.contact_1,
86 self.contact_2,
87 self.contact_3,
88 self.contact_4,
89 self.contact_5,
90 self.contact_6,
91 )
92 test_cases = (
93 ((), ()),
94 ((cust_1,), (c1, c2, c5)),
95 ((cust_1, cust_2), (c1, c2, c3, c5)),
96 ((cust_1, cust_2, cust_3), (c1, c2, c3, c4, c5)),
97 ((cust_1, cust_2, cust_3, cust_4), (c1, c2, c3, c4, c5)),
98 ((cust_1, cust_2, cust_3, cust_4, cust_5), (c1, c2, c3, c4, c5, c6)),
99 )
100
101 for customers, contacts in test_cases:
102 with self.subTest(
103 "filter(customer__in=customers)",
104 customers=customers,
105 contacts=contacts,
106 ):
107 self.assertSequenceEqual(
108 Contact.objects.filter(customer__in=customers).order_by("id"),
109 contacts,
110 )
111 with self.subTest(
112 "filter(TupleIn)",
113 customers=customers,
114 contacts=contacts,
115 ):
116 lhs = (F("customer_code"), F("company_code"))
117 rhs = [(c.customer_id, c.company) for c in customers]
118 lookup = TupleIn(lhs, rhs)
119 self.assertSequenceEqual(
120 Contact.objects.filter(lookup).order_by("id"), contacts
121 )
122
123 @skipUnlessDBFeature("allow_sliced_subqueries_with_in")
124 def test_in_subquery(self):
125 subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
126 self.assertSequenceEqual(
127 Contact.objects.filter(customer__in=subquery).order_by("id"),
128 (self.contact_1, self.contact_2, self.contact_5),
129 )
130
131 def test_tuple_in_subquery_must_be_query(self):
132 lhs = (F("customer_code"), F("company_code"))
133 # If rhs is any non-Query object with an as_sql() function.
134 rhs = In(F("customer_code"), [1, 2, 3])
135 with self.assertRaisesMessage(
136 ValueError,
137 "'in' subquery lookup of ('customer_code', 'company_code') "
138 "must be a Query object (received 'In')",
139 ):
140 TupleIn(lhs, rhs)
141
142 def test_tuple_in_subquery_must_have_2_fields(self):
143 lhs = (F("customer_code"), F("company_code"))
144 rhs = Customer.objects.values_list("customer_id").query
145 msg = (
146 "The QuerySet value for the 'in' lookup must have 2 selected "
147 "fields (received 1)"
148 )
149 with self.assertRaisesMessage(ValueError, msg):
150 TupleIn(lhs, rhs)
151
152 def test_tuple_in_subquery(self):
153 customers = Customer.objects.values_list("customer_id", "company")
154 test_cases = (
155 (self.customer_1, (self.contact_1, self.contact_2, self.contact_5)),
156 (self.customer_2, (self.contact_3,)),
157 (self.customer_3, (self.contact_4,)),
158 (self.customer_4, ()),
159 (self.customer_5, (self.contact_6,)),
160 )
161
162 for customer, contacts in test_cases:
163 lhs = (F("customer_code"), F("company_code"))
164 rhs = customers.filter(id=customer.id).query
165 lookup = TupleIn(lhs, rhs)
166 qs = Contact.objects.filter(lookup).order_by("id")
167
168 with self.subTest(customer=customer.id, query=str(qs.query)):
169 self.assertSequenceEqual(qs, contacts)
170
171 def test_tuple_in_rhs_must_be_collection_of_tuples_or_lists(self):
172 test_cases = (
173 (1, 2, 3),
174 ((1, 2), (3, 4), None),
175 )
176
177 for rhs in test_cases:
178 with self.subTest(rhs=rhs):
179 with self.assertRaisesMessage(
180 ValueError,
181 "'in' lookup of ('customer_code', 'company_code') "
182 "must be a collection of tuples or lists",
183 ):
184 TupleIn((F("customer_code"), F("company_code")), rhs)
185
186 def test_tuple_in_rhs_must_have_2_elements_each(self):
187 test_cases = (
188 ((),),
189 ((1,),),
190 ((1, 2, 3),),
191 )
192
193 for rhs in test_cases:
194 with self.subTest(rhs=rhs):
195 with self.assertRaisesMessage(
196 ValueError,
197 "'in' lookup of ('customer_code', 'company_code') "
198 "must have 2 elements each",
199 ):
200 TupleIn((F("customer_code"), F("company_code")), rhs)
201
202 def test_lt(self):
203 c1, c2, c3, c4, c5, c6 = (
204 self.contact_1,
205 self.contact_2,
206 self.contact_3,
207 self.contact_4,
208 self.contact_5,
209 self.contact_6,
210 )
211 test_cases = (
212 (self.customer_1, ()),
213 (self.customer_2, (c1, c2, c5)),
214 (self.customer_5, (c1, c2, c3, c5)),
215 (self.customer_3, (c1, c2, c3, c5, c6)),
216 (self.customer_4, (c1, c2, c3, c4, c5, c6)),
217 )
218
219 for customer, contacts in test_cases:
220 with self.subTest(
221 "filter(customer__lt=customer)",
222 customer=customer,
223 contacts=contacts,
224 ):
225 self.assertSequenceEqual(
226 Contact.objects.filter(customer__lt=customer).order_by("id"),
227 contacts,
228 )
229 with self.subTest(
230 "filter(TupleLessThan)",
231 customer=customer,
232 contacts=contacts,
233 ):
234 lhs = (F("customer_code"), F("company_code"))
235 rhs = (customer.customer_id, customer.company)
236 lookup = TupleLessThan(lhs, rhs)
237 self.assertSequenceEqual(
238 Contact.objects.filter(lookup).order_by("id"), contacts
239 )
240
241 def test_lt_subquery(self):
242 with self.assertRaisesMessage(
243 ValueError, "'lt' doesn't support multi-column subqueries."
244 ):
245 subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
246 self.assertSequenceEqual(
247 Contact.objects.filter(customer__lt=subquery).order_by("id"), ()
248 )
249
250 def test_lte(self):
251 c1, c2, c3, c4, c5, c6 = (
252 self.contact_1,
253 self.contact_2,
254 self.contact_3,
255 self.contact_4,
256 self.contact_5,
257 self.contact_6,
258 )
259 test_cases = (
260 (self.customer_1, (c1, c2, c5)),
261 (self.customer_2, (c1, c2, c3, c5)),
262 (self.customer_5, (c1, c2, c3, c5, c6)),
263 (self.customer_3, (c1, c2, c3, c4, c5, c6)),
264 (self.customer_4, (c1, c2, c3, c4, c5, c6)),
265 )
266
267 for customer, contacts in test_cases:
268 with self.subTest(
269 "filter(customer__lte=customer)",
270 customer=customer,
271 contacts=contacts,
272 ):
273 self.assertSequenceEqual(
274 Contact.objects.filter(customer__lte=customer).order_by("id"),
275 contacts,
276 )
277 with self.subTest(
278 "filter(TupleLessThanOrEqual)",
279 customer=customer,
280 contacts=contacts,
281 ):
282 lhs = (F("customer_code"), F("company_code"))
283 rhs = (customer.customer_id, customer.company)
284 lookup = TupleLessThanOrEqual(lhs, rhs)
285 self.assertSequenceEqual(
286 Contact.objects.filter(lookup).order_by("id"), contacts
287 )
288
289 def test_lte_subquery(self):
290 with self.assertRaisesMessage(
291 ValueError, "'lte' doesn't support multi-column subqueries."
292 ):
293 subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
294 self.assertSequenceEqual(
295 Contact.objects.filter(customer__lte=subquery).order_by("id"), ()
296 )
297
298 def test_gt(self):
299 test_cases = (
300 (self.customer_1, (self.contact_3, self.contact_4, self.contact_6)),
301 (self.customer_2, (self.contact_4, self.contact_6)),
302 (self.customer_5, (self.contact_4,)),
303 (self.customer_3, ()),
304 (self.customer_4, ()),
305 )
306
307 for customer, contacts in test_cases:
308 with self.subTest(
309 "filter(customer__gt=customer)",
310 customer=customer,
311 contacts=contacts,
312 ):
313 self.assertSequenceEqual(
314 Contact.objects.filter(customer__gt=customer).order_by("id"),
315 contacts,
316 )
317 with self.subTest(
318 "filter(TupleGreaterThan)",
319 customer=customer,
320 contacts=contacts,
321 ):
322 lhs = (F("customer_code"), F("company_code"))
323 rhs = (customer.customer_id, customer.company)
324 lookup = TupleGreaterThan(lhs, rhs)
325 self.assertSequenceEqual(
326 Contact.objects.filter(lookup).order_by("id"), contacts
327 )
328
329 def test_gt_subquery(self):
330 with self.assertRaisesMessage(
331 ValueError, "'gt' doesn't support multi-column subqueries."
332 ):
333 subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
334 self.assertSequenceEqual(
335 Contact.objects.filter(customer__gt=subquery).order_by("id"), ()
336 )
337
338 def test_gte(self):
339 c1, c2, c3, c4, c5, c6 = (
340 self.contact_1,
341 self.contact_2,
342 self.contact_3,
343 self.contact_4,
344 self.contact_5,
345 self.contact_6,
346 )
347 test_cases = (
348 (self.customer_1, (c1, c2, c3, c4, c5, c6)),
349 (self.customer_2, (c3, c4, c6)),
350 (self.customer_5, (c4, c6)),
351 (self.customer_3, (c4,)),
352 (self.customer_4, ()),
353 )
354
355 for customer, contacts in test_cases:
356 with self.subTest(
357 "filter(customer__gte=customer)",
358 customer=customer,
359 contacts=contacts,
360 ):
361 self.assertSequenceEqual(
362 Contact.objects.filter(customer__gte=customer).order_by("pk"),
363 contacts,
364 )
365 with self.subTest(
366 "filter(TupleGreaterThanOrEqual)",
367 customer=customer,
368 contacts=contacts,
369 ):
370 lhs = (F("customer_code"), F("company_code"))
371 rhs = (customer.customer_id, customer.company)
372 lookup = TupleGreaterThanOrEqual(lhs, rhs)
373 self.assertSequenceEqual(
374 Contact.objects.filter(lookup).order_by("id"), contacts
375 )
376
377 def test_gte_subquery(self):
378 with self.assertRaisesMessage(
379 ValueError, "'gte' doesn't support multi-column subqueries."
380 ):
381 subquery = Customer.objects.filter(id=self.customer_1.id)[:1]
382 self.assertSequenceEqual(
383 Contact.objects.filter(customer__gte=subquery).order_by("id"), ()
384 )
385
386 def test_isnull(self):
387 contacts = (
388 self.contact_1,
389 self.contact_2,
390 self.contact_3,
391 self.contact_4,
392 self.contact_5,
393 self.contact_6,
394 )
395
396 with self.subTest("filter(customer__isnull=True)"):
397 self.assertSequenceEqual(
398 Contact.objects.filter(customer__isnull=True).order_by("id"),
399 (),
400 )
401 with self.subTest("filter(TupleIsNull(True))"):
402 lhs = (F("customer_code"), F("company_code"))
403 lookup = TupleIsNull(lhs, True)
404 self.assertSequenceEqual(
405 Contact.objects.filter(lookup).order_by("id"),
406 (),
407 )
408 with self.subTest("filter(customer__isnull=False)"):
409 self.assertSequenceEqual(
410 Contact.objects.filter(customer__isnull=False).order_by("id"),
411 contacts,
412 )
413 with self.subTest("filter(TupleIsNull(False))"):
414 lhs = (F("customer_code"), F("company_code"))
415 lookup = TupleIsNull(lhs, False)
416 self.assertSequenceEqual(
417 Contact.objects.filter(lookup).order_by("id"),
418 contacts,
419 )
420
421 def test_isnull_subquery(self):
422 with self.assertRaisesMessage(
423 ValueError, "'isnull' doesn't support multi-column subqueries."
424 ):
425 subquery = Customer.objects.filter(id=0)[:1]
426 self.assertSequenceEqual(
427 Contact.objects.filter(customer__isnull=subquery).order_by("id"), ()
428 )
429
430 def test_lookup_errors(self):
431 m_2_elements = "'%s' lookup of 'customer' must have 2 elements"
432 m_2_elements_each = "'in' lookup of 'customer' must have 2 elements each"
433 test_cases = (
434 ({"customer": 1}, m_2_elements % "exact"),
435 ({"customer": (1, 2, 3)}, m_2_elements % "exact"),
436 ({"customer__in": (1, 2, 3)}, m_2_elements_each),
437 ({"customer__in": ("foo", "bar")}, m_2_elements_each),
438 ({"customer__gt": 1}, m_2_elements % "gt"),
439 ({"customer__gt": (1, 2, 3)}, m_2_elements % "gt"),
440 ({"customer__gte": 1}, m_2_elements % "gte"),
441 ({"customer__gte": (1, 2, 3)}, m_2_elements % "gte"),
442 ({"customer__lt": 1}, m_2_elements % "lt"),
443 ({"customer__lt": (1, 2, 3)}, m_2_elements % "lt"),
444 ({"customer__lte": 1}, m_2_elements % "lte"),
445 ({"customer__lte": (1, 2, 3)}, m_2_elements % "lte"),
446 )
447
448 for kwargs, message in test_cases:
449 with (
450 self.subTest(kwargs=kwargs),
451 self.assertRaisesMessage(ValueError, message),
452 ):
453 Contact.objects.get(**kwargs)
454
455 def test_tuple_lookup_names(self):
456 test_cases = (
457 (TupleExact, "exact"),
458 (TupleGreaterThan, "gt"),
459 (TupleGreaterThanOrEqual, "gte"),
460 (TupleLessThan, "lt"),
461 (TupleLessThanOrEqual, "lte"),
462 (TupleIn, "in"),
463 (TupleIsNull, "isnull"),
464 )
465
466 for lookup_class, lookup_name in test_cases:
467 with self.subTest(lookup_name):
468 self.assertEqual(lookup_class.lookup_name, lookup_name)
469
470 def test_tuple_lookup_rhs_must_be_tuple_or_list(self):
471 test_cases = itertools.product(
472 (
473 TupleExact,
474 TupleGreaterThan,
475 TupleGreaterThanOrEqual,
476 TupleLessThan,
477 TupleLessThanOrEqual,
478 TupleIn,
479 ),
480 (
481 0,
482 1,
483 None,
484 True,
485 False,
486 {"foo": "bar"},
487 ),
488 )
489
490 for lookup_cls, rhs in test_cases:
491 lookup_name = lookup_cls.lookup_name
492 with self.subTest(lookup_name=lookup_name, rhs=rhs):
493 with self.assertRaisesMessage(
494 ValueError,
495 f"'{lookup_name}' lookup of ('customer_code', 'company_code') "
496 "must be a tuple or a list",
497 ):
498 lookup_cls((F("customer_code"), F("company_code")), rhs)
499
500 def test_tuple_lookup_rhs_must_have_2_elements(self):
501 test_cases = itertools.product(
502 (
503 TupleExact,
504 TupleGreaterThan,
505 TupleGreaterThanOrEqual,
506 TupleLessThan,
507 TupleLessThanOrEqual,
508 ),
509 (
510 [],
511 [1],
512 [1, 2, 3],
513 (),
514 (1,),
515 (1, 2, 3),
516 ),
517 )
518
519 for lookup_cls, rhs in test_cases:
520 lookup_name = lookup_cls.lookup_name
521 with self.subTest(lookup_name=lookup_name, rhs=rhs):
522 with self.assertRaisesMessage(
523 ValueError,
524 f"'{lookup_name}' lookup of ('customer_code', 'company_code') "
525 "must have 2 elements",
526 ):
527 lookup_cls((F("customer_code"), F("company_code")), rhs)