tests.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. from __future__ import unicode_literals
  2. from django.db.models import CharField, TextField, Value as V
  3. from django.db.models.functions import (
  4. Coalesce, Concat, Length, Lower, Substr, Upper,
  5. )
  6. from django.test import TestCase
  7. from django.utils import six, timezone
  8. from .models import Article, Author
  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. # mixed Text and Char wrapped
  47. article = Article.objects.annotate(
  48. headline=Coalesce(Lower('summary'), Lower('text'), output_field=TextField()),
  49. )
  50. self.assertQuerysetEqual(
  51. article.order_by('title'), [
  52. lorem_ipsum.lower(),
  53. ],
  54. lambda a: a.headline
  55. )
  56. def test_coalesce_ordering(self):
  57. Author.objects.create(name='John Smith', alias='smithj')
  58. Author.objects.create(name='Rhonda')
  59. authors = Author.objects.order_by(Coalesce('alias', 'name'))
  60. self.assertQuerysetEqual(
  61. authors, [
  62. 'Rhonda',
  63. 'John Smith',
  64. ],
  65. lambda a: a.name
  66. )
  67. authors = Author.objects.order_by(Coalesce('alias', 'name').asc())
  68. self.assertQuerysetEqual(
  69. authors, [
  70. 'Rhonda',
  71. 'John Smith',
  72. ],
  73. lambda a: a.name
  74. )
  75. authors = Author.objects.order_by(Coalesce('alias', 'name').desc())
  76. self.assertQuerysetEqual(
  77. authors, [
  78. 'John Smith',
  79. 'Rhonda',
  80. ],
  81. lambda a: a.name
  82. )
  83. def test_concat(self):
  84. Author.objects.create(name='Jayden')
  85. Author.objects.create(name='John Smith', alias='smithj', goes_by='John')
  86. Author.objects.create(name='Margaret', goes_by='Maggie')
  87. Author.objects.create(name='Rhonda', alias='adnohR')
  88. authors = Author.objects.annotate(joined=Concat('alias', 'goes_by'))
  89. self.assertQuerysetEqual(
  90. authors.order_by('name'), [
  91. '',
  92. 'smithjJohn',
  93. 'Maggie',
  94. 'adnohR',
  95. ],
  96. lambda a: a.joined
  97. )
  98. with self.assertRaisesMessage(ValueError, 'Concat must take at least two expressions'):
  99. Author.objects.annotate(joined=Concat('alias'))
  100. def test_concat_many(self):
  101. Author.objects.create(name='Jayden')
  102. Author.objects.create(name='John Smith', alias='smithj', goes_by='John')
  103. Author.objects.create(name='Margaret', goes_by='Maggie')
  104. Author.objects.create(name='Rhonda', alias='adnohR')
  105. authors = Author.objects.annotate(
  106. joined=Concat('name', V(' ('), 'goes_by', V(')'), output_field=CharField()),
  107. )
  108. self.assertQuerysetEqual(
  109. authors.order_by('name'), [
  110. 'Jayden ()',
  111. 'John Smith (John)',
  112. 'Margaret (Maggie)',
  113. 'Rhonda ()',
  114. ],
  115. lambda a: a.joined
  116. )
  117. def test_concat_mixed_char_text(self):
  118. Article.objects.create(title='The Title', text=lorem_ipsum, written=timezone.now())
  119. article = Article.objects.annotate(
  120. title_text=Concat('title', V(' - '), 'text', output_field=TextField()),
  121. ).get(title='The Title')
  122. self.assertEqual(article.title + ' - ' + article.text, article.title_text)
  123. # wrap the concat in something else to ensure that we're still
  124. # getting text rather than bytes
  125. article = Article.objects.annotate(
  126. title_text=Upper(Concat('title', V(' - '), 'text', output_field=TextField())),
  127. ).get(title='The Title')
  128. expected = article.title + ' - ' + article.text
  129. self.assertEqual(expected.upper(), article.title_text)
  130. def test_lower(self):
  131. Author.objects.create(name='John Smith', alias='smithj')
  132. Author.objects.create(name='Rhonda')
  133. authors = Author.objects.annotate(lower_name=Lower('name'))
  134. self.assertQuerysetEqual(
  135. authors.order_by('name'), [
  136. 'john smith',
  137. 'rhonda',
  138. ],
  139. lambda a: a.lower_name
  140. )
  141. Author.objects.update(name=Lower('name'))
  142. self.assertQuerysetEqual(
  143. authors.order_by('name'), [
  144. ('john smith', 'john smith'),
  145. ('rhonda', 'rhonda'),
  146. ],
  147. lambda a: (a.lower_name, a.name)
  148. )
  149. def test_upper(self):
  150. Author.objects.create(name='John Smith', alias='smithj')
  151. Author.objects.create(name='Rhonda')
  152. authors = Author.objects.annotate(upper_name=Upper('name'))
  153. self.assertQuerysetEqual(
  154. authors.order_by('name'), [
  155. 'JOHN SMITH',
  156. 'RHONDA',
  157. ],
  158. lambda a: a.upper_name
  159. )
  160. Author.objects.update(name=Upper('name'))
  161. self.assertQuerysetEqual(
  162. authors.order_by('name'), [
  163. ('JOHN SMITH', 'JOHN SMITH'),
  164. ('RHONDA', 'RHONDA'),
  165. ],
  166. lambda a: (a.upper_name, a.name)
  167. )
  168. def test_length(self):
  169. Author.objects.create(name='John Smith', alias='smithj')
  170. Author.objects.create(name='Rhonda')
  171. authors = Author.objects.annotate(
  172. name_length=Length('name'),
  173. alias_length=Length('alias'))
  174. self.assertQuerysetEqual(
  175. authors.order_by('name'), [
  176. (10, 6),
  177. (6, None),
  178. ],
  179. lambda a: (a.name_length, a.alias_length)
  180. )
  181. self.assertEqual(authors.filter(alias_length__lte=Length('name')).count(), 1)
  182. def test_length_ordering(self):
  183. Author.objects.create(name='John Smith', alias='smithj')
  184. Author.objects.create(name='John Smith', alias='smithj1')
  185. Author.objects.create(name='Rhonda', alias='ronny')
  186. authors = Author.objects.order_by(Length('name'), Length('alias'))
  187. self.assertQuerysetEqual(
  188. authors, [
  189. ('Rhonda', 'ronny'),
  190. ('John Smith', 'smithj'),
  191. ('John Smith', 'smithj1'),
  192. ],
  193. lambda a: (a.name, a.alias)
  194. )
  195. def test_substr(self):
  196. Author.objects.create(name='John Smith', alias='smithj')
  197. Author.objects.create(name='Rhonda')
  198. authors = Author.objects.annotate(name_part=Substr('name', 5, 3))
  199. self.assertQuerysetEqual(
  200. authors.order_by('name'), [
  201. ' Sm',
  202. 'da',
  203. ],
  204. lambda a: a.name_part
  205. )
  206. authors = Author.objects.annotate(name_part=Substr('name', 2))
  207. self.assertQuerysetEqual(
  208. authors.order_by('name'), [
  209. 'ohn Smith',
  210. 'honda',
  211. ],
  212. lambda a: a.name_part
  213. )
  214. # if alias is null, set to first 5 lower characters of the name
  215. Author.objects.filter(alias__isnull=True).update(
  216. alias=Lower(Substr('name', 1, 5)),
  217. )
  218. self.assertQuerysetEqual(
  219. authors.order_by('name'), [
  220. 'smithj',
  221. 'rhond',
  222. ],
  223. lambda a: a.alias
  224. )
  225. def test_substr_start(self):
  226. Author.objects.create(name='John Smith', alias='smithj')
  227. a = Author.objects.annotate(
  228. name_part_1=Substr('name', 1),
  229. name_part_2=Substr('name', 2),
  230. ).get(alias='smithj')
  231. self.assertEqual(a.name_part_1[1:], a.name_part_2)
  232. with six.assertRaisesRegex(self, ValueError, "'pos' must be greater than 0"):
  233. Author.objects.annotate(raises=Substr('name', 0))
  234. def test_substr_with_expressions(self):
  235. Author.objects.create(name='John Smith', alias='smithj')
  236. Author.objects.create(name='Rhonda')
  237. authors = Author.objects.annotate(name_part=Substr('name', 5, 3))
  238. self.assertQuerysetEqual(
  239. authors.order_by('name'), [
  240. ' Sm',
  241. 'da',
  242. ],
  243. lambda a: a.name_part
  244. )
  245. def test_nested_function_ordering(self):
  246. Author.objects.create(name='John Smith')
  247. Author.objects.create(name='Rhonda Simpson', alias='ronny')
  248. authors = Author.objects.order_by(Length(Coalesce('alias', 'name')))
  249. self.assertQuerysetEqual(
  250. authors, [
  251. 'Rhonda Simpson',
  252. 'John Smith',
  253. ],
  254. lambda a: a.name
  255. )
  256. authors = Author.objects.order_by(Length(Coalesce('alias', 'name')).desc())
  257. self.assertQuerysetEqual(
  258. authors, [
  259. 'John Smith',
  260. 'Rhonda Simpson',
  261. ],
  262. lambda a: a.name
  263. )