2021-04-24 05:07:18 +00:00
|
|
|
from django.db.models import BooleanField, Exists, F, OuterRef, Q
|
|
|
|
from django.db.models.expressions import RawSQL
|
2016-11-06 10:37:07 +00:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
2021-03-14 21:00:40 +00:00
|
|
|
from .models import Tag
|
|
|
|
|
2016-11-06 10:37:07 +00:00
|
|
|
|
|
|
|
class QTests(SimpleTestCase):
|
2017-05-25 12:36:09 +00:00
|
|
|
def test_combine_and_empty(self):
|
|
|
|
q = Q(x=1)
|
|
|
|
self.assertEqual(q & Q(), q)
|
|
|
|
self.assertEqual(Q() & q, q)
|
|
|
|
|
2021-02-17 16:04:59 +00:00
|
|
|
q = Q(x__in={}.keys())
|
|
|
|
self.assertEqual(q & Q(), q)
|
|
|
|
self.assertEqual(Q() & q, q)
|
|
|
|
|
2017-05-25 12:36:09 +00:00
|
|
|
def test_combine_and_both_empty(self):
|
|
|
|
self.assertEqual(Q() & Q(), Q())
|
|
|
|
|
2017-05-18 18:03:30 +00:00
|
|
|
def test_combine_or_empty(self):
|
|
|
|
q = Q(x=1)
|
|
|
|
self.assertEqual(q | Q(), q)
|
|
|
|
self.assertEqual(Q() | q, q)
|
|
|
|
|
2021-02-17 16:04:59 +00:00
|
|
|
q = Q(x__in={}.keys())
|
|
|
|
self.assertEqual(q | Q(), q)
|
|
|
|
self.assertEqual(Q() | q, q)
|
|
|
|
|
2021-07-02 20:09:13 +00: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 09:27:57 +00: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 20:09:13 +00:00
|
|
|
base_q ^ Q(),
|
|
|
|
Q() ^ base_q,
|
2021-04-28 09:27:57 +00: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 18:03:30 +00:00
|
|
|
def test_combine_or_both_empty(self):
|
|
|
|
self.assertEqual(Q() | Q(), Q())
|
|
|
|
|
2021-07-02 20:09:13 +00:00
|
|
|
def test_combine_xor_both_empty(self):
|
|
|
|
self.assertEqual(Q() ^ Q(), Q())
|
|
|
|
|
2018-03-28 13:03:56 +00: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 20:09:13 +00:00
|
|
|
with self.assertRaisesMessage(TypeError, str(obj)):
|
|
|
|
q ^ obj
|
2018-03-28 13:03:56 +00:00
|
|
|
|
2021-04-24 05:07:18 +00:00
|
|
|
def test_combine_negated_boolean_expression(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
tagged = Tag.objects.filter(category=OuterRef("pk"))
|
2021-04-24 05:07:18 +00:00
|
|
|
tests = [
|
|
|
|
Q() & ~Exists(tagged),
|
|
|
|
Q() | ~Exists(tagged),
|
2021-07-02 20:09:13 +00:00
|
|
|
Q() ^ ~Exists(tagged),
|
2021-04-24 05:07:18 +00:00
|
|
|
]
|
|
|
|
for q in tests:
|
|
|
|
with self.subTest(q=q):
|
|
|
|
self.assertIs(q.negated, True)
|
|
|
|
|
2016-11-06 10:37:07 +00:00
|
|
|
def test_deconstruct(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q = Q(price__gt=F("discounted_price"))
|
2016-11-06 10:37:07 +00:00
|
|
|
path, args, kwargs = q.deconstruct()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(path, "django.db.models.Q")
|
|
|
|
self.assertEqual(args, (("price__gt", F("discounted_price")),))
|
2021-03-14 21:00:40 +00:00
|
|
|
self.assertEqual(kwargs, {})
|
2016-11-06 10:37:07 +00:00
|
|
|
|
|
|
|
def test_deconstruct_negated(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q = ~Q(price__gt=F("discounted_price"))
|
2016-11-06 10:37:07 +00:00
|
|
|
path, args, kwargs = q.deconstruct()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(args, (("price__gt", F("discounted_price")),))
|
|
|
|
self.assertEqual(kwargs, {"_negated": True})
|
2016-11-06 10:37:07 +00:00
|
|
|
|
|
|
|
def test_deconstruct_or(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
2016-11-06 10:37:07 +00:00
|
|
|
q = q1 | q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
(
|
|
|
|
("price__gt", F("discounted_price")),
|
|
|
|
("price", F("discounted_price")),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(kwargs, {"_connector": "OR"})
|
2016-11-06 10:37:07 +00:00
|
|
|
|
2021-07-02 20:09:13 +00: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")),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.assertEqual(kwargs, {"_connector": "XOR"})
|
|
|
|
|
2016-11-06 10:37:07 +00:00
|
|
|
def test_deconstruct_and(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
2016-11-06 10:37:07 +00:00
|
|
|
q = q1 & q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
(
|
|
|
|
("price__gt", F("discounted_price")),
|
|
|
|
("price", F("discounted_price")),
|
|
|
|
),
|
|
|
|
)
|
2018-02-12 19:20:54 +00:00
|
|
|
self.assertEqual(kwargs, {})
|
2016-11-06 10:37:07 +00:00
|
|
|
|
2018-02-12 19:00:29 +00:00
|
|
|
def test_deconstruct_multiple_kwargs(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q = Q(price__gt=F("discounted_price"), price=F("discounted_price"))
|
2018-02-12 19:00:29 +00:00
|
|
|
path, args, kwargs = q.deconstruct()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
args,
|
|
|
|
(
|
|
|
|
("price", F("discounted_price")),
|
|
|
|
("price__gt", F("discounted_price")),
|
|
|
|
),
|
|
|
|
)
|
2018-02-12 19:20:54 +00:00
|
|
|
self.assertEqual(kwargs, {})
|
2018-02-12 19:00:29 +00:00
|
|
|
|
2016-11-06 10:37:07 +00:00
|
|
|
def test_deconstruct_nested(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q = Q(Q(price__gt=F("discounted_price")))
|
2016-11-06 10:37:07 +00:00
|
|
|
path, args, kwargs = q.deconstruct()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(args, (Q(price__gt=F("discounted_price")),))
|
2018-02-12 19:20:54 +00:00
|
|
|
self.assertEqual(kwargs, {})
|
2016-11-06 10:37:07 +00:00
|
|
|
|
2021-03-14 21:00:40 +00:00
|
|
|
def test_deconstruct_boolean_expression(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
expr = RawSQL("1 = 1", BooleanField())
|
2021-04-24 05:07:18 +00:00
|
|
|
q = Q(expr)
|
2021-03-14 21:00:40 +00:00
|
|
|
_, args, kwargs = q.deconstruct()
|
2021-04-24 05:07:18 +00:00
|
|
|
self.assertEqual(args, (expr,))
|
2021-03-14 21:00:40 +00:00
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
2016-11-06 10:37:07 +00:00
|
|
|
def test_reconstruct(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q = Q(price__gt=F("discounted_price"))
|
2016-11-06 10:37:07 +00:00
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(Q(*args, **kwargs), q)
|
|
|
|
|
|
|
|
def test_reconstruct_negated(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q = ~Q(price__gt=F("discounted_price"))
|
2016-11-06 10:37:07 +00:00
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(Q(*args, **kwargs), q)
|
|
|
|
|
|
|
|
def test_reconstruct_or(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
2016-11-06 10:37:07 +00:00
|
|
|
q = q1 | q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(Q(*args, **kwargs), q)
|
|
|
|
|
2021-07-02 20:09:13 +00: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 10:37:07 +00:00
|
|
|
def test_reconstruct_and(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
q1 = Q(price__gt=F("discounted_price"))
|
|
|
|
q2 = Q(price=F("discounted_price"))
|
2016-11-06 10:37:07 +00:00
|
|
|
q = q1 & q2
|
|
|
|
path, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(Q(*args, **kwargs), q)
|