2022-02-03 19:24:19 +00:00
|
|
|
from django.db.models import CharField
|
|
|
|
from django.db.models import Value as V
|
2018-08-16 00:14:37 +00:00
|
|
|
from django.db.models.functions import Coalesce, Length, Upper
|
2018-08-16 00:06:19 +00:00
|
|
|
from django.test import TestCase
|
2018-08-21 16:17:46 +00:00
|
|
|
from django.test.utils import register_lookup
|
2014-11-22 03:14:43 +00:00
|
|
|
|
2018-08-16 00:14:37 +00:00
|
|
|
from .models import Author
|
2014-11-22 03:14:43 +00:00
|
|
|
|
|
|
|
|
2018-08-21 16:17:46 +00:00
|
|
|
class UpperBilateral(Upper):
|
|
|
|
bilateral = True
|
|
|
|
|
|
|
|
|
2014-11-22 03:14:43 +00:00
|
|
|
class FunctionTests(TestCase):
|
2015-01-09 15:16:16 +00:00
|
|
|
def test_nested_function_ordering(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
Author.objects.create(name="John Smith")
|
|
|
|
Author.objects.create(name="Rhonda Simpson", alias="ronny")
|
2015-01-09 15:16:16 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
authors = Author.objects.order_by(Length(Coalesce("alias", "name")))
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
authors,
|
|
|
|
[
|
|
|
|
"Rhonda Simpson",
|
|
|
|
"John Smith",
|
2015-01-09 15:16:16 +00:00
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda a: a.name,
|
2015-01-09 15:16:16 +00:00
|
|
|
)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
authors = Author.objects.order_by(Length(Coalesce("alias", "name")).desc())
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
authors,
|
|
|
|
[
|
|
|
|
"John Smith",
|
|
|
|
"Rhonda Simpson",
|
2015-01-09 15:16:16 +00:00
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda a: a.name,
|
2015-01-09 15:16:16 +00:00
|
|
|
)
|
2015-05-29 14:54:10 +00:00
|
|
|
|
2015-08-03 02:30:06 +00:00
|
|
|
def test_func_transform_bilateral(self):
|
2018-08-21 16:17:46 +00:00
|
|
|
with register_lookup(CharField, UpperBilateral):
|
2022-02-03 19:24:19 +00:00
|
|
|
Author.objects.create(name="John Smith", alias="smithj")
|
|
|
|
Author.objects.create(name="Rhonda")
|
|
|
|
authors = Author.objects.filter(name__upper__exact="john smith")
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
authors.order_by("name"),
|
|
|
|
[
|
|
|
|
"John Smith",
|
2015-08-03 02:30:06 +00:00
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda a: a.name,
|
2015-08-03 02:30:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_func_transform_bilateral_multivalue(self):
|
2018-08-21 16:17:46 +00:00
|
|
|
with register_lookup(CharField, UpperBilateral):
|
2022-02-03 19:24:19 +00:00
|
|
|
Author.objects.create(name="John Smith", alias="smithj")
|
|
|
|
Author.objects.create(name="Rhonda")
|
|
|
|
authors = Author.objects.filter(name__upper__in=["john smith", "rhonda"])
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
authors.order_by("name"),
|
|
|
|
[
|
|
|
|
"John Smith",
|
|
|
|
"Rhonda",
|
2015-08-03 02:30:06 +00:00
|
|
|
],
|
2022-02-03 19:24:19 +00:00
|
|
|
lambda a: a.name,
|
2015-08-03 02:30:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_function_as_filter(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
Author.objects.create(name="John Smith", alias="SMITHJ")
|
|
|
|
Author.objects.create(name="Rhonda")
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Author.objects.filter(alias=Upper(V("smithj"))),
|
|
|
|
["John Smith"],
|
|
|
|
lambda x: x.name,
|
2015-08-03 02:30:06 +00:00
|
|
|
)
|
2022-09-24 10:29:58 +00:00
|
|
|
self.assertQuerySetEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
Author.objects.exclude(alias=Upper(V("smithj"))),
|
|
|
|
["Rhonda"],
|
|
|
|
lambda x: x.name,
|
2015-08-03 02:30:06 +00:00
|
|
|
)
|