tests.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. from __future__ import unicode_literals
  2. from django.db.models import TextField, CharField, Value as V
  3. from django.db.models.functions import (
  4. Coalesce, Concat, Lower, Upper, Length, Substr,
  5. )
  6. from django.test import TestCase
  7. from django.utils import six, timezone
  8. from .models import Author, Article
  9. lorem_ipsum = """
  10. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
  11. tempor incididunt ut labore et dolore magna aliqua."""
  12. class FunctionTests(TestCase):
  13. def test_coalesce(self):
  14. Author.objects.create(name='John Smith', alias='smithj')
  15. Author.objects.create(name='Rhonda')
  16. authors = Author.objects.annotate(display_name=Coalesce('alias', 'name'))
  17. self.assertQuerysetEqual(
  18. authors.order_by('name'), [
  19. 'smithj',
  20. 'Rhonda',
  21. ],
  22. lambda a: a.display_name
  23. )
  24. with self.assertRaisesMessage(ValueError, 'Coalesce must take at least two expressions'):
  25. Author.objects.annotate(display_name=Coalesce('alias'))
  26. def test_coalesce_mixed_values(self):
  27. a1 = Author.objects.create(name='John Smith', alias='smithj')
  28. a2 = Author.objects.create(name='Rhonda')
  29. ar1 = Article.objects.create(
  30. title="How to Django",
  31. text=lorem_ipsum,
  32. written=timezone.now(),
  33. )
  34. ar1.authors.add(a1)
  35. ar1.authors.add(a2)
  36. # mixed Text and Char
  37. article = Article.objects.annotate(
  38. headline=Coalesce('summary', 'text', output_field=TextField()),
  39. )
  40. self.assertQuerysetEqual(
  41. article.order_by('title'), [
  42. lorem_ipsum,
  43. ],
  44. lambda a: a.headline
  45. )
  46. def test_concat(self):
  47. Author.objects.create(name='Jayden')
  48. Author.objects.create(name='John Smith', alias='smithj', goes_by='John')
  49. Author.objects.create(name='Margaret', goes_by='Maggie')
  50. Author.objects.create(name='Rhonda', alias='adnohR')
  51. authors = Author.objects.annotate(joined=Concat('alias', 'goes_by'))
  52. self.assertQuerysetEqual(
  53. authors.order_by('name'), [
  54. '',
  55. 'smithjJohn',
  56. 'Maggie',
  57. 'adnohR',
  58. ],
  59. lambda a: a.joined
  60. )
  61. with self.assertRaisesMessage(ValueError, 'Concat must take at least two expressions'):
  62. Author.objects.annotate(joined=Concat('alias'))
  63. def test_concat_many(self):
  64. Author.objects.create(name='Jayden')
  65. Author.objects.create(name='John Smith', alias='smithj', goes_by='John')
  66. Author.objects.create(name='Margaret', goes_by='Maggie')
  67. Author.objects.create(name='Rhonda', alias='adnohR')
  68. authors = Author.objects.annotate(
  69. joined=Concat('name', V(' ('), 'goes_by', V(')'), output_field=CharField()),
  70. )
  71. self.assertQuerysetEqual(
  72. authors.order_by('name'), [
  73. 'Jayden ()',
  74. 'John Smith (John)',
  75. 'Margaret (Maggie)',
  76. 'Rhonda ()',
  77. ],
  78. lambda a: a.joined
  79. )
  80. def test_concat_mixed_char_text(self):
  81. Article.objects.create(title='The Title', text=lorem_ipsum, written=timezone.now())
  82. article = Article.objects.annotate(
  83. title_text=Concat('title', V(' - '), 'text', output_field=TextField()),
  84. ).get(title='The Title')
  85. self.assertEqual(article.title + ' - ' + article.text, article.title_text)
  86. # wrap the concat in something else to ensure that we're still
  87. # getting text rather than bytes
  88. article = Article.objects.annotate(
  89. title_text=Upper(Concat('title', V(' - '), 'text', output_field=TextField())),
  90. ).get(title='The Title')
  91. expected = article.title + ' - ' + article.text
  92. self.assertEqual(expected.upper(), article.title_text)
  93. def test_lower(self):
  94. Author.objects.create(name='John Smith', alias='smithj')
  95. Author.objects.create(name='Rhonda')
  96. authors = Author.objects.annotate(lower_name=Lower('name'))
  97. self.assertQuerysetEqual(
  98. authors.order_by('name'), [
  99. 'john smith',
  100. 'rhonda',
  101. ],
  102. lambda a: a.lower_name
  103. )
  104. Author.objects.update(name=Lower('name'))
  105. self.assertQuerysetEqual(
  106. authors.order_by('name'), [
  107. ('john smith', 'john smith'),
  108. ('rhonda', 'rhonda'),
  109. ],
  110. lambda a: (a.lower_name, a.name)
  111. )
  112. def test_upper(self):
  113. Author.objects.create(name='John Smith', alias='smithj')
  114. Author.objects.create(name='Rhonda')
  115. authors = Author.objects.annotate(upper_name=Upper('name'))
  116. self.assertQuerysetEqual(
  117. authors.order_by('name'), [
  118. 'JOHN SMITH',
  119. 'RHONDA',
  120. ],
  121. lambda a: a.upper_name
  122. )
  123. Author.objects.update(name=Upper('name'))
  124. self.assertQuerysetEqual(
  125. authors.order_by('name'), [
  126. ('JOHN SMITH', 'JOHN SMITH'),
  127. ('RHONDA', 'RHONDA'),
  128. ],
  129. lambda a: (a.upper_name, a.name)
  130. )
  131. def test_length(self):
  132. Author.objects.create(name='John Smith', alias='smithj')
  133. Author.objects.create(name='Rhonda')
  134. authors = Author.objects.annotate(
  135. name_length=Length('name'),
  136. alias_length=Length('alias'))
  137. self.assertQuerysetEqual(
  138. authors.order_by('name'), [
  139. (10, 6),
  140. (6, None),
  141. ],
  142. lambda a: (a.name_length, a.alias_length)
  143. )
  144. self.assertEqual(authors.filter(alias_length__lte=Length('name')).count(), 1)
  145. def test_substr(self):
  146. Author.objects.create(name='John Smith', alias='smithj')
  147. Author.objects.create(name='Rhonda')
  148. authors = Author.objects.annotate(name_part=Substr('name', 5, 3))
  149. self.assertQuerysetEqual(
  150. authors.order_by('name'), [
  151. ' Sm',
  152. 'da',
  153. ],
  154. lambda a: a.name_part
  155. )
  156. authors = Author.objects.annotate(name_part=Substr('name', 2))
  157. self.assertQuerysetEqual(
  158. authors.order_by('name'), [
  159. 'ohn Smith',
  160. 'honda',
  161. ],
  162. lambda a: a.name_part
  163. )
  164. # if alias is null, set to first 5 lower characters of the name
  165. Author.objects.filter(alias__isnull=True).update(
  166. alias=Lower(Substr('name', 1, 5)),
  167. )
  168. self.assertQuerysetEqual(
  169. authors.order_by('name'), [
  170. 'smithj',
  171. 'rhond',
  172. ],
  173. lambda a: a.alias
  174. )
  175. def test_substr_start(self):
  176. Author.objects.create(name='John Smith', alias='smithj')
  177. a = Author.objects.annotate(
  178. name_part_1=Substr('name', 1),
  179. name_part_2=Substr('name', 2),
  180. ).get(alias='smithj')
  181. self.assertEqual(a.name_part_1[1:], a.name_part_2)
  182. with six.assertRaisesRegex(self, ValueError, "'pos' must be greater than 0"):
  183. Author.objects.annotate(raises=Substr('name', 0))