diff options
| author | Artur Beltsov <artur1998g@gmail.com> | 2020-11-08 12:52:34 +0500 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-12-02 09:30:50 +0100 |
| commit | 48b4bae983a1f9a73624fe62ef5bd1130b0dba39 (patch) | |
| tree | fb4759d8fcaadd48f45c08cf28f080f52690a356 /tests/db_functions | |
| parent | adb40d217ec57ade46b1394cfbf3c513dc669445 (diff) | |
Fixed #32179 -- Added JSONObject database function.
Diffstat (limited to 'tests/db_functions')
| -rw-r--r-- | tests/db_functions/comparison/test_json_object.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/tests/db_functions/comparison/test_json_object.py b/tests/db_functions/comparison/test_json_object.py new file mode 100644 index 0000000000..2c7c1beae9 --- /dev/null +++ b/tests/db_functions/comparison/test_json_object.py @@ -0,0 +1,82 @@ +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.create(name='Ivan Ivanov', alias='iivanov') + + 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}) + + +@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() |
