Debian Codegraph @

tests/db_functions/json/test_json_object.py

raw
1 from django.db import NotSupportedError
2 from django.db.models import F, Value
3 from django.db.models.functions import JSONObject, Lower
4 from django.test import TestCase
5 from django.test.testcases import skipIfDBFeature, skipUnlessDBFeature
6 from django.utils import timezone
7
8 from ..models import Article, Author
9
10
11 @skipUnlessDBFeature("has_json_object_function")
12 class JSONObjectTests(TestCase):
13 @classmethod
14 def setUpTestData(cls):
15 Author.objects.bulk_create(
16 [
17 Author(name="Ivan Ivanov", alias="iivanov"),
18 Author(name="Bertha Berthy", alias="bberthy"),
19 ]
20 )
21
22 def test_empty(self):
23 obj = Author.objects.annotate(json_object=JSONObject()).first()
24 self.assertEqual(obj.json_object, {})
25
26 def test_basic(self):
27 obj = Author.objects.annotate(json_object=JSONObject(name="name")).first()
28 self.assertEqual(obj.json_object, {"name": "Ivan Ivanov"})
29
30 def test_expressions(self):
31 obj = Author.objects.annotate(
32 json_object=JSONObject(
33 name=Lower("name"),
34 alias="alias",
35 goes_by="goes_by",
36 salary=Value(30000.15),
37 age=F("age") * 2,
38 )
39 ).first()
40 self.assertEqual(
41 obj.json_object,
42 {
43 "name": "ivan ivanov",
44 "alias": "iivanov",
45 "goes_by": None,
46 "salary": 30000.15,
47 "age": 60,
48 },
49 )
50
51 def test_nested_json_object(self):
52 obj = Author.objects.annotate(
53 json_object=JSONObject(
54 name="name",
55 nested_json_object=JSONObject(
56 alias="alias",
57 age="age",
58 ),
59 )
60 ).first()
61 self.assertEqual(
62 obj.json_object,
63 {
64 "name": "Ivan Ivanov",
65 "nested_json_object": {
66 "alias": "iivanov",
67 "age": 30,
68 },
69 },
70 )
71
72 def test_nested_empty_json_object(self):
73 obj = Author.objects.annotate(
74 json_object=JSONObject(
75 name="name",
76 nested_json_object=JSONObject(),
77 )
78 ).first()
79 self.assertEqual(
80 obj.json_object,
81 {
82 "name": "Ivan Ivanov",
83 "nested_json_object": {},
84 },
85 )
86
87 def test_textfield(self):
88 Article.objects.create(
89 title="The Title",
90 text="x" * 4000,
91 written=timezone.now(),
92 )
93 obj = Article.objects.annotate(json_object=JSONObject(text=F("text"))).first()
94 self.assertEqual(obj.json_object, {"text": "x" * 4000})
95
96 def test_order_by_key(self):
97 qs = Author.objects.annotate(attrs=JSONObject(alias=F("alias"))).order_by(
98 "attrs__alias"
99 )
100 self.assertQuerySetEqual(qs, Author.objects.order_by("alias"))
101
102 def test_order_by_nested_key(self):
103 qs = Author.objects.annotate(
104 attrs=JSONObject(nested=JSONObject(alias=F("alias")))
105 ).order_by("-attrs__nested__alias")
106 self.assertQuerySetEqual(qs, Author.objects.order_by("-alias"))
107
108
109 @skipIfDBFeature("has_json_object_function")
110 class JSONObjectNotSupportedTests(TestCase):
111 def test_not_supported(self):
112 msg = "JSONObject() is not supported on this database backend."
113 with self.assertRaisesMessage(NotSupportedError, msg):
114 Author.objects.annotate(json_object=JSONObject()).get()