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.create(name="Ivan Ivanov", alias="iivanov")
    
  16. 
    
  17.     def test_empty(self):
    
  18.         obj = Author.objects.annotate(json_object=JSONObject()).first()
    
  19.         self.assertEqual(obj.json_object, {})
    
  20. 
    
  21.     def test_basic(self):
    
  22.         obj = Author.objects.annotate(json_object=JSONObject(name="name")).first()
    
  23.         self.assertEqual(obj.json_object, {"name": "Ivan Ivanov"})
    
  24. 
    
  25.     def test_expressions(self):
    
  26.         obj = Author.objects.annotate(
    
  27.             json_object=JSONObject(
    
  28.                 name=Lower("name"),
    
  29.                 alias="alias",
    
  30.                 goes_by="goes_by",
    
  31.                 salary=Value(30000.15),
    
  32.                 age=F("age") * 2,
    
  33.             )
    
  34.         ).first()
    
  35.         self.assertEqual(
    
  36.             obj.json_object,
    
  37.             {
    
  38.                 "name": "ivan ivanov",
    
  39.                 "alias": "iivanov",
    
  40.                 "goes_by": None,
    
  41.                 "salary": 30000.15,
    
  42.                 "age": 60,
    
  43.             },
    
  44.         )
    
  45. 
    
  46.     def test_nested_json_object(self):
    
  47.         obj = Author.objects.annotate(
    
  48.             json_object=JSONObject(
    
  49.                 name="name",
    
  50.                 nested_json_object=JSONObject(
    
  51.                     alias="alias",
    
  52.                     age="age",
    
  53.                 ),
    
  54.             )
    
  55.         ).first()
    
  56.         self.assertEqual(
    
  57.             obj.json_object,
    
  58.             {
    
  59.                 "name": "Ivan Ivanov",
    
  60.                 "nested_json_object": {
    
  61.                     "alias": "iivanov",
    
  62.                     "age": 30,
    
  63.                 },
    
  64.             },
    
  65.         )
    
  66. 
    
  67.     def test_nested_empty_json_object(self):
    
  68.         obj = Author.objects.annotate(
    
  69.             json_object=JSONObject(
    
  70.                 name="name",
    
  71.                 nested_json_object=JSONObject(),
    
  72.             )
    
  73.         ).first()
    
  74.         self.assertEqual(
    
  75.             obj.json_object,
    
  76.             {
    
  77.                 "name": "Ivan Ivanov",
    
  78.                 "nested_json_object": {},
    
  79.             },
    
  80.         )
    
  81. 
    
  82.     def test_textfield(self):
    
  83.         Article.objects.create(
    
  84.             title="The Title",
    
  85.             text="x" * 4000,
    
  86.             written=timezone.now(),
    
  87.         )
    
  88.         obj = Article.objects.annotate(json_object=JSONObject(text=F("text"))).first()
    
  89.         self.assertEqual(obj.json_object, {"text": "x" * 4000})
    
  90. 
    
  91. 
    
  92. @skipIfDBFeature("has_json_object_function")
    
  93. class JSONObjectNotSupportedTests(TestCase):
    
  94.     def test_not_supported(self):
    
  95.         msg = "JSONObject() is not supported on this database backend."
    
  96.         with self.assertRaisesMessage(NotSupportedError, msg):
    
  97.             Author.objects.annotate(json_object=JSONObject()).get()