Skip to content

Commit c2820e4

Browse files
committed
Add an annotation test
1 parent a9b93ca commit c2820e4

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

tests/annotations_/__init__.py

Whitespace-only changes.

tests/annotations_/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.db import models
2+
3+
4+
class Book(models.Model):
5+
isbn = models.CharField(max_length=9)
6+
name = models.CharField(max_length=255)

tests/annotations_/tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django.db.models import BooleanField, ExpressionWrapper, IntegerField, Q, Sum
2+
from django.db.models.functions import Cast
3+
from django.test import TestCase
4+
5+
from .models import Book
6+
7+
8+
class AnnotationTestCase(TestCase):
9+
def test_aggregate_over_empty_expression_annotation(self):
10+
Book.objects.create(isbn="000", name="book 1")
11+
Book.objects.create(isbn="001", name="book 2")
12+
qs = Book.objects.annotate(
13+
selected=ExpressionWrapper(Q(pk__in=[]), output_field=BooleanField()),
14+
).aggregate(selected__sum=Sum(Cast("selected", IntegerField())))
15+
self.assertEqual(qs["selected__sum"], 0)

0 commit comments

Comments
 (0)