tests.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. from __future__ import unicode_literals
  2. import datetime
  3. from decimal import Decimal
  4. from django.core.exceptions import FieldDoesNotExist, FieldError
  5. from django.db.models import (
  6. Sum, Count,
  7. F, Value, Func,
  8. IntegerField, BooleanField, CharField)
  9. from django.test import TestCase
  10. from django.utils import six
  11. from .models import Author, Book, Store, DepartmentStore, Company, Employee
  12. def cxOracle_513_py3_bug(func):
  13. """
  14. cx_Oracle versions up to and including 5.1.3 have a bug with respect to
  15. string handling under Python3 (essentially, they treat Python3 strings
  16. as Python2 strings rather than unicode). This makes some tests here
  17. fail under Python 3 -- so we mark them as expected failures.
  18. See https://code.djangoproject.com/ticket/23843, in particular comment 6,
  19. which points to https://bitbucket.org/anthony_tuininga/cx_oracle/issue/6/
  20. """
  21. from unittest import expectedFailure
  22. from django.db import connection
  23. if connection.vendor == 'oracle' and six.PY3 and connection.Database.version <= '5.1.3':
  24. return expectedFailure(func)
  25. else:
  26. return func
  27. class NonAggregateAnnotationTestCase(TestCase):
  28. fixtures = ["annotations.json"]
  29. def test_basic_annotation(self):
  30. books = Book.objects.annotate(
  31. is_book=Value(1, output_field=IntegerField()))
  32. for book in books:
  33. self.assertEqual(book.is_book, 1)
  34. def test_basic_f_annotation(self):
  35. books = Book.objects.annotate(another_rating=F('rating'))
  36. for book in books:
  37. self.assertEqual(book.another_rating, book.rating)
  38. def test_joined_annotation(self):
  39. books = Book.objects.select_related('publisher').annotate(
  40. num_awards=F('publisher__num_awards'))
  41. for book in books:
  42. self.assertEqual(book.num_awards, book.publisher.num_awards)
  43. def test_annotate_with_aggregation(self):
  44. books = Book.objects.annotate(
  45. is_book=Value(1, output_field=IntegerField()),
  46. rating_count=Count('rating'))
  47. for book in books:
  48. self.assertEqual(book.is_book, 1)
  49. self.assertEqual(book.rating_count, 1)
  50. def test_aggregate_over_annotation(self):
  51. agg = Author.objects.annotate(other_age=F('age')).aggregate(otherage_sum=Sum('other_age'))
  52. other_agg = Author.objects.aggregate(age_sum=Sum('age'))
  53. self.assertEqual(agg['otherage_sum'], other_agg['age_sum'])
  54. def test_filter_annotation(self):
  55. books = Book.objects.annotate(
  56. is_book=Value(1, output_field=IntegerField())
  57. ).filter(is_book=1)
  58. for book in books:
  59. self.assertEqual(book.is_book, 1)
  60. def test_filter_annotation_with_f(self):
  61. books = Book.objects.annotate(
  62. other_rating=F('rating')
  63. ).filter(other_rating=3.5)
  64. for book in books:
  65. self.assertEqual(book.other_rating, 3.5)
  66. def test_filter_annotation_with_double_f(self):
  67. books = Book.objects.annotate(
  68. other_rating=F('rating')
  69. ).filter(other_rating=F('rating'))
  70. for book in books:
  71. self.assertEqual(book.other_rating, book.rating)
  72. def test_filter_agg_with_double_f(self):
  73. books = Book.objects.annotate(
  74. sum_rating=Sum('rating')
  75. ).filter(sum_rating=F('sum_rating'))
  76. for book in books:
  77. self.assertEqual(book.sum_rating, book.rating)
  78. def test_filter_wrong_annotation(self):
  79. with six.assertRaisesRegex(self, FieldError, "Cannot resolve keyword .*"):
  80. list(Book.objects.annotate(
  81. sum_rating=Sum('rating')
  82. ).filter(sum_rating=F('nope')))
  83. def test_update_with_annotation(self):
  84. book_preupdate = Book.objects.get(pk=2)
  85. Book.objects.annotate(other_rating=F('rating') - 1).update(rating=F('other_rating'))
  86. book_postupdate = Book.objects.get(pk=2)
  87. self.assertEqual(book_preupdate.rating - 1, book_postupdate.rating)
  88. def test_annotation_with_m2m(self):
  89. books = Book.objects.annotate(author_age=F('authors__age')).filter(pk=1).order_by('author_age')
  90. self.assertEqual(books[0].author_age, 34)
  91. self.assertEqual(books[1].author_age, 35)
  92. def test_annotation_reverse_m2m(self):
  93. books = Book.objects.annotate(
  94. store_name=F('store__name')).filter(
  95. name='Practical Django Projects').order_by(
  96. 'store_name')
  97. self.assertQuerysetEqual(
  98. books, [
  99. 'Amazon.com',
  100. 'Books.com',
  101. 'Mamma and Pappa\'s Books'
  102. ],
  103. lambda b: b.store_name
  104. )
  105. def test_values_annotation(self):
  106. """
  107. Annotations can reference fields in a values clause,
  108. and contribute to an existing values clause.
  109. """
  110. # annotate references a field in values()
  111. qs = Book.objects.values('rating').annotate(other_rating=F('rating') - 1)
  112. book = qs.get(pk=1)
  113. self.assertEqual(book['rating'] - 1, book['other_rating'])
  114. # filter refs the annotated value
  115. book = qs.get(other_rating=4)
  116. self.assertEqual(book['other_rating'], 4)
  117. # can annotate an existing values with a new field
  118. book = qs.annotate(other_isbn=F('isbn')).get(other_rating=4)
  119. self.assertEqual(book['other_rating'], 4)
  120. self.assertEqual(book['other_isbn'], '155860191')
  121. def test_defer_annotation(self):
  122. """
  123. Deferred attributes can be referenced by an annotation,
  124. but they are not themselves deferred, and cannot be deferred.
  125. """
  126. qs = Book.objects.defer('rating').annotate(other_rating=F('rating') - 1)
  127. with self.assertNumQueries(2):
  128. book = qs.get(other_rating=4)
  129. self.assertEqual(book.rating, 5)
  130. self.assertEqual(book.other_rating, 4)
  131. with six.assertRaisesRegex(self, FieldDoesNotExist, "\w has no field named u?'other_rating'"):
  132. book = qs.defer('other_rating').get(other_rating=4)
  133. def test_mti_annotations(self):
  134. """
  135. Fields on an inherited model can be referenced by an
  136. annotated field.
  137. """
  138. d = DepartmentStore.objects.create(
  139. name='Angus & Robinson',
  140. original_opening=datetime.date(2014, 3, 8),
  141. friday_night_closing=datetime.time(21, 00, 00),
  142. chain='Westfield'
  143. )
  144. books = Book.objects.filter(rating__gt=4)
  145. for b in books:
  146. d.books.add(b)
  147. qs = DepartmentStore.objects.annotate(
  148. other_name=F('name'),
  149. other_chain=F('chain'),
  150. is_open=Value(True, BooleanField()),
  151. book_isbn=F('books__isbn')
  152. ).order_by('book_isbn').filter(chain='Westfield')
  153. self.assertQuerysetEqual(
  154. qs, [
  155. ('Angus & Robinson', 'Westfield', True, '155860191'),
  156. ('Angus & Robinson', 'Westfield', True, '159059725')
  157. ],
  158. lambda d: (d.other_name, d.other_chain, d.is_open, d.book_isbn)
  159. )
  160. def test_column_field_ordering(self):
  161. """
  162. Test that columns are aligned in the correct order for
  163. resolve_columns. This test will fail on mysql if column
  164. ordering is out. Column fields should be aligned as:
  165. 1. extra_select
  166. 2. model_fields
  167. 3. annotation_fields
  168. 4. model_related_fields
  169. """
  170. store = Store.objects.first()
  171. Employee.objects.create(id=1, first_name='Max', manager=True, last_name='Paine',
  172. store=store, age=23, salary=Decimal(50000.00))
  173. Employee.objects.create(id=2, first_name='Buffy', manager=False, last_name='Summers',
  174. store=store, age=18, salary=Decimal(40000.00))
  175. qs = Employee.objects.extra(
  176. select={'random_value': '42'}
  177. ).select_related('store').annotate(
  178. annotated_value=Value(17, output_field=IntegerField())
  179. )
  180. rows = [
  181. (1, 'Max', True, 42, 'Paine', 23, Decimal(50000.00), store.name, 17),
  182. (2, 'Buffy', False, 42, 'Summers', 18, Decimal(40000.00), store.name, 17)
  183. ]
  184. self.assertQuerysetEqual(
  185. qs.order_by('id'), rows,
  186. lambda e: (
  187. e.id, e.first_name, e.manager, e.random_value, e.last_name, e.age,
  188. e.salary, e.store.name, e.annotated_value))
  189. def test_column_field_ordering_with_deferred(self):
  190. store = Store.objects.first()
  191. Employee.objects.create(id=1, first_name='Max', manager=True, last_name='Paine',
  192. store=store, age=23, salary=Decimal(50000.00))
  193. Employee.objects.create(id=2, first_name='Buffy', manager=False, last_name='Summers',
  194. store=store, age=18, salary=Decimal(40000.00))
  195. qs = Employee.objects.extra(
  196. select={'random_value': '42'}
  197. ).select_related('store').annotate(
  198. annotated_value=Value(17, output_field=IntegerField())
  199. )
  200. rows = [
  201. (1, 'Max', True, 42, 'Paine', 23, Decimal(50000.00), store.name, 17),
  202. (2, 'Buffy', False, 42, 'Summers', 18, Decimal(40000.00), store.name, 17)
  203. ]
  204. # and we respect deferred columns!
  205. self.assertQuerysetEqual(
  206. qs.defer('age').order_by('id'), rows,
  207. lambda e: (
  208. e.id, e.first_name, e.manager, e.random_value, e.last_name, e.age,
  209. e.salary, e.store.name, e.annotated_value))
  210. @cxOracle_513_py3_bug
  211. def test_custom_functions(self):
  212. Company(name='Apple', motto=None, ticker_name='APPL', description='Beautiful Devices').save()
  213. Company(name='Django Software Foundation', motto=None, ticker_name=None, description=None).save()
  214. Company(name='Google', motto='Do No Evil', ticker_name='GOOG', description='Internet Company').save()
  215. Company(name='Yahoo', motto=None, ticker_name=None, description='Internet Company').save()
  216. qs = Company.objects.annotate(
  217. tagline=Func(
  218. F('motto'),
  219. F('ticker_name'),
  220. F('description'),
  221. Value('No Tag'),
  222. function='COALESCE')
  223. ).order_by('name')
  224. self.assertQuerysetEqual(
  225. qs, [
  226. ('Apple', 'APPL'),
  227. ('Django Software Foundation', 'No Tag'),
  228. ('Google', 'Do No Evil'),
  229. ('Yahoo', 'Internet Company')
  230. ],
  231. lambda c: (c.name, c.tagline)
  232. )
  233. @cxOracle_513_py3_bug
  234. def test_custom_functions_can_ref_other_functions(self):
  235. Company(name='Apple', motto=None, ticker_name='APPL', description='Beautiful Devices').save()
  236. Company(name='Django Software Foundation', motto=None, ticker_name=None, description=None).save()
  237. Company(name='Google', motto='Do No Evil', ticker_name='GOOG', description='Internet Company').save()
  238. Company(name='Yahoo', motto=None, ticker_name=None, description='Internet Company').save()
  239. class Lower(Func):
  240. function = 'LOWER'
  241. qs = Company.objects.annotate(
  242. tagline=Func(
  243. F('motto'),
  244. F('ticker_name'),
  245. F('description'),
  246. Value('No Tag'),
  247. function='COALESCE')
  248. ).annotate(
  249. tagline_lower=Lower(F('tagline'), output_field=CharField())
  250. ).order_by('name')
  251. # LOWER function supported by:
  252. # oracle, postgres, mysql, sqlite, sqlserver
  253. self.assertQuerysetEqual(
  254. qs, [
  255. ('Apple', 'APPL'.lower()),
  256. ('Django Software Foundation', 'No Tag'.lower()),
  257. ('Google', 'Do No Evil'.lower()),
  258. ('Yahoo', 'Internet Company'.lower())
  259. ],
  260. lambda c: (c.name, c.tagline_lower)
  261. )