Debian Codegraph @

tests/db_functions/text/test_concat.py

raw
1 from unittest import skipUnless
2
3 from django.db import connection
4 from django.db.models import CharField, TextField
5 from django.db.models import Value as V
6 from django.db.models.functions import Concat, ConcatPair, Upper
7 from django.test import TestCase
8 from django.utils import timezone
9
10 from ..models import Article, Author
11
12 lorem_ipsum = """
13 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
14 tempor incididunt ut labore et dolore magna aliqua."""
15
16
17 class ConcatTests(TestCase):
18 def test_basic(self):
19 Author.objects.create(name="Jayden")
20 Author.objects.create(name="John Smith", alias="smithj", goes_by="John")
21 Author.objects.create(name="Margaret", goes_by="Maggie")
22 Author.objects.create(name="Rhonda", alias="adnohR")
23 authors = Author.objects.annotate(joined=Concat("alias", "goes_by"))
24 self.assertQuerySetEqual(
25 authors.order_by("name"),
26 [
27 "",
28 "smithjJohn",
29 "Maggie",
30 "adnohR",
31 ],
32 lambda a: a.joined,
33 )
34
35 def test_gt_two_expressions(self):
36 with self.assertRaisesMessage(
37 ValueError, "Concat must take at least two expressions"
38 ):
39 Author.objects.annotate(joined=Concat("alias"))
40
41 def test_many(self):
42 Author.objects.create(name="Jayden")
43 Author.objects.create(name="John Smith", alias="smithj", goes_by="John")
44 Author.objects.create(name="Margaret", goes_by="Maggie")
45 Author.objects.create(name="Rhonda", alias="adnohR")
46 authors = Author.objects.annotate(
47 joined=Concat("name", V(" ("), "goes_by", V(")"), output_field=CharField()),
48 )
49 self.assertQuerySetEqual(
50 authors.order_by("name"),
51 [
52 "Jayden ()",
53 "John Smith (John)",
54 "Margaret (Maggie)",
55 "Rhonda ()",
56 ],
57 lambda a: a.joined,
58 )
59
60 def test_mixed_char_text(self):
61 Article.objects.create(
62 title="The Title", text=lorem_ipsum, written=timezone.now()
63 )
64 article = Article.objects.annotate(
65 title_text=Concat("title", V(" - "), "text", output_field=TextField()),
66 ).get(title="The Title")
67 self.assertEqual(article.title + " - " + article.text, article.title_text)
68 # Wrap the concat in something else to ensure that text is returned
69 # rather than bytes.
70 article = Article.objects.annotate(
71 title_text=Upper(
72 Concat("title", V(" - "), "text", output_field=TextField())
73 ),
74 ).get(title="The Title")
75 expected = article.title + " - " + article.text
76 self.assertEqual(expected.upper(), article.title_text)
77
78 @skipUnless(
79 connection.vendor in ("sqlite", "postgresql"),
80 "SQLite and PostgreSQL specific implementation detail.",
81 )
82 def test_coalesce_idempotent(self):
83 pair = ConcatPair(V("a"), V("b"))
84 # Check nodes counts
85 self.assertEqual(len(list(pair.flatten())), 3)
86 self.assertEqual(
87 len(list(pair.coalesce().flatten())), 7
88 ) # + 2 Coalesce + 2 Value()
89 self.assertEqual(len(list(pair.flatten())), 3)
90
91 def test_sql_generation_idempotency(self):
92 qs = Article.objects.annotate(description=Concat("title", V(": "), "summary"))
93 # Multiple compilations should not alter the generated query.
94 self.assertEqual(str(qs.query), str(qs.all().query))
95
96 def test_concat_non_str(self):
97 Author.objects.create(name="The Name", age=42)
98 with self.assertNumQueries(1) as ctx:
99 author = Author.objects.annotate(
100 name_text=Concat(
101 "name", V(":"), "alias", V(":"), "age", output_field=TextField()
102 ),
103 ).get()
104 self.assertEqual(author.name_text, "The Name::42")
105 # Only non-string columns are casted on PostgreSQL.
106 self.assertEqual(
107 ctx.captured_queries[0]["sql"].count("::text"),
108 1 if connection.vendor == "postgresql" else 0,
109 )
110
111 def test_equal(self):
112 self.assertEqual(
113 Concat("foo", "bar", output_field=TextField()),
114 Concat("foo", "bar", output_field=TextField()),
115 )
116 self.assertNotEqual(
117 Concat("foo", "bar", output_field=TextField()),
118 Concat("foo", "bar", output_field=CharField()),
119 )
120 self.assertNotEqual(
121 Concat("foo", "bar", output_field=TextField()),
122 Concat("bar", "foo", output_field=TextField()),
123 )