2021-09-29 06:00:50 +08:00
|
|
|
from django.db.models import Subquery, TextField
|
2018-08-16 08:06:19 +08:00
|
|
|
from django.db.models.functions import Coalesce, Lower
|
|
|
|
from django.test import TestCase
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from ..models import Article, Author
|
|
|
|
|
|
|
|
lorem_ipsum = """
|
|
|
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
|
|
|
|
tempor incididunt ut labore et dolore magna aliqua."""
|
|
|
|
|
|
|
|
|
|
|
|
class CoalesceTests(TestCase):
|
|
|
|
def test_basic(self):
|
|
|
|
Author.objects.create(name="John Smith", alias="smithj")
|
|
|
|
Author.objects.create(name="Rhonda")
|
|
|
|
authors = Author.objects.annotate(display_name=Coalesce("alias", "name"))
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2018-08-16 08:06:19 +08:00
|
|
|
authors.order_by("name"), ["smithj", "Rhonda"], lambda a: a.display_name
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_gt_two_expressions(self):
|
|
|
|
with self.assertRaisesMessage(
|
|
|
|
ValueError, "Coalesce must take at least two expressions"
|
|
|
|
):
|
|
|
|
Author.objects.annotate(display_name=Coalesce("alias"))
|
|
|
|
|
|
|
|
def test_mixed_values(self):
|
|
|
|
a1 = Author.objects.create(name="John Smith", alias="smithj")
|
|
|
|
a2 = Author.objects.create(name="Rhonda")
|
|
|
|
ar1 = Article.objects.create(
|
|
|
|
title="How to Django",
|
|
|
|
text=lorem_ipsum,
|
|
|
|
written=timezone.now(),
|
|
|
|
)
|
|
|
|
ar1.authors.add(a1)
|
|
|
|
ar1.authors.add(a2)
|
|
|
|
# mixed Text and Char
|
|
|
|
article = Article.objects.annotate(
|
|
|
|
headline=Coalesce("summary", "text", output_field=TextField()),
|
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2018-08-16 08:06:19 +08:00
|
|
|
article.order_by("title"), [lorem_ipsum], lambda a: a.headline
|
|
|
|
)
|
|
|
|
# mixed Text and Char wrapped
|
|
|
|
article = Article.objects.annotate(
|
|
|
|
headline=Coalesce(
|
|
|
|
Lower("summary"), Lower("text"), output_field=TextField()
|
|
|
|
),
|
|
|
|
)
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2018-08-16 08:06:19 +08:00
|
|
|
article.order_by("title"), [lorem_ipsum.lower()], lambda a: a.headline
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_ordering(self):
|
|
|
|
Author.objects.create(name="John Smith", alias="smithj")
|
|
|
|
Author.objects.create(name="Rhonda")
|
|
|
|
authors = Author.objects.order_by(Coalesce("alias", "name"))
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(authors, ["Rhonda", "John Smith"], lambda a: a.name)
|
2018-08-16 08:06:19 +08:00
|
|
|
authors = Author.objects.order_by(Coalesce("alias", "name").asc())
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(authors, ["Rhonda", "John Smith"], lambda a: a.name)
|
2018-08-16 08:06:19 +08:00
|
|
|
authors = Author.objects.order_by(Coalesce("alias", "name").desc())
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(authors, ["John Smith", "Rhonda"], lambda a: a.name)
|
2021-09-29 06:00:50 +08:00
|
|
|
|
|
|
|
def test_empty_queryset(self):
|
|
|
|
Author.objects.create(name="John Smith")
|
2022-08-24 11:06:04 +08:00
|
|
|
queryset = Author.objects.values("id")
|
2021-09-29 06:00:50 +08:00
|
|
|
tests = [
|
2022-08-24 11:06:04 +08:00
|
|
|
(queryset.none(), "QuerySet.none()"),
|
|
|
|
(queryset.filter(id=0), "QuerySet.filter(id=0)"),
|
|
|
|
(Subquery(queryset.none()), "Subquery(QuerySet.none())"),
|
|
|
|
(Subquery(queryset.filter(id=0)), "Subquery(Queryset.filter(id=0)"),
|
2021-09-29 06:00:50 +08:00
|
|
|
]
|
2022-08-24 11:06:04 +08:00
|
|
|
for empty_query, description in tests:
|
|
|
|
with self.subTest(description), self.assertNumQueries(1):
|
2021-09-29 06:00:50 +08:00
|
|
|
qs = Author.objects.annotate(annotation=Coalesce(empty_query, 42))
|
|
|
|
self.assertEqual(qs.first().annotation, 42)
|