diff options
| author | Bendeguz Csirmaz <csirmazbendeguz@gmail.com> | 2024-10-15 01:31:27 +0800 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-11-04 09:20:54 +0100 |
| commit | f7601aed515a125cde776ebbf6ff6e8432cbafdb (patch) | |
| tree | 0c40dd6bb13f16dc7b080611602219724796fd7b /tests/foreign_object | |
| parent | 611bf6c2e2a1b4ab93273980c45150c099ab146d (diff) | |
Refs #373 -- Added TupleIn subqueries.
Diffstat (limited to 'tests/foreign_object')
| -rw-r--r-- | tests/foreign_object/test_tuple_lookups.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/foreign_object/test_tuple_lookups.py b/tests/foreign_object/test_tuple_lookups.py index 499329e7ca..797fea1c8a 100644 --- a/tests/foreign_object/test_tuple_lookups.py +++ b/tests/foreign_object/test_tuple_lookups.py @@ -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), |
