Ticket #31094: test-31094.diff

File test-31094.diff, 1.3 KB (added by Mariusz Felisiak, 4 years ago)

Regression test.

  • tests/aggregation/tests.py

    diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py
    index a93c39e3d5..0aa0ebe4e9 100644
    a b class AggregateTestCase(TestCase):  
    11701170            Exists(long_books_qs),
    11711171        ).annotate(total=Count('*'))
    11721172        self.assertEqual(dict(has_long_books_breakdown), {True: 2, False: 3})
     1173
     1174    @skipUnlessDBFeature('supports_subqueries_in_group_by')
     1175    def test_aggregation_subquery_annotation_related_field(self):
     1176        publisher = Publisher.objects.create(name=self.a9.name, num_awards=2)
     1177        book = Book.objects.create(
     1178            isbn='159059999', name='Test book.', pages=819, rating=2.5,
     1179            price=Decimal('14.44'), contact=self.a9, publisher=publisher,
     1180            pubdate=datetime.date(2019, 12, 6),
     1181        )
     1182        book.authors.add(self.a5, self.a6, self.a7)
     1183        books_qs = Book.objects.annotate(
     1184            contact_publisher=Subquery(
     1185                Publisher.objects.filter(
     1186                    pk=OuterRef('publisher'),
     1187                    name=OuterRef('contact__name'),
     1188                ).values('name')[:1],
     1189            )
     1190        ).filter(
     1191            contact_publisher__isnull=False,
     1192        ).annotate(count=Count('authors'))
     1193        self.assertSequenceEqual(books_qs, [book])
Back to Top