From d7d711c68cc070d9b6962f43f97ece097162adcc Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Sun, 15 Dec 2024 16:01:45 +0000 Subject: Refs #35718, Refs #32179 -- Moved JSONObject to django.db.models.functions.json. --- tests/db_functions/comparison/test_json_object.py | 114 ---------------------- tests/db_functions/json/__init__.py | 0 tests/db_functions/json/test_json_object.py | 114 ++++++++++++++++++++++ 3 files changed, 114 insertions(+), 114 deletions(-) delete mode 100644 tests/db_functions/comparison/test_json_object.py create mode 100644 tests/db_functions/json/__init__.py create mode 100644 tests/db_functions/json/test_json_object.py (limited to 'tests') diff --git a/tests/db_functions/comparison/test_json_object.py b/tests/db_functions/comparison/test_json_object.py deleted file mode 100644 index 9a3d48288c..0000000000 --- a/tests/db_functions/comparison/test_json_object.py +++ /dev/null @@ -1,114 +0,0 @@ -from django.db import NotSupportedError -from django.db.models import F, Value -from django.db.models.functions import JSONObject, Lower -from django.test import TestCase -from django.test.testcases import skipIfDBFeature, skipUnlessDBFeature -from django.utils import timezone - -from ..models import Article, Author - - -@skipUnlessDBFeature("has_json_object_function") -class JSONObjectTests(TestCase): - @classmethod - def setUpTestData(cls): - Author.objects.bulk_create( - [ - Author(name="Ivan Ivanov", alias="iivanov"), - Author(name="Bertha Berthy", alias="bberthy"), - ] - ) - - def test_empty(self): - obj = Author.objects.annotate(json_object=JSONObject()).first() - self.assertEqual(obj.json_object, {}) - - def test_basic(self): - obj = Author.objects.annotate(json_object=JSONObject(name="name")).first() - self.assertEqual(obj.json_object, {"name": "Ivan Ivanov"}) - - def test_expressions(self): - obj = Author.objects.annotate( - json_object=JSONObject( - name=Lower("name"), - alias="alias", - goes_by="goes_by", - salary=Value(30000.15), - age=F("age") * 2, - ) - ).first() - self.assertEqual( - obj.json_object, - { - "name": "ivan ivanov", - "alias": "iivanov", - "goes_by": None, - "salary": 30000.15, - "age": 60, - }, - ) - - def test_nested_json_object(self): - obj = Author.objects.annotate( - json_object=JSONObject( - name="name", - nested_json_object=JSONObject( - alias="alias", - age="age", - ), - ) - ).first() - self.assertEqual( - obj.json_object, - { - "name": "Ivan Ivanov", - "nested_json_object": { - "alias": "iivanov", - "age": 30, - }, - }, - ) - - def test_nested_empty_json_object(self): - obj = Author.objects.annotate( - json_object=JSONObject( - name="name", - nested_json_object=JSONObject(), - ) - ).first() - self.assertEqual( - obj.json_object, - { - "name": "Ivan Ivanov", - "nested_json_object": {}, - }, - ) - - def test_textfield(self): - Article.objects.create( - title="The Title", - text="x" * 4000, - written=timezone.now(), - ) - obj = Article.objects.annotate(json_object=JSONObject(text=F("text"))).first() - self.assertEqual(obj.json_object, {"text": "x" * 4000}) - - def test_order_by_key(self): - qs = Author.objects.annotate(attrs=JSONObject(alias=F("alias"))).order_by( - "attrs__alias" - ) - self.assertQuerySetEqual(qs, Author.objects.order_by("alias")) - - def test_order_by_nested_key(self): - qs = Author.objects.annotate( - attrs=JSONObject(nested=JSONObject(alias=F("alias"))) - ).order_by("-attrs__nested__alias") - self.assertQuerySetEqual(qs, Author.objects.order_by("-alias")) - - -@skipIfDBFeature("has_json_object_function") -class JSONObjectNotSupportedTests(TestCase): - def test_not_supported(self): - msg = "JSONObject() is not supported on this database backend." - with self.assertRaisesMessage(NotSupportedError, msg): - Author.objects.annotate(json_object=JSONObject()).get() diff --git a/tests/db_functions/json/__init__.py b/tests/db_functions/json/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/db_functions/json/test_json_object.py b/tests/db_functions/json/test_json_object.py new file mode 100644 index 0000000000..9a3d48288c --- /dev/null +++ b/tests/db_functions/json/test_json_object.py @@ -0,0 +1,114 @@ +from django.db import NotSupportedError +from django.db.models import F, Value +from django.db.models.functions import JSONObject, Lower +from django.test import TestCase +from django.test.testcases import skipIfDBFeature, skipUnlessDBFeature +from django.utils import timezone + +from ..models import Article, Author + + +@skipUnlessDBFeature("has_json_object_function") +class JSONObjectTests(TestCase): + @classmethod + def setUpTestData(cls): + Author.objects.bulk_create( + [ + Author(name="Ivan Ivanov", alias="iivanov"), + Author(name="Bertha Berthy", alias="bberthy"), + ] + ) + + def test_empty(self): + obj = Author.objects.annotate(json_object=JSONObject()).first() + self.assertEqual(obj.json_object, {}) + + def test_basic(self): + obj = Author.objects.annotate(json_object=JSONObject(name="name")).first() + self.assertEqual(obj.json_object, {"name": "Ivan Ivanov"}) + + def test_expressions(self): + obj = Author.objects.annotate( + json_object=JSONObject( + name=Lower("name"), + alias="alias", + goes_by="goes_by", + salary=Value(30000.15), + age=F("age") * 2, + ) + ).first() + self.assertEqual( + obj.json_object, + { + "name": "ivan ivanov", + "alias": "iivanov", + "goes_by": None, + "salary": 30000.15, + "age": 60, + }, + ) + + def test_nested_json_object(self): + obj = Author.objects.annotate( + json_object=JSONObject( + name="name", + nested_json_object=JSONObject( + alias="alias", + age="age", + ), + ) + ).first() + self.assertEqual( + obj.json_object, + { + "name": "Ivan Ivanov", + "nested_json_object": { + "alias": "iivanov", + "age": 30, + }, + }, + ) + + def test_nested_empty_json_object(self): + obj = Author.objects.annotate( + json_object=JSONObject( + name="name", + nested_json_object=JSONObject(), + ) + ).first() + self.assertEqual( + obj.json_object, + { + "name": "Ivan Ivanov", + "nested_json_object": {}, + }, + ) + + def test_textfield(self): + Article.objects.create( + title="The Title", + text="x" * 4000, + written=timezone.now(), + ) + obj = Article.objects.annotate(json_object=JSONObject(text=F("text"))).first() + self.assertEqual(obj.json_object, {"text": "x" * 4000}) + + def test_order_by_key(self): + qs = Author.objects.annotate(attrs=JSONObject(alias=F("alias"))).order_by( + "attrs__alias" + ) + self.assertQuerySetEqual(qs, Author.objects.order_by("alias")) + + def test_order_by_nested_key(self): + qs = Author.objects.annotate( + attrs=JSONObject(nested=JSONObject(alias=F("alias"))) + ).order_by("-attrs__nested__alias") + self.assertQuerySetEqual(qs, Author.objects.order_by("-alias")) + + +@skipIfDBFeature("has_json_object_function") +class JSONObjectNotSupportedTests(TestCase): + def test_not_supported(self): + msg = "JSONObject() is not supported on this database backend." + with self.assertRaisesMessage(NotSupportedError, msg): + Author.objects.annotate(json_object=JSONObject()).get() -- cgit v1.3