tests.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. import datetime
  2. from decimal import Decimal
  3. from django.core.exceptions import FieldDoesNotExist, FieldError
  4. from django.db.models import (
  5. BooleanField, CharField, Count, DateTimeField, ExpressionWrapper, F, Func,
  6. IntegerField, NullBooleanField, Q, Sum, Value,
  7. )
  8. from django.db.models.functions import Length, Lower
  9. from django.test import TestCase, skipUnlessDBFeature
  10. from .models import (
  11. Author, Book, Company, DepartmentStore, Employee, Publisher, Store, Ticket,
  12. )
  13. def cxOracle_py3_bug(func):
  14. """
  15. There's a bug in Django/cx_Oracle with respect to string handling under
  16. Python 3 (essentially, they treat Python 3 strings as Python 2 strings
  17. rather than unicode). This makes some tests here fail under Python 3, so
  18. we mark them as expected failures until someone fixes them in #23843.
  19. """
  20. from unittest import expectedFailure
  21. from django.db import connection
  22. return expectedFailure(func) if connection.vendor == 'oracle' else func
  23. class NonAggregateAnnotationTestCase(TestCase):
  24. @classmethod
  25. def setUpTestData(cls):
  26. cls.a1 = Author.objects.create(name='Adrian Holovaty', age=34)
  27. cls.a2 = Author.objects.create(name='Jacob Kaplan-Moss', age=35)
  28. cls.a3 = Author.objects.create(name='Brad Dayley', age=45)
  29. cls.a4 = Author.objects.create(name='James Bennett', age=29)
  30. cls.a5 = Author.objects.create(name='Jeffrey Forcier', age=37)
  31. cls.a6 = Author.objects.create(name='Paul Bissex', age=29)
  32. cls.a7 = Author.objects.create(name='Wesley J. Chun', age=25)
  33. cls.a8 = Author.objects.create(name='Peter Norvig', age=57)
  34. cls.a9 = Author.objects.create(name='Stuart Russell', age=46)
  35. cls.a1.friends.add(cls.a2, cls.a4)
  36. cls.a2.friends.add(cls.a1, cls.a7)
  37. cls.a4.friends.add(cls.a1)
  38. cls.a5.friends.add(cls.a6, cls.a7)
  39. cls.a6.friends.add(cls.a5, cls.a7)
  40. cls.a7.friends.add(cls.a2, cls.a5, cls.a6)
  41. cls.a8.friends.add(cls.a9)
  42. cls.a9.friends.add(cls.a8)
  43. cls.p1 = Publisher.objects.create(name='Apress', num_awards=3)
  44. cls.p2 = Publisher.objects.create(name='Sams', num_awards=1)
  45. cls.p3 = Publisher.objects.create(name='Prentice Hall', num_awards=7)
  46. cls.p4 = Publisher.objects.create(name='Morgan Kaufmann', num_awards=9)
  47. cls.p5 = Publisher.objects.create(name="Jonno's House of Books", num_awards=0)
  48. cls.b1 = Book.objects.create(
  49. isbn='159059725', name='The Definitive Guide to Django: Web Development Done Right',
  50. pages=447, rating=4.5, price=Decimal('30.00'), contact=cls.a1, publisher=cls.p1,
  51. pubdate=datetime.date(2007, 12, 6)
  52. )
  53. cls.b2 = Book.objects.create(
  54. isbn='067232959', name='Sams Teach Yourself Django in 24 Hours',
  55. pages=528, rating=3.0, price=Decimal('23.09'), contact=cls.a3, publisher=cls.p2,
  56. pubdate=datetime.date(2008, 3, 3)
  57. )
  58. cls.b3 = Book.objects.create(
  59. isbn='159059996', name='Practical Django Projects',
  60. pages=300, rating=4.0, price=Decimal('29.69'), contact=cls.a4, publisher=cls.p1,
  61. pubdate=datetime.date(2008, 6, 23)
  62. )
  63. cls.b4 = Book.objects.create(
  64. isbn='013235613', name='Python Web Development with Django',
  65. pages=350, rating=4.0, price=Decimal('29.69'), contact=cls.a5, publisher=cls.p3,
  66. pubdate=datetime.date(2008, 11, 3)
  67. )
  68. cls.b5 = Book.objects.create(
  69. isbn='013790395', name='Artificial Intelligence: A Modern Approach',
  70. pages=1132, rating=4.0, price=Decimal('82.80'), contact=cls.a8, publisher=cls.p3,
  71. pubdate=datetime.date(1995, 1, 15)
  72. )
  73. cls.b6 = Book.objects.create(
  74. isbn='155860191', name='Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp',
  75. pages=946, rating=5.0, price=Decimal('75.00'), contact=cls.a8, publisher=cls.p4,
  76. pubdate=datetime.date(1991, 10, 15)
  77. )
  78. cls.b1.authors.add(cls.a1, cls.a2)
  79. cls.b2.authors.add(cls.a3)
  80. cls.b3.authors.add(cls.a4)
  81. cls.b4.authors.add(cls.a5, cls.a6, cls.a7)
  82. cls.b5.authors.add(cls.a8, cls.a9)
  83. cls.b6.authors.add(cls.a8)
  84. s1 = Store.objects.create(
  85. name='Amazon.com',
  86. original_opening=datetime.datetime(1994, 4, 23, 9, 17, 42),
  87. friday_night_closing=datetime.time(23, 59, 59)
  88. )
  89. s2 = Store.objects.create(
  90. name='Books.com',
  91. original_opening=datetime.datetime(2001, 3, 15, 11, 23, 37),
  92. friday_night_closing=datetime.time(23, 59, 59)
  93. )
  94. s3 = Store.objects.create(
  95. name="Mamma and Pappa's Books",
  96. original_opening=datetime.datetime(1945, 4, 25, 16, 24, 14),
  97. friday_night_closing=datetime.time(21, 30)
  98. )
  99. s1.books.add(cls.b1, cls.b2, cls.b3, cls.b4, cls.b5, cls.b6)
  100. s2.books.add(cls.b1, cls.b3, cls.b5, cls.b6)
  101. s3.books.add(cls.b3, cls.b4, cls.b6)
  102. def test_basic_annotation(self):
  103. books = Book.objects.annotate(
  104. is_book=Value(1, output_field=IntegerField()))
  105. for book in books:
  106. self.assertEqual(book.is_book, 1)
  107. def test_basic_f_annotation(self):
  108. books = Book.objects.annotate(another_rating=F('rating'))
  109. for book in books:
  110. self.assertEqual(book.another_rating, book.rating)
  111. def test_joined_annotation(self):
  112. books = Book.objects.select_related('publisher').annotate(
  113. num_awards=F('publisher__num_awards'))
  114. for book in books:
  115. self.assertEqual(book.num_awards, book.publisher.num_awards)
  116. def test_mixed_type_annotation_date_interval(self):
  117. active = datetime.datetime(2015, 3, 20, 14, 0, 0)
  118. duration = datetime.timedelta(hours=1)
  119. expires = datetime.datetime(2015, 3, 20, 14, 0, 0) + duration
  120. Ticket.objects.create(active_at=active, duration=duration)
  121. t = Ticket.objects.annotate(
  122. expires=ExpressionWrapper(F('active_at') + F('duration'), output_field=DateTimeField())
  123. ).first()
  124. self.assertEqual(t.expires, expires)
  125. def test_mixed_type_annotation_numbers(self):
  126. test = self.b1
  127. b = Book.objects.annotate(
  128. combined=ExpressionWrapper(F('pages') + F('rating'), output_field=IntegerField())
  129. ).get(isbn=test.isbn)
  130. combined = int(test.pages + test.rating)
  131. self.assertEqual(b.combined, combined)
  132. def test_empty_expression_annotation(self):
  133. books = Book.objects.annotate(
  134. selected=ExpressionWrapper(Q(pk__in=[]), output_field=BooleanField())
  135. )
  136. self.assertEqual(len(books), Book.objects.count())
  137. self.assertTrue(all(not book.selected for book in books))
  138. books = Book.objects.annotate(
  139. selected=ExpressionWrapper(Q(pk__in=Book.objects.none()), output_field=BooleanField())
  140. )
  141. self.assertEqual(len(books), Book.objects.count())
  142. self.assertTrue(all(not book.selected for book in books))
  143. def test_annotate_with_aggregation(self):
  144. books = Book.objects.annotate(
  145. is_book=Value(1, output_field=IntegerField()),
  146. rating_count=Count('rating'))
  147. for book in books:
  148. self.assertEqual(book.is_book, 1)
  149. self.assertEqual(book.rating_count, 1)
  150. def test_aggregate_over_annotation(self):
  151. agg = Author.objects.annotate(other_age=F('age')).aggregate(otherage_sum=Sum('other_age'))
  152. other_agg = Author.objects.aggregate(age_sum=Sum('age'))
  153. self.assertEqual(agg['otherage_sum'], other_agg['age_sum'])
  154. @skipUnlessDBFeature('can_distinct_on_fields')
  155. def test_distinct_on_with_annotation(self):
  156. store = Store.objects.create(
  157. name='test store',
  158. original_opening=datetime.datetime.now(),
  159. friday_night_closing=datetime.time(21, 00, 00),
  160. )
  161. names = [
  162. 'Theodore Roosevelt',
  163. 'Eleanor Roosevelt',
  164. 'Franklin Roosevelt',
  165. 'Ned Stark',
  166. 'Catelyn Stark',
  167. ]
  168. for name in names:
  169. Employee.objects.create(
  170. store=store,
  171. first_name=name.split()[0],
  172. last_name=name.split()[1],
  173. age=30, salary=2000,
  174. )
  175. people = Employee.objects.annotate(
  176. name_lower=Lower('last_name'),
  177. ).distinct('name_lower')
  178. self.assertEqual({p.last_name for p in people}, {'Stark', 'Roosevelt'})
  179. self.assertEqual(len(people), 2)
  180. people2 = Employee.objects.annotate(
  181. test_alias=F('store__name'),
  182. ).distinct('test_alias')
  183. self.assertEqual(len(people2), 1)
  184. lengths = Employee.objects.annotate(
  185. name_len=Length('first_name'),
  186. ).distinct('name_len').values_list('name_len', flat=True)
  187. self.assertSequenceEqual(lengths, [3, 7, 8])
  188. def test_filter_annotation(self):
  189. books = Book.objects.annotate(
  190. is_book=Value(1, output_field=IntegerField())
  191. ).filter(is_book=1)
  192. for book in books:
  193. self.assertEqual(book.is_book, 1)
  194. def test_filter_annotation_with_f(self):
  195. books = Book.objects.annotate(
  196. other_rating=F('rating')
  197. ).filter(other_rating=3.5)
  198. for book in books:
  199. self.assertEqual(book.other_rating, 3.5)
  200. def test_filter_annotation_with_double_f(self):
  201. books = Book.objects.annotate(
  202. other_rating=F('rating')
  203. ).filter(other_rating=F('rating'))
  204. for book in books:
  205. self.assertEqual(book.other_rating, book.rating)
  206. def test_filter_agg_with_double_f(self):
  207. books = Book.objects.annotate(
  208. sum_rating=Sum('rating')
  209. ).filter(sum_rating=F('sum_rating'))
  210. for book in books:
  211. self.assertEqual(book.sum_rating, book.rating)
  212. def test_filter_wrong_annotation(self):
  213. with self.assertRaisesMessage(FieldError, "Cannot resolve keyword 'nope' into field."):
  214. list(Book.objects.annotate(
  215. sum_rating=Sum('rating')
  216. ).filter(sum_rating=F('nope')))
  217. def test_combined_annotation_commutative(self):
  218. book1 = Book.objects.annotate(adjusted_rating=F('rating') + 2).get(pk=self.b1.pk)
  219. book2 = Book.objects.annotate(adjusted_rating=2 + F('rating')).get(pk=self.b1.pk)
  220. self.assertEqual(book1.adjusted_rating, book2.adjusted_rating)
  221. book1 = Book.objects.annotate(adjusted_rating=F('rating') + None).get(pk=self.b1.pk)
  222. book2 = Book.objects.annotate(adjusted_rating=None + F('rating')).get(pk=self.b1.pk)
  223. self.assertEqual(book1.adjusted_rating, book2.adjusted_rating)
  224. def test_update_with_annotation(self):
  225. book_preupdate = Book.objects.get(pk=self.b2.pk)
  226. Book.objects.annotate(other_rating=F('rating') - 1).update(rating=F('other_rating'))
  227. book_postupdate = Book.objects.get(pk=self.b2.pk)
  228. self.assertEqual(book_preupdate.rating - 1, book_postupdate.rating)
  229. def test_annotation_with_m2m(self):
  230. books = Book.objects.annotate(author_age=F('authors__age')).filter(pk=self.b1.pk).order_by('author_age')
  231. self.assertEqual(books[0].author_age, 34)
  232. self.assertEqual(books[1].author_age, 35)
  233. def test_annotation_reverse_m2m(self):
  234. books = Book.objects.annotate(
  235. store_name=F('store__name')).filter(
  236. name='Practical Django Projects').order_by(
  237. 'store_name')
  238. self.assertQuerysetEqual(
  239. books, [
  240. 'Amazon.com',
  241. 'Books.com',
  242. 'Mamma and Pappa\'s Books'
  243. ],
  244. lambda b: b.store_name
  245. )
  246. def test_values_annotation(self):
  247. """
  248. Annotations can reference fields in a values clause,
  249. and contribute to an existing values clause.
  250. """
  251. # annotate references a field in values()
  252. qs = Book.objects.values('rating').annotate(other_rating=F('rating') - 1)
  253. book = qs.get(pk=self.b1.pk)
  254. self.assertEqual(book['rating'] - 1, book['other_rating'])
  255. # filter refs the annotated value
  256. book = qs.get(other_rating=4)
  257. self.assertEqual(book['other_rating'], 4)
  258. # can annotate an existing values with a new field
  259. book = qs.annotate(other_isbn=F('isbn')).get(other_rating=4)
  260. self.assertEqual(book['other_rating'], 4)
  261. self.assertEqual(book['other_isbn'], '155860191')
  262. def test_values_with_pk_annotation(self):
  263. # annotate references a field in values() with pk
  264. publishers = Publisher.objects.values('id', 'book__rating').annotate(total=Sum('book__rating'))
  265. for publisher in publishers.filter(pk=self.p1.pk):
  266. self.assertEqual(publisher['book__rating'], publisher['total'])
  267. def test_defer_annotation(self):
  268. """
  269. Deferred attributes can be referenced by an annotation,
  270. but they are not themselves deferred, and cannot be deferred.
  271. """
  272. qs = Book.objects.defer('rating').annotate(other_rating=F('rating') - 1)
  273. with self.assertNumQueries(2):
  274. book = qs.get(other_rating=4)
  275. self.assertEqual(book.rating, 5)
  276. self.assertEqual(book.other_rating, 4)
  277. with self.assertRaisesMessage(FieldDoesNotExist, "Book has no field named 'other_rating'"):
  278. book = qs.defer('other_rating').get(other_rating=4)
  279. def test_mti_annotations(self):
  280. """
  281. Fields on an inherited model can be referenced by an
  282. annotated field.
  283. """
  284. d = DepartmentStore.objects.create(
  285. name='Angus & Robinson',
  286. original_opening=datetime.date(2014, 3, 8),
  287. friday_night_closing=datetime.time(21, 00, 00),
  288. chain='Westfield'
  289. )
  290. books = Book.objects.filter(rating__gt=4)
  291. for b in books:
  292. d.books.add(b)
  293. qs = DepartmentStore.objects.annotate(
  294. other_name=F('name'),
  295. other_chain=F('chain'),
  296. is_open=Value(True, BooleanField()),
  297. book_isbn=F('books__isbn')
  298. ).order_by('book_isbn').filter(chain='Westfield')
  299. self.assertQuerysetEqual(
  300. qs, [
  301. ('Angus & Robinson', 'Westfield', True, '155860191'),
  302. ('Angus & Robinson', 'Westfield', True, '159059725')
  303. ],
  304. lambda d: (d.other_name, d.other_chain, d.is_open, d.book_isbn)
  305. )
  306. def test_null_annotation(self):
  307. """
  308. Annotating None onto a model round-trips
  309. """
  310. book = Book.objects.annotate(no_value=Value(None, output_field=IntegerField())).first()
  311. self.assertIsNone(book.no_value)
  312. def test_order_by_annotation(self):
  313. authors = Author.objects.annotate(other_age=F('age')).order_by('other_age')
  314. self.assertQuerysetEqual(
  315. authors, [
  316. 25, 29, 29, 34, 35, 37, 45, 46, 57,
  317. ],
  318. lambda a: a.other_age
  319. )
  320. def test_order_by_aggregate(self):
  321. authors = Author.objects.values('age').annotate(age_count=Count('age')).order_by('age_count', 'age')
  322. self.assertQuerysetEqual(
  323. authors, [
  324. (25, 1), (34, 1), (35, 1), (37, 1), (45, 1), (46, 1), (57, 1), (29, 2),
  325. ],
  326. lambda a: (a['age'], a['age_count'])
  327. )
  328. def test_annotate_exists(self):
  329. authors = Author.objects.annotate(c=Count('id')).filter(c__gt=1)
  330. self.assertFalse(authors.exists())
  331. def test_column_field_ordering(self):
  332. """
  333. Columns are aligned in the correct order for resolve_columns. This test
  334. will fail on MySQL if column ordering is out. Column fields should be
  335. aligned as:
  336. 1. extra_select
  337. 2. model_fields
  338. 3. annotation_fields
  339. 4. model_related_fields
  340. """
  341. store = Store.objects.first()
  342. Employee.objects.create(id=1, first_name='Max', manager=True, last_name='Paine',
  343. store=store, age=23, salary=Decimal(50000.00))
  344. Employee.objects.create(id=2, first_name='Buffy', manager=False, last_name='Summers',
  345. store=store, age=18, salary=Decimal(40000.00))
  346. qs = Employee.objects.extra(
  347. select={'random_value': '42'}
  348. ).select_related('store').annotate(
  349. annotated_value=Value(17, output_field=IntegerField())
  350. )
  351. rows = [
  352. (1, 'Max', True, 42, 'Paine', 23, Decimal(50000.00), store.name, 17),
  353. (2, 'Buffy', False, 42, 'Summers', 18, Decimal(40000.00), store.name, 17)
  354. ]
  355. self.assertQuerysetEqual(
  356. qs.order_by('id'), rows,
  357. lambda e: (
  358. e.id, e.first_name, e.manager, e.random_value, e.last_name, e.age,
  359. e.salary, e.store.name, e.annotated_value))
  360. def test_column_field_ordering_with_deferred(self):
  361. store = Store.objects.first()
  362. Employee.objects.create(id=1, first_name='Max', manager=True, last_name='Paine',
  363. store=store, age=23, salary=Decimal(50000.00))
  364. Employee.objects.create(id=2, first_name='Buffy', manager=False, last_name='Summers',
  365. store=store, age=18, salary=Decimal(40000.00))
  366. qs = Employee.objects.extra(
  367. select={'random_value': '42'}
  368. ).select_related('store').annotate(
  369. annotated_value=Value(17, output_field=IntegerField())
  370. )
  371. rows = [
  372. (1, 'Max', True, 42, 'Paine', 23, Decimal(50000.00), store.name, 17),
  373. (2, 'Buffy', False, 42, 'Summers', 18, Decimal(40000.00), store.name, 17)
  374. ]
  375. # and we respect deferred columns!
  376. self.assertQuerysetEqual(
  377. qs.defer('age').order_by('id'), rows,
  378. lambda e: (
  379. e.id, e.first_name, e.manager, e.random_value, e.last_name, e.age,
  380. e.salary, e.store.name, e.annotated_value))
  381. @cxOracle_py3_bug
  382. def test_custom_functions(self):
  383. Company(name='Apple', motto=None, ticker_name='APPL', description='Beautiful Devices').save()
  384. Company(name='Django Software Foundation', motto=None, ticker_name=None, description=None).save()
  385. Company(name='Google', motto='Do No Evil', ticker_name='GOOG', description='Internet Company').save()
  386. Company(name='Yahoo', motto=None, ticker_name=None, description='Internet Company').save()
  387. qs = Company.objects.annotate(
  388. tagline=Func(
  389. F('motto'),
  390. F('ticker_name'),
  391. F('description'),
  392. Value('No Tag'),
  393. function='COALESCE'
  394. )
  395. ).order_by('name')
  396. self.assertQuerysetEqual(
  397. qs, [
  398. ('Apple', 'APPL'),
  399. ('Django Software Foundation', 'No Tag'),
  400. ('Google', 'Do No Evil'),
  401. ('Yahoo', 'Internet Company')
  402. ],
  403. lambda c: (c.name, c.tagline)
  404. )
  405. @cxOracle_py3_bug
  406. def test_custom_functions_can_ref_other_functions(self):
  407. Company(name='Apple', motto=None, ticker_name='APPL', description='Beautiful Devices').save()
  408. Company(name='Django Software Foundation', motto=None, ticker_name=None, description=None).save()
  409. Company(name='Google', motto='Do No Evil', ticker_name='GOOG', description='Internet Company').save()
  410. Company(name='Yahoo', motto=None, ticker_name=None, description='Internet Company').save()
  411. class Lower(Func):
  412. function = 'LOWER'
  413. qs = Company.objects.annotate(
  414. tagline=Func(
  415. F('motto'),
  416. F('ticker_name'),
  417. F('description'),
  418. Value('No Tag'),
  419. function='COALESCE')
  420. ).annotate(
  421. tagline_lower=Lower(F('tagline'), output_field=CharField())
  422. ).order_by('name')
  423. # LOWER function supported by:
  424. # oracle, postgres, mysql, sqlite, sqlserver
  425. self.assertQuerysetEqual(
  426. qs, [
  427. ('Apple', 'APPL'.lower()),
  428. ('Django Software Foundation', 'No Tag'.lower()),
  429. ('Google', 'Do No Evil'.lower()),
  430. ('Yahoo', 'Internet Company'.lower())
  431. ],
  432. lambda c: (c.name, c.tagline_lower)
  433. )
  434. def test_boolean_value_annotation(self):
  435. books = Book.objects.annotate(
  436. is_book=Value(True, output_field=BooleanField()),
  437. is_pony=Value(False, output_field=BooleanField()),
  438. is_none=Value(None, output_field=NullBooleanField()),
  439. )
  440. self.assertGreater(len(books), 0)
  441. for book in books:
  442. self.assertIs(book.is_book, True)
  443. self.assertIs(book.is_pony, False)
  444. self.assertIsNone(book.is_none)