mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Refs #373 -- Added TupleIn subqueries.
This commit is contained in:
committed by
Sarah Boyce
parent
611bf6c2e2
commit
f7601aed51
@@ -11,6 +11,7 @@ from django.db.models.fields.tuple_lookups import (
|
||||
TupleLessThan,
|
||||
TupleLessThanOrEqual,
|
||||
)
|
||||
from django.db.models.lookups import In
|
||||
from django.test import TestCase, skipUnlessDBFeature
|
||||
|
||||
from .models import Contact, Customer
|
||||
@@ -126,6 +127,46 @@ class TupleLookupsTests(TestCase):
|
||||
(self.contact_1, self.contact_2, self.contact_5),
|
||||
)
|
||||
|
||||
def test_tuple_in_subquery_must_be_query(self):
|
||||
lhs = (F("customer_code"), F("company_code"))
|
||||
# If rhs is any non-Query object with an as_sql() function.
|
||||
rhs = In(F("customer_code"), [1, 2, 3])
|
||||
with self.assertRaisesMessage(
|
||||
ValueError,
|
||||
"'in' subquery lookup of ('customer_code', 'company_code') "
|
||||
"must be a Query object (received 'In')",
|
||||
):
|
||||
TupleIn(lhs, rhs)
|
||||
|
||||
def test_tuple_in_subquery_must_have_2_fields(self):
|
||||
lhs = (F("customer_code"), F("company_code"))
|
||||
rhs = Customer.objects.values_list("customer_id").query
|
||||
with self.assertRaisesMessage(
|
||||
ValueError,
|
||||
"'in' subquery lookup of ('customer_code', 'company_code') "
|
||||
"must have 2 fields (received 1)",
|
||||
):
|
||||
TupleIn(lhs, rhs)
|
||||
|
||||
def test_tuple_in_subquery(self):
|
||||
customers = Customer.objects.values_list("customer_id", "company")
|
||||
test_cases = (
|
||||
(self.customer_1, (self.contact_1, self.contact_2, self.contact_5)),
|
||||
(self.customer_2, (self.contact_3,)),
|
||||
(self.customer_3, (self.contact_4,)),
|
||||
(self.customer_4, ()),
|
||||
(self.customer_5, (self.contact_6,)),
|
||||
)
|
||||
|
||||
for customer, contacts in test_cases:
|
||||
lhs = (F("customer_code"), F("company_code"))
|
||||
rhs = customers.filter(id=customer.id).query
|
||||
lookup = TupleIn(lhs, rhs)
|
||||
qs = Contact.objects.filter(lookup).order_by("id")
|
||||
|
||||
with self.subTest(customer=customer.id, query=str(qs.query)):
|
||||
self.assertSequenceEqual(qs, contacts)
|
||||
|
||||
def test_tuple_in_rhs_must_be_collection_of_tuples_or_lists(self):
|
||||
test_cases = (
|
||||
(1, 2, 3),
|
||||
|
||||
Reference in New Issue
Block a user