2016-11-05 13:12:12 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from django.core.exceptions import FieldError
|
2022-01-31 14:51:38 +00:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, connection
|
2019-11-15 05:08:36 +00:00
|
|
|
from django.db.models import BooleanField, CharField, F, Q
|
2022-01-31 14:51:38 +00:00
|
|
|
from django.db.models.expressions import (
|
|
|
|
Col,
|
|
|
|
Exists,
|
|
|
|
ExpressionWrapper,
|
|
|
|
Func,
|
|
|
|
RawSQL,
|
|
|
|
Value,
|
|
|
|
)
|
2016-11-05 13:12:12 +00:00
|
|
|
from django.db.models.fields.related_lookups import RelatedIsNull
|
|
|
|
from django.db.models.functions import Lower
|
|
|
|
from django.db.models.lookups import Exact, GreaterThan, IsNull, LessThan
|
2022-01-31 14:51:38 +00:00
|
|
|
from django.db.models.sql.constants import SINGLE
|
|
|
|
from django.db.models.sql.query import JoinPromoter, Query, get_field_names_from_opts
|
2022-07-23 12:31:35 +00:00
|
|
|
from django.db.models.sql.where import AND, OR
|
2022-01-31 14:51:38 +00:00
|
|
|
from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
|
2018-08-21 16:17:46 +00:00
|
|
|
from django.test.utils import register_lookup
|
2016-11-05 13:12:12 +00:00
|
|
|
|
|
|
|
from .models import Author, Item, ObjectC, Ranking
|
|
|
|
|
|
|
|
|
2018-11-26 19:05:02 +00:00
|
|
|
class TestQuery(SimpleTestCase):
|
2016-11-05 13:12:12 +00:00
|
|
|
def test_simple_query(self):
|
|
|
|
query = Query(Author)
|
|
|
|
where = query.build_where(Q(num__gt=2))
|
|
|
|
lookup = where.children[0]
|
|
|
|
self.assertIsInstance(lookup, GreaterThan)
|
|
|
|
self.assertEqual(lookup.rhs, 2)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(lookup.lhs.target, Author._meta.get_field("num"))
|
2016-11-05 13:12:12 +00:00
|
|
|
|
2019-09-10 03:58:29 +00:00
|
|
|
def test_non_alias_cols_query(self):
|
|
|
|
query = Query(Author, alias_cols=False)
|
2022-02-03 19:24:19 +00:00
|
|
|
where = query.build_where(Q(num__gt=2, name__isnull=False) | Q(num__lt=F("id")))
|
2019-04-30 08:20:41 +00:00
|
|
|
|
|
|
|
name_isnull_lookup, num_gt_lookup = where.children[0].children
|
|
|
|
self.assertIsInstance(num_gt_lookup, GreaterThan)
|
2019-09-10 03:58:29 +00:00
|
|
|
self.assertIsInstance(num_gt_lookup.lhs, Col)
|
|
|
|
self.assertIsNone(num_gt_lookup.lhs.alias)
|
2019-04-30 08:20:41 +00:00
|
|
|
self.assertIsInstance(name_isnull_lookup, IsNull)
|
2019-09-10 03:58:29 +00:00
|
|
|
self.assertIsInstance(name_isnull_lookup.lhs, Col)
|
|
|
|
self.assertIsNone(name_isnull_lookup.lhs.alias)
|
2019-04-30 08:20:41 +00:00
|
|
|
|
|
|
|
num_lt_lookup = where.children[1]
|
|
|
|
self.assertIsInstance(num_lt_lookup, LessThan)
|
2019-09-10 03:58:29 +00:00
|
|
|
self.assertIsInstance(num_lt_lookup.rhs, Col)
|
|
|
|
self.assertIsNone(num_lt_lookup.rhs.alias)
|
|
|
|
self.assertIsInstance(num_lt_lookup.lhs, Col)
|
|
|
|
self.assertIsNone(num_lt_lookup.lhs.alias)
|
2019-04-30 08:20:41 +00:00
|
|
|
|
2016-11-05 13:12:12 +00:00
|
|
|
def test_complex_query(self):
|
|
|
|
query = Query(Author)
|
|
|
|
where = query.build_where(Q(num__gt=2) | Q(num__lt=0))
|
|
|
|
self.assertEqual(where.connector, OR)
|
|
|
|
|
|
|
|
lookup = where.children[0]
|
|
|
|
self.assertIsInstance(lookup, GreaterThan)
|
|
|
|
self.assertEqual(lookup.rhs, 2)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(lookup.lhs.target, Author._meta.get_field("num"))
|
2016-11-05 13:12:12 +00:00
|
|
|
|
|
|
|
lookup = where.children[1]
|
|
|
|
self.assertIsInstance(lookup, LessThan)
|
|
|
|
self.assertEqual(lookup.rhs, 0)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(lookup.lhs.target, Author._meta.get_field("num"))
|
2016-11-05 13:12:12 +00:00
|
|
|
|
|
|
|
def test_multiple_fields(self):
|
2019-09-10 03:58:29 +00:00
|
|
|
query = Query(Item, alias_cols=False)
|
2022-02-03 19:24:19 +00:00
|
|
|
where = query.build_where(Q(modified__gt=F("created")))
|
2016-11-05 13:12:12 +00:00
|
|
|
lookup = where.children[0]
|
|
|
|
self.assertIsInstance(lookup, GreaterThan)
|
2019-09-10 03:58:29 +00:00
|
|
|
self.assertIsInstance(lookup.rhs, Col)
|
|
|
|
self.assertIsNone(lookup.rhs.alias)
|
|
|
|
self.assertIsInstance(lookup.lhs, Col)
|
|
|
|
self.assertIsNone(lookup.lhs.alias)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(lookup.rhs.target, Item._meta.get_field("created"))
|
|
|
|
self.assertEqual(lookup.lhs.target, Item._meta.get_field("modified"))
|
2016-11-05 13:12:12 +00:00
|
|
|
|
|
|
|
def test_transform(self):
|
2019-09-10 03:58:29 +00:00
|
|
|
query = Query(Author, alias_cols=False)
|
2018-08-21 16:17:46 +00:00
|
|
|
with register_lookup(CharField, Lower):
|
2022-02-03 19:24:19 +00:00
|
|
|
where = query.build_where(~Q(name__lower="foo"))
|
2016-11-05 13:12:12 +00:00
|
|
|
lookup = where.children[0]
|
|
|
|
self.assertIsInstance(lookup, Exact)
|
|
|
|
self.assertIsInstance(lookup.lhs, Lower)
|
2019-09-10 03:58:29 +00:00
|
|
|
self.assertIsInstance(lookup.lhs.lhs, Col)
|
|
|
|
self.assertIsNone(lookup.lhs.lhs.alias)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(lookup.lhs.lhs.target, Author._meta.get_field("name"))
|
2016-11-05 13:12:12 +00:00
|
|
|
|
|
|
|
def test_negated_nullable(self):
|
|
|
|
query = Query(Item)
|
|
|
|
where = query.build_where(~Q(modified__lt=datetime(2017, 1, 1)))
|
|
|
|
self.assertTrue(where.negated)
|
|
|
|
lookup = where.children[0]
|
|
|
|
self.assertIsInstance(lookup, LessThan)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(lookup.lhs.target, Item._meta.get_field("modified"))
|
2016-11-05 13:12:12 +00:00
|
|
|
lookup = where.children[1]
|
|
|
|
self.assertIsInstance(lookup, IsNull)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(lookup.lhs.target, Item._meta.get_field("modified"))
|
2016-11-05 13:12:12 +00:00
|
|
|
|
|
|
|
def test_foreign_key(self):
|
|
|
|
query = Query(Item)
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "Joined field references are not permitted in this query"
|
2016-11-05 13:12:12 +00:00
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
query.build_where(Q(creator__num__gt=2))
|
|
|
|
|
|
|
|
def test_foreign_key_f(self):
|
|
|
|
query = Query(Ranking)
|
|
|
|
with self.assertRaises(FieldError):
|
2022-02-03 19:24:19 +00:00
|
|
|
query.build_where(Q(rank__gt=F("author__num")))
|
2016-11-05 13:12:12 +00:00
|
|
|
|
|
|
|
def test_foreign_key_exclusive(self):
|
2019-09-10 03:58:29 +00:00
|
|
|
query = Query(ObjectC, alias_cols=False)
|
2016-11-05 13:12:12 +00:00
|
|
|
where = query.build_where(Q(objecta=None) | Q(objectb=None))
|
|
|
|
a_isnull = where.children[0]
|
|
|
|
self.assertIsInstance(a_isnull, RelatedIsNull)
|
2019-09-10 03:58:29 +00:00
|
|
|
self.assertIsInstance(a_isnull.lhs, Col)
|
|
|
|
self.assertIsNone(a_isnull.lhs.alias)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(a_isnull.lhs.target, ObjectC._meta.get_field("objecta"))
|
2016-11-05 13:12:12 +00:00
|
|
|
b_isnull = where.children[1]
|
|
|
|
self.assertIsInstance(b_isnull, RelatedIsNull)
|
2019-09-10 03:58:29 +00:00
|
|
|
self.assertIsInstance(b_isnull.lhs, Col)
|
|
|
|
self.assertIsNone(b_isnull.lhs.alias)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(b_isnull.lhs.target, ObjectC._meta.get_field("objectb"))
|
2019-09-23 19:51:43 +00:00
|
|
|
|
|
|
|
def test_clone_select_related(self):
|
|
|
|
query = Query(Item)
|
2022-02-03 19:24:19 +00:00
|
|
|
query.add_select_related(["creator"])
|
2019-09-23 19:51:43 +00:00
|
|
|
clone = query.clone()
|
2022-02-03 19:24:19 +00:00
|
|
|
clone.add_select_related(["note", "creator__extra"])
|
|
|
|
self.assertEqual(query.select_related, {"creator": {}})
|
2019-11-10 08:48:36 +00:00
|
|
|
|
|
|
|
def test_iterable_lookup_value(self):
|
|
|
|
query = Query(Item)
|
2022-02-03 19:24:19 +00:00
|
|
|
where = query.build_where(Q(name=["a", "b"]))
|
2019-11-10 08:48:36 +00:00
|
|
|
name_exact = where.children[0]
|
|
|
|
self.assertIsInstance(name_exact, Exact)
|
|
|
|
self.assertEqual(name_exact.rhs, "['a', 'b']")
|
2019-11-15 05:08:36 +00:00
|
|
|
|
|
|
|
def test_filter_conditional(self):
|
|
|
|
query = Query(Item)
|
|
|
|
where = query.build_where(Func(output_field=BooleanField()))
|
|
|
|
exact = where.children[0]
|
|
|
|
self.assertIsInstance(exact, Exact)
|
|
|
|
self.assertIsInstance(exact.lhs, Func)
|
|
|
|
self.assertIs(exact.rhs, True)
|
|
|
|
|
|
|
|
def test_filter_conditional_join(self):
|
|
|
|
query = Query(Item)
|
2022-02-03 19:24:19 +00:00
|
|
|
filter_expr = Func("note__note", output_field=BooleanField())
|
|
|
|
msg = "Joined field references are not permitted in this query"
|
2019-11-15 05:08:36 +00:00
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
query.build_where(filter_expr)
|
2019-11-15 05:24:21 +00:00
|
|
|
|
|
|
|
def test_filter_non_conditional(self):
|
|
|
|
query = Query(Item)
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "Cannot filter against a non-conditional expression."
|
2019-11-15 05:24:21 +00:00
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
query.build_where(Func(output_field=CharField()))
|
2021-03-26 07:14:09 +00:00
|
|
|
|
|
|
|
|
2022-01-31 14:51:38 +00:00
|
|
|
class TestQueryNoModel(TestCase):
|
|
|
|
def test_rawsql_annotation(self):
|
|
|
|
query = Query(None)
|
2022-12-02 09:43:49 +00:00
|
|
|
sql = "%s = 1"
|
2022-01-31 14:51:38 +00:00
|
|
|
# Wrap with a CASE WHEN expression if a database backend (e.g. Oracle)
|
|
|
|
# doesn't support boolean expression in SELECT list.
|
|
|
|
if not connection.features.supports_boolean_expr_in_select_clause:
|
|
|
|
sql = f"CASE WHEN {sql} THEN 1 ELSE 0 END"
|
2022-12-02 09:43:49 +00:00
|
|
|
query.add_annotation(RawSQL(sql, (1,), BooleanField()), "_check")
|
2022-01-31 14:51:38 +00:00
|
|
|
result = query.get_compiler(using=DEFAULT_DB_ALIAS).execute_sql(SINGLE)
|
|
|
|
self.assertEqual(result[0], 1)
|
|
|
|
|
|
|
|
def test_subquery_annotation(self):
|
|
|
|
query = Query(None)
|
|
|
|
query.add_annotation(Exists(Item.objects.all()), "_check")
|
|
|
|
result = query.get_compiler(using=DEFAULT_DB_ALIAS).execute_sql(SINGLE)
|
|
|
|
self.assertEqual(result[0], 0)
|
|
|
|
|
|
|
|
@skipUnlessDBFeature("supports_boolean_expr_in_select_clause")
|
|
|
|
def test_q_annotation(self):
|
|
|
|
query = Query(None)
|
|
|
|
check = ExpressionWrapper(
|
2022-12-02 09:43:49 +00:00
|
|
|
Q(RawSQL("%s = 1", (1,), BooleanField())) | Q(Exists(Item.objects.all())),
|
2022-01-31 14:51:38 +00:00
|
|
|
BooleanField(),
|
|
|
|
)
|
|
|
|
query.add_annotation(check, "_check")
|
|
|
|
result = query.get_compiler(using=DEFAULT_DB_ALIAS).execute_sql(SINGLE)
|
|
|
|
self.assertEqual(result[0], 1)
|
|
|
|
|
|
|
|
def test_names_to_path_field(self):
|
|
|
|
query = Query(None)
|
|
|
|
query.add_annotation(Value(True), "value")
|
|
|
|
path, final_field, targets, names = query.names_to_path(["value"], opts=None)
|
|
|
|
self.assertEqual(path, [])
|
|
|
|
self.assertIsInstance(final_field, BooleanField)
|
|
|
|
self.assertEqual(len(targets), 1)
|
|
|
|
self.assertIsInstance(targets[0], BooleanField)
|
|
|
|
self.assertEqual(names, [])
|
|
|
|
|
|
|
|
def test_names_to_path_field_error(self):
|
|
|
|
query = Query(None)
|
|
|
|
msg = "Cannot resolve keyword 'nonexistent' into field."
|
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
query.names_to_path(["nonexistent"], opts=None)
|
|
|
|
|
|
|
|
def test_get_field_names_from_opts(self):
|
|
|
|
self.assertEqual(get_field_names_from_opts(None), set())
|
|
|
|
|
|
|
|
|
2021-03-26 07:14:09 +00:00
|
|
|
class JoinPromoterTest(SimpleTestCase):
|
|
|
|
def test_repr(self):
|
|
|
|
self.assertEqual(
|
2022-07-23 12:31:35 +00:00
|
|
|
repr(JoinPromoter(AND, 3, True)),
|
2021-03-26 07:14:09 +00:00
|
|
|
"JoinPromoter(connector='AND', num_children=3, negated=True)",
|
|
|
|
)
|