2021-03-15 05:00:40 +08:00
|
|
|
from django.db.models import Exists, F, OuterRef, Q
|
2016-11-06 18:37:07 +08:00
|
|
|
from django.test import SimpleTestCase
|
|
|
|
|
2021-03-15 05:00:40 +08:00
|
|
|
from .models import Tag
|
|
|
|
|
2016-11-06 18:37:07 +08:00
|
|
|
|
|
|
|
class QTests(SimpleTestCase):
|
2017-05-25 20:36:09 +08:00
|
|
|
def test_combine_and_empty(self):
|
|
|
|
q = Q(x=1)
|
|
|
|
self.assertEqual(q & Q(), q)
|
|
|
|
self.assertEqual(Q() & q, q)
|
|
|
|
|
2021-02-18 00:04:59 +08:00
|
|
|
q = Q(x__in={}.keys())
|
|
|
|
self.assertEqual(q & Q(), q)
|
|
|
|
self.assertEqual(Q() & q, q)
|
|
|
|
|
2017-05-25 20:36:09 +08:00
|
|
|
def test_combine_and_both_empty(self):
|
|
|
|
self.assertEqual(Q() & Q(), Q())
|
|
|
|
|
2017-05-19 02:03:30 +08:00
|
|
|
def test_combine_or_empty(self):
|
|
|
|
q = Q(x=1)
|
|
|
|
self.assertEqual(q | Q(), q)
|
|
|
|
self.assertEqual(Q() | q, q)
|
|
|
|
|
2021-02-18 00:04:59 +08:00
|
|
|
q = Q(x__in={}.keys())
|
|
|
|
self.assertEqual(q | Q(), q)
|
|
|
|
self.assertEqual(Q() | q, q)
|
|
|
|
|
2017-05-19 02:03:30 +08:00
|
|
|
def test_combine_or_both_empty(self):
|
|
|
|
self.assertEqual(Q() | Q(), Q())
|
|
|
|
|
2018-03-28 21:03:56 +08: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
|
|
|
|
|
2016-11-06 18:37:07 +08:00
|
|
|
def test_deconstruct(self):
|
|
|
|
q = Q(price__gt=F('discounted_price'))
|
|
|
|
path, args, kwargs = q.deconstruct()
|
2018-02-13 03:20:54 +08:00
|
|
|
self.assertEqual(path, 'django.db.models.Q')
|
2021-03-15 05:00:40 +08:00
|
|
|
self.assertEqual(args, (('price__gt', F('discounted_price')),))
|
|
|
|
self.assertEqual(kwargs, {})
|
2016-11-06 18:37:07 +08:00
|
|
|
|
|
|
|
def test_deconstruct_negated(self):
|
|
|
|
q = ~Q(price__gt=F('discounted_price'))
|
|
|
|
path, args, kwargs = q.deconstruct()
|
2021-03-15 05:00:40 +08:00
|
|
|
self.assertEqual(args, (('price__gt', F('discounted_price')),))
|
|
|
|
self.assertEqual(kwargs, {'_negated': True})
|
2016-11-06 18:37:07 +08: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')),
|
|
|
|
))
|
|
|
|
self.assertEqual(kwargs, {'_connector': 'OR'})
|
|
|
|
|
|
|
|
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')),
|
|
|
|
))
|
2018-02-13 03:20:54 +08:00
|
|
|
self.assertEqual(kwargs, {})
|
2016-11-06 18:37:07 +08:00
|
|
|
|
2018-02-13 03:00:29 +08: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')),
|
|
|
|
))
|
2018-02-13 03:20:54 +08:00
|
|
|
self.assertEqual(kwargs, {})
|
2018-02-13 03:00:29 +08:00
|
|
|
|
2016-11-06 18:37:07 +08: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-13 03:20:54 +08:00
|
|
|
self.assertEqual(kwargs, {})
|
2016-11-06 18:37:07 +08:00
|
|
|
|
2021-03-15 05:00:40 +08:00
|
|
|
def test_deconstruct_boolean_expression(self):
|
|
|
|
tagged = Tag.objects.filter(category=OuterRef('pk'))
|
|
|
|
q = Q(Exists(tagged))
|
|
|
|
_, args, kwargs = q.deconstruct()
|
|
|
|
self.assertEqual(args, (Exists(tagged),))
|
|
|
|
self.assertEqual(kwargs, {})
|
|
|
|
|
2016-11-06 18:37:07 +08: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)
|
|
|
|
|
|
|
|
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)
|