|
@@ -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
|