1 | # -*- coding: utf-8 -*- |
---|
2 | from django.db.models import Count |
---|
3 | from django.db.models.expressions import RawSQL |
---|
4 | from django.test import TestCase |
---|
5 | |
---|
6 | from .models import Author |
---|
7 | |
---|
8 | |
---|
9 | class Bisect29416TestCase(TestCase): |
---|
10 | |
---|
11 | def test_bisect_29416(self): |
---|
12 | nb_books = RawSQL(( |
---|
13 | "SELECT COUNT(*) FROM annotations_book " |
---|
14 | "WHERE annotations_book.author_id=annotations_author.id" |
---|
15 | ), []) |
---|
16 | query = str( |
---|
17 | Author.objects.all().values('id') |
---|
18 | .annotate( |
---|
19 | nb_books=nb_books, |
---|
20 | nb_friends=Count('friends'), |
---|
21 | ) |
---|
22 | .order_by() |
---|
23 | .query |
---|
24 | ) |
---|
25 | self.assertEqual(query, ( |
---|
26 | "SELECT `annotations_author`.`id`, (" |
---|
27 | "SELECT COUNT(*) FROM annotations_book " |
---|
28 | "WHERE annotations_book.author_id=annotations_author.id" |
---|
29 | ") AS `nb_books`, " |
---|
30 | "COUNT(`annotations_author_friends`.`to_author_id`) AS `nb_friends` " |
---|
31 | "FROM `annotations_author` " |
---|
32 | "LEFT OUTER JOIN `annotations_author_friends` ON (" |
---|
33 | "`annotations_author`.`id` = `annotations_author_friends`.`from_author_id`) " |
---|
34 | "GROUP BY `annotations_author`.`id` ORDER BY NULL" |
---|
35 | )) |
---|