2022-01-31 16:04:13 +01:00
|
|
|
from django.core.exceptions import FieldError
|
2024-08-28 17:53:40 +02:00
|
|
|
from django.db import connection
|
2022-05-03 14:06:42 +02:00
|
|
|
from django.db.models import (
|
|
|
|
BooleanField,
|
|
|
|
Exists,
|
|
|
|
ExpressionWrapper,
|
|
|
|
F,
|
|
|
|
OuterRef,
|
|
|
|
Q,
|
|
|
|
Value,
|
|
|
|
)
|
2022-09-26 22:59:25 +02:00
|
|
|
from django.db.models.expressions import NegatedExpression, RawSQL
|
2022-05-03 14:06:42 +02:00
|
|
|
from django.db.models.functions import Lower
|
2024-04-21 08:46:26 +02:00
|
|
|
from django.db.models.lookups import Exact, IsNull
|
2022-05-03 14:06:42 +02:00
|
|
|
from django.db.models.sql.where import NothingNode
|
2022-01-31 16:04:13 +01:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
2016-11-06 11:37:07 +01:00
|
|
|
|
2021-03-14 22:00:40 +01:00
|
|
|
from .models import Tag
|
|
|
|
|
2016-11-06 11:37:07 +01:00
|
|
|
|
|
|
|
class QTests(SimpleTestCase):
|
2017-05-25 14:36:09 +02:00
|
|
|
def test_combine_and_empty(self):
|
|
|
|
q = Q(x=1)
|
|
|
|
self.assertEqual(q & Q(), q)
|
|
|
|
self.assertEqual(Q() & q, q)
|
|
|
|
|
2021-02-17 17:04:59 +01:00
|
|
|
q = Q(x__in={}.keys())
|
|
|
|
self.assertEqual(q & Q(), q)
|
|
|
|
self.assertEqual(Q() & q, q)
|
|
|
|
|
2017-05-25 14:36:09 +02:00
|
|
|
def test_combine_and_both_empty(self):
|
|
|
|
self.assertEqual(Q() & Q(), Q())
|
|
|
|
|
2017-05-18 20:03:30 +02:00
|
|
|
def test_combine_or_empty(self):
|
|
|
|
q = Q(x=1)
|
|
|
|
self.assertEqual(q | Q(), q)
|
|
|
|
self.assertEqual(Q() | q, q)
|
|
|
|
|
2021-02-17 17:04:59 +01:00
|
|
|
q = Q(x__in={}.keys())
|
|
|
|
self.assertEqual(q | Q(), q)
|
|
|
|
self.assertEqual(Q() | q, q)
|
|
|
|
|
2021-07-02 22:09:13 +02:00
|
|
|
def test_combine_xor_empty(self):
|
|
|
|
q = Q(x=1)
|
|
|
|
self.assertEqual(q ^ Q(), q)
|
|
|
|
self.assertEqual(Q() ^ q, q)
|
|
|
|
|
|
|
|
q = Q(x__in={}.keys())
|
|
|
|
self.assertEqual(q ^ Q(), q)
|
|
|
|
self.assertEqual(Q() ^ q, q)
|
|
|
|
|
2021-04-28 11:27:57 +02:00
|
|
|
def test_combine_empty_copy(self):
|
|
|
|
base_q = Q(x=1)
|
|
|
|
tests = [
|
|
|
|
base_q | Q(),
|
|
|
|
Q() | base_q,
|
|
|
|
base_q & Q(),
|
|
|
|
Q() & base_q,
|
2021-07-02 22:09:13 +02:00
|
|
|
base_q ^ Q(),
|
|
|
|
Q() ^ base_q,
|
2021-04-28 11:27:57 +02:00
|
|
|
]
|
|
|
|
for i, q in enumerate(tests):
|
|
|
|
with self.subTest(i=i):
|
|
|
|
self.assertEqual(q, base_q)
|
|
|
|
self.assertIsNot(q, base_q)
|
|
|
|
|
2017-05-18 20:03:30 +02:00
|
|
|
def test_combine_or_both_empty(self):
|
|
|
|
self.assertEqual(Q() | Q(), Q())
|
|
|
|
|
2021-07-02 22:09:13 +02:00
|
|
|
def test_combine_xor_both_empty(self):
|
|
|
|
self.assertEqual(Q() ^ Q(), Q())
|
|
|
|
|
2018-03-28 15:03:56 +02:00
|
|
|
def test_combine_not_q_object(self):
|
|
|
|
obj = object()
|
|
|
|
q = Q(x=1)
|
|
|
|
with self.assertRaisesMessage(TypeError, str(obj)):
|
|
|
|
q | obj
|
|
|
|
with self.assertRaisesMessage(TypeError, str(obj)):
|
|
|
|
q & obj
|
2021-07-02 22:09:13 +02:00
|
|
|
with self.assertRaisesMessage(TypeError, str(obj)):
|
|
|
|
q ^ obj
|
2018-03-28 15:03:56 +02:00
|
|
|
|
2021-04-24 07:07:18 +02:00
|
|
|
def test_combine_negated_boolean_expression(self):
|
|
|
|
tagged = Tag.objects.filter(category=OuterRef("pk"))
|
|
|
|
tests = [
|
|
|
|
Q() & ~Exists(tagged),
|
|
|
|
Q() | ~Exists(tagged),
|
2021-07-02 22:09:13 +02:00
|
|
|
Q() ^ ~Exists(tagged),
|
2021-04-24 07:07:18 +02:00
|
|
|
]
|
|
|
|
for q in tests:
|
|
|
|
with self.subTest(q=q):
|
2022-09-26 22:59:25 +02:00
|
|
|
self.assertIsInstance(q, NegatedExpression)
|
2021-04-24 07:07:18 +02:00
|
|
|
|
2016-11-06 11:37:07 +01:00
|
|
|
def test_deconstruct(self):
|
|
|
|
q = Q(price__gt=F("discounted_price"))
|
|
|
|
path, args, kwargs = q.deconstruct()
|
2018-02-12 20:20:54 +01:00
|
|
|
self.assertEqual(path, "django.db.models.Q")
|
2021-03-14 22:00:40 +01:00
|
|
|
self.assertEqual(args, (("price__gt", F("discounted_price")),))
|
|
|
|
self.assertEqual(kwargs, {})
|
2016-11-06 11:37:07 +01:00
|
|
|
|
|
|
|
def test_deconstruct_negated(self):
|
|
|
|
q = ~Q(price__gt=F("discounted_price"))
|
|
|
|
path, args, kwargs = q.deconstruct()
|
2021-03-14 22:00:40 +01:00
|
|
|
self.assertEqual(args, (("price__gt", F("discounted_price")),))
|
|
|
|
self.assertEqual(kwargs, {"_negated": True})
|
2016-11-06 11:37:07 +01:00
|
|
|
|
|
|
|
def test_deconstruct_or(self):
|
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
|
|
|
q = q1 | q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
(
|
|
|
|
("price__gt", F("discounted_price")),
|
|
|
|
("price", F("discounted_price")),
|
2022-02-03 20:24:19 +01:00
|
|
|
),
|
2016-11-06 11:37:07 +01:00
|
|
|
)
|
2022-07-23 14:31:35 +02:00
|
|
|
self.assertEqual(kwargs, {"_connector": Q.OR})
|
2016-11-06 11:37:07 +01:00
|
|
|
|
2021-07-02 22:09:13 +02:00
|
|
|
def test_deconstruct_xor(self):
|
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
|
|
|
q = q1 ^ q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
(
|
|
|
|
("price__gt", F("discounted_price")),
|
|
|
|
("price", F("discounted_price")),
|
|
|
|
),
|
|
|
|
)
|
2022-07-23 14:31:35 +02:00
|
|
|
self.assertEqual(kwargs, {"_connector": Q.XOR})
|
2021-07-02 22:09:13 +02:00
|
|
|
|
2016-11-06 11:37:07 +01:00
|
|
|
def test_deconstruct_and(self):
|
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
|
|
|
q = q1 & q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
(
|
|
|
|
("price__gt", F("discounted_price")),
|
|
|
|
("price", F("discounted_price")),
|
2022-02-03 20:24:19 +01:00
|
|
|
),
|
2016-11-06 11:37:07 +01:00
|
|
|
)
|
2018-02-12 20:20:54 +01:00
|
|
|
self.assertEqual(kwargs, {})
|
2016-11-06 11:37:07 +01:00
|
|
|
|
2018-02-12 20:00:29 +01:00
|
|
|
def test_deconstruct_multiple_kwargs(self):
|
|
|
|
q = Q(price__gt=F("discounted_price"), price=F("discounted_price"))
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
(
|
|
|
|
("price", F("discounted_price")),
|
|
|
|
("price__gt", F("discounted_price")),
|
2022-02-03 20:24:19 +01:00
|
|
|
),
|
2018-02-12 20:00:29 +01:00
|
|
|
)
|
2018-02-12 20:20:54 +01:00
|
|
|
self.assertEqual(kwargs, {})
|
2018-02-12 20:00:29 +01:00
|
|
|
|
2016-11-06 11:37:07 +01:00
|
|
|
def test_deconstruct_nested(self):
|
|
|
|
q = Q(Q(price__gt=F("discounted_price")))
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(args, (Q(price__gt=F("discounted_price")),))
|
2018-02-12 20:20:54 +01:00
|
|
|
self.assertEqual(kwargs, {})
|
2016-11-06 11:37:07 +01:00
|
|
|
|
2021-03-14 22:00:40 +01:00
|
|
|
def test_deconstruct_boolean_expression(self):
|
2021-04-24 07:07:18 +02:00
|
|
|
expr = RawSQL("1 = 1", BooleanField())
|
|
|
|
q = Q(expr)
|
2021-03-14 22:00:40 +01:00
|
|
|
_, args, kwargs = q.deconstruct()
|
2021-04-24 07:07:18 +02:00
|
|
|
self.assertEqual(args, (expr,))
|
2021-03-14 22:00:40 +01:00
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
2016-11-06 11:37:07 +01:00
|
|
|
def test_reconstruct(self):
|
|
|
|
q = Q(price__gt=F("discounted_price"))
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(Q(*args, **kwargs), q)
|
|
|
|
|
|
|
|
def test_reconstruct_negated(self):
|
|
|
|
q = ~Q(price__gt=F("discounted_price"))
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(Q(*args, **kwargs), q)
|
|
|
|
|
|
|
|
def test_reconstruct_or(self):
|
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
|
|
|
q = q1 | q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(Q(*args, **kwargs), q)
|
|
|
|
|
2021-07-02 22:09:13 +02:00
|
|
|
def test_reconstruct_xor(self):
|
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
|
|
|
q = q1 ^ q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(Q(*args, **kwargs), q)
|
|
|
|
|
2016-11-06 11:37:07 +01:00
|
|
|
def test_reconstruct_and(self):
|
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
|
|
|
q = q1 & q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(Q(*args, **kwargs), q)
|
2022-05-03 14:06:42 +02:00
|
|
|
|
2023-07-27 09:07:48 +02:00
|
|
|
def test_equal(self):
|
|
|
|
self.assertEqual(Q(), Q())
|
|
|
|
self.assertEqual(
|
|
|
|
Q(("pk__in", (1, 2))),
|
|
|
|
Q(("pk__in", [1, 2])),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Q(("pk__in", (1, 2))),
|
|
|
|
Q(pk__in=[1, 2]),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
Q(("pk__in", (1, 2))),
|
|
|
|
Q(("pk__in", {1: "first", 2: "second"}.keys())),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
Q(name__iexact=F("other_name")),
|
|
|
|
Q(name=Lower(F("other_name"))),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_hash(self):
|
|
|
|
self.assertEqual(hash(Q()), hash(Q()))
|
|
|
|
self.assertEqual(
|
|
|
|
hash(Q(("pk__in", (1, 2)))),
|
|
|
|
hash(Q(("pk__in", [1, 2]))),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
hash(Q(("pk__in", (1, 2)))),
|
|
|
|
hash(Q(pk__in=[1, 2])),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
hash(Q(("pk__in", (1, 2)))),
|
|
|
|
hash(Q(("pk__in", {1: "first", 2: "second"}.keys()))),
|
|
|
|
)
|
|
|
|
self.assertNotEqual(
|
|
|
|
hash(Q(name__iexact=F("other_name"))),
|
|
|
|
hash(Q(name=Lower(F("other_name")))),
|
|
|
|
)
|
|
|
|
|
2022-05-03 14:06:42 +02:00
|
|
|
def test_flatten(self):
|
|
|
|
q = Q()
|
|
|
|
self.assertEqual(list(q.flatten()), [q])
|
|
|
|
q = Q(NothingNode())
|
|
|
|
self.assertEqual(list(q.flatten()), [q, q.children[0]])
|
|
|
|
q = Q(
|
|
|
|
ExpressionWrapper(
|
|
|
|
Q(RawSQL("id = 0", params=(), output_field=BooleanField()))
|
|
|
|
| Q(price=Value("4.55"))
|
|
|
|
| Q(name=Lower("category")),
|
|
|
|
output_field=BooleanField(),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
flatten = list(q.flatten())
|
|
|
|
self.assertEqual(len(flatten), 7)
|
2022-01-31 16:04:13 +01:00
|
|
|
|
2021-09-21 01:49:16 +02:00
|
|
|
def test_create_helper(self):
|
|
|
|
items = [("a", 1), ("b", 2), ("c", 3)]
|
|
|
|
for connector in [Q.AND, Q.OR, Q.XOR]:
|
|
|
|
with self.subTest(connector=connector):
|
|
|
|
self.assertEqual(
|
|
|
|
Q.create(items, connector=connector),
|
|
|
|
Q(*items, _connector=connector),
|
|
|
|
)
|
|
|
|
|
2024-04-21 08:46:26 +02:00
|
|
|
def test_referenced_base_fields(self):
|
|
|
|
# Make sure Q.referenced_base_fields retrieves all base fields from
|
|
|
|
# both filters and F expressions.
|
|
|
|
tests = [
|
|
|
|
(Q(field_1=1) & Q(field_2=1), {"field_1", "field_2"}),
|
|
|
|
(
|
|
|
|
Q(Exact(F("field_3"), IsNull(F("field_4"), True))),
|
|
|
|
{"field_3", "field_4"},
|
|
|
|
),
|
|
|
|
(Q(Exact(Q(field_5=F("field_6")), True)), {"field_5", "field_6"}),
|
|
|
|
(Q(field_2=1), {"field_2"}),
|
|
|
|
(Q(field_7__lookup=True), {"field_7"}),
|
|
|
|
(Q(field_7__joined_field__lookup=True), {"field_7"}),
|
|
|
|
]
|
|
|
|
combined_q = Q(1)
|
|
|
|
combined_q_base_fields = set()
|
|
|
|
for q, expected_base_fields in tests:
|
|
|
|
combined_q &= q
|
|
|
|
combined_q_base_fields |= expected_base_fields
|
|
|
|
tests.append((combined_q, combined_q_base_fields))
|
|
|
|
for q, expected_base_fields in tests:
|
|
|
|
with self.subTest(q=q):
|
|
|
|
self.assertEqual(
|
|
|
|
q.referenced_base_fields,
|
|
|
|
expected_base_fields,
|
|
|
|
)
|
|
|
|
|
2022-01-31 16:04:13 +01:00
|
|
|
|
|
|
|
class QCheckTests(TestCase):
|
|
|
|
def test_basic(self):
|
|
|
|
q = Q(price__gt=20)
|
|
|
|
self.assertIs(q.check({"price": 30}), True)
|
|
|
|
self.assertIs(q.check({"price": 10}), False)
|
|
|
|
|
|
|
|
def test_expression(self):
|
|
|
|
q = Q(name="test")
|
|
|
|
self.assertIs(q.check({"name": Lower(Value("TeSt"))}), True)
|
|
|
|
self.assertIs(q.check({"name": Value("other")}), False)
|
|
|
|
|
|
|
|
def test_missing_field(self):
|
|
|
|
q = Q(description__startswith="prefix")
|
|
|
|
msg = "Cannot resolve keyword 'description' into field."
|
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
q.check({"name": "test"})
|
|
|
|
|
|
|
|
def test_boolean_expression(self):
|
|
|
|
q = Q(ExpressionWrapper(Q(price__gt=20), output_field=BooleanField()))
|
|
|
|
self.assertIs(q.check({"price": 25}), True)
|
|
|
|
self.assertIs(q.check({"price": Value(10)}), False)
|
|
|
|
|
|
|
|
def test_rawsql(self):
|
|
|
|
"""
|
|
|
|
RawSQL expressions cause a database error because "price" cannot be
|
|
|
|
replaced by its value. In this case, Q.check() logs a warning and
|
|
|
|
return True.
|
|
|
|
"""
|
|
|
|
q = Q(RawSQL("price > %s", params=(20,), output_field=BooleanField()))
|
|
|
|
with self.assertLogs("django.db.models", "WARNING") as cm:
|
|
|
|
self.assertIs(q.check({"price": 10}), True)
|
|
|
|
self.assertIn(
|
|
|
|
f"Got a database error calling check() on {q!r}: ",
|
|
|
|
cm.records[0].getMessage(),
|
|
|
|
)
|
2024-08-28 17:53:40 +02:00
|
|
|
|
|
|
|
# We must leave the connection in a usable state (#35712).
|
|
|
|
self.assertTrue(connection.is_usable())
|