Ticket #29416: test_regression_29416.py

File test_regression_29416.py, 1.2 KB (added by Antoine Pinsard, 2 years ago)

tests/annotations/test_regression_29416.py

Line 
1# -*- coding: utf-8 -*-
2from django.db.models import Count
3from django.db.models.expressions import RawSQL
4from django.test import TestCase
5
6from .models import Author
7
8
9class 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        ))
Back to Top