| 1 |
import unittest |
| 2 |
|
| 3 |
from django.db import NotSupportedError, connection |
| 4 |
from django.db.models import CharField, F, Value |
| 5 |
from django.db.models.functions import Cast, JSONArray, JSONObject, Lower |
| 6 |
from django.test import TestCase |
| 7 |
from django.test.testcases import skipIfDBFeature, skipUnlessDBFeature |
| 8 |
from django.utils import timezone |
| 9 |
|
| 10 |
from ..models import Article, Author |
| 11 |
|
| 12 |
|
| 13 |
@skipUnlessDBFeature("supports_json_field") |
| 14 |
class JSONArrayTests(TestCase): |
| 15 |
@classmethod |
| 16 |
def setUpTestData(cls): |
| 17 |
Author.objects.create(name="Ivan Ivanov", alias="iivanov") |
| 18 |
|
| 19 |
def test_empty(self): |
| 20 |
obj = Author.objects.annotate(json_array=JSONArray()).first() |
| 21 |
self.assertEqual(obj.json_array, []) |
| 22 |
|
| 23 |
def test_basic(self): |
| 24 |
obj = Author.objects.annotate( |
| 25 |
json_array=JSONArray(Value("name"), F("name")) |
| 26 |
).first() |
| 27 |
self.assertEqual(obj.json_array, ["name", "Ivan Ivanov"]) |
| 28 |
|
| 29 |
def test_expressions(self): |
| 30 |
obj = Author.objects.annotate( |
| 31 |
json_array=JSONArray( |
| 32 |
Lower("name"), |
| 33 |
F("alias"), |
| 34 |
F("goes_by"), |
| 35 |
Value(30000.15), |
| 36 |
F("age") * 2, |
| 37 |
) |
| 38 |
).first() |
| 39 |
self.assertEqual( |
| 40 |
obj.json_array, |
| 41 |
[ |
| 42 |
"ivan ivanov", |
| 43 |
"iivanov", |
| 44 |
None, |
| 45 |
30000.15, |
| 46 |
60, |
| 47 |
], |
| 48 |
) |
| 49 |
|
| 50 |
def test_nested_json_array(self): |
| 51 |
obj = Author.objects.annotate( |
| 52 |
json_array=JSONArray( |
| 53 |
F("name"), |
| 54 |
JSONArray(F("alias"), F("age")), |
| 55 |
) |
| 56 |
).first() |
| 57 |
self.assertEqual( |
| 58 |
obj.json_array, |
| 59 |
[ |
| 60 |
"Ivan Ivanov", |
| 61 |
["iivanov", 30], |
| 62 |
], |
| 63 |
) |
| 64 |
|
| 65 |
def test_nested_empty_json_array(self): |
| 66 |
obj = Author.objects.annotate( |
| 67 |
json_array=JSONArray( |
| 68 |
F("name"), |
| 69 |
JSONArray(), |
| 70 |
) |
| 71 |
).first() |
| 72 |
self.assertEqual( |
| 73 |
obj.json_array, |
| 74 |
[ |
| 75 |
"Ivan Ivanov", |
| 76 |
[], |
| 77 |
], |
| 78 |
) |
| 79 |
|
| 80 |
def test_textfield(self): |
| 81 |
Article.objects.create( |
| 82 |
title="The Title", |
| 83 |
text="x" * 4000, |
| 84 |
written=timezone.now(), |
| 85 |
) |
| 86 |
obj = Article.objects.annotate(json_array=JSONArray(F("text"))).first() |
| 87 |
self.assertEqual(obj.json_array, ["x" * 4000]) |
| 88 |
|
| 89 |
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests") |
| 90 |
def test_explicit_cast(self): |
| 91 |
qs = Author.objects.annotate( |
| 92 |
json_array=JSONArray(Cast("age", CharField())) |
| 93 |
).values("json_array") |
| 94 |
with self.assertNumQueries(1) as ctx: |
| 95 |
self.assertSequenceEqual(qs, [{"json_array": ["30"]}]) |
| 96 |
sql = ctx.captured_queries[0]["sql"] |
| 97 |
self.assertIn("::varchar", sql) |
| 98 |
self.assertNotIn("::varchar)::varchar", sql) |
| 99 |
|
| 100 |
def test_order_by_key(self): |
| 101 |
qs = Author.objects.annotate(arr=JSONArray(F("alias"))).order_by("arr__0") |
| 102 |
self.assertQuerySetEqual(qs, Author.objects.order_by("alias")) |
| 103 |
|
| 104 |
def test_order_by_nested_key(self): |
| 105 |
qs = Author.objects.annotate(arr=JSONArray(JSONArray(F("alias")))).order_by( |
| 106 |
"-arr__0__0" |
| 107 |
) |
| 108 |
self.assertQuerySetEqual(qs, Author.objects.order_by("-alias")) |
| 109 |
|
| 110 |
|
| 111 |
@skipIfDBFeature("supports_json_field") |
| 112 |
class JSONArrayNotSupportedTests(TestCase): |
| 113 |
def test_not_supported(self): |
| 114 |
msg = "JSONFields are not supported on this database backend." |
| 115 |
with self.assertRaisesMessage(NotSupportedError, msg): |
| 116 |
Author.objects.annotate(json_array=JSONArray()).first() |
| 117 |
|
| 118 |
|
| 119 |
@skipUnlessDBFeature("has_json_object_function", "supports_json_field") |
| 120 |
class JSONArrayObjectTests(TestCase): |
| 121 |
@classmethod |
| 122 |
def setUpTestData(cls): |
| 123 |
Author.objects.create(name="Ivan Ivanov", alias="iivanov") |
| 124 |
|
| 125 |
def test_nested_json_array_object(self): |
| 126 |
obj = Author.objects.annotate( |
| 127 |
json_array=JSONArray( |
| 128 |
JSONObject( |
| 129 |
name1="name", |
| 130 |
nested_json_object1=JSONObject(alias1="alias", age1="age"), |
| 131 |
), |
| 132 |
JSONObject( |
| 133 |
name2="name", |
| 134 |
nested_json_object2=JSONObject(alias2="alias", age2="age"), |
| 135 |
), |
| 136 |
) |
| 137 |
).first() |
| 138 |
self.assertEqual( |
| 139 |
obj.json_array, |
| 140 |
[ |
| 141 |
{ |
| 142 |
"name1": "Ivan Ivanov", |
| 143 |
"nested_json_object1": {"alias1": "iivanov", "age1": 30}, |
| 144 |
}, |
| 145 |
{ |
| 146 |
"name2": "Ivan Ivanov", |
| 147 |
"nested_json_object2": {"alias2": "iivanov", "age2": 30}, |
| 148 |
}, |
| 149 |
], |
| 150 |
) |
| 151 |
|
| 152 |
def test_nested_json_object_array(self): |
| 153 |
obj = Author.objects.annotate( |
| 154 |
json_object=JSONObject( |
| 155 |
name="name", |
| 156 |
nested_json_array=JSONArray( |
| 157 |
JSONObject(alias1="alias", age1="age"), |
| 158 |
JSONObject(alias2="alias", age2="age"), |
| 159 |
), |
| 160 |
) |
| 161 |
).first() |
| 162 |
self.assertEqual( |
| 163 |
obj.json_object, |
| 164 |
{ |
| 165 |
"name": "Ivan Ivanov", |
| 166 |
"nested_json_array": [ |
| 167 |
{"alias1": "iivanov", "age1": 30}, |
| 168 |
{"alias2": "iivanov", "age2": 30}, |
| 169 |
], |
| 170 |
}, |
| 171 |
) |
| 172 |
|
| 173 |
def test_order_by_nested_key(self): |
| 174 |
qs = Author.objects.annotate( |
| 175 |
arr=JSONArray(JSONObject(alias=F("alias"))) |
| 176 |
).order_by("-arr__0__alias") |
| 177 |
self.assertQuerySetEqual(qs, Author.objects.order_by("-alias")) |