summaryrefslogtreecommitdiff
path: root/tests/annotations/tests.py
diff options
context:
space:
mode:
authorDevin Cox <dcox@surefyre.co>2024-08-09 13:56:56 -0700
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-08-12 15:35:19 +0200
commite03083917db03757e48f8edac4c8491b72c8a3c4 (patch)
tree9b96fb24ab403cc56d5510e74d23a70eeab207fa /tests/annotations/tests.py
parent228128618bd895ecad235d2215f4ad4e3232595d (diff)
Fixed #35586 -- Added support for set-returning database functions.
Aggregation optimization didn't account for not referenced set-returning annotations on Postgres. Co-authored-by: Simon Charette <charette.s@gmail.com>
Diffstat (limited to 'tests/annotations/tests.py')
-rw-r--r--tests/annotations/tests.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py
index 703847e1dd..29660a827e 100644
--- a/tests/annotations/tests.py
+++ b/tests/annotations/tests.py
@@ -1,7 +1,9 @@
import datetime
from decimal import Decimal
+from unittest import skipUnless
from django.core.exceptions import FieldDoesNotExist, FieldError
+from django.db import connection
from django.db.models import (
BooleanField,
Case,
@@ -15,6 +17,7 @@ from django.db.models import (
FloatField,
Func,
IntegerField,
+ JSONField,
Max,
OuterRef,
Q,
@@ -43,6 +46,7 @@ from .models import (
Company,
DepartmentStore,
Employee,
+ JsonModel,
Publisher,
Store,
Ticket,
@@ -1167,6 +1171,23 @@ class NonAggregateAnnotationTestCase(TestCase):
with self.assertRaisesMessage(ValueError, msg):
Book.objects.annotate(**{crafted_alias: Value(1)})
+ @skipUnless(connection.vendor == "postgresql", "PostgreSQL tests")
+ @skipUnlessDBFeature("supports_json_field")
+ def test_set_returning_functions(self):
+ class JSONBPathQuery(Func):
+ function = "jsonb_path_query"
+ output_field = JSONField()
+ set_returning = True
+
+ test_model = JsonModel.objects.create(
+ data={"key": [{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}]}, id=1
+ )
+ qs = JsonModel.objects.annotate(
+ table_element=JSONBPathQuery("data", Value("$.key[*]"))
+ ).filter(pk=test_model.pk)
+
+ self.assertEqual(qs.count(), len(qs))
+
class AliasTests(TestCase):
@classmethod