0
0
mirror of https://github.com/django/django.git synced 2024-12-01 15:42:04 +01:00

Refs #31956 -- Added test for ordering by JSONField with a custom decoder.

This commit is contained in:
Mariusz Felisiak 2020-08-28 08:33:07 +02:00
parent 4c0b4720b0
commit 2210539142

View File

@ -313,20 +313,28 @@ class TestQuerying(TestCase):
)
def test_ordering_by_transform(self):
objs = [
NullableJSONModel.objects.create(value={'ord': 93, 'name': 'bar'}),
NullableJSONModel.objects.create(value={'ord': 22.1, 'name': 'foo'}),
NullableJSONModel.objects.create(value={'ord': -1, 'name': 'baz'}),
NullableJSONModel.objects.create(value={'ord': 21.931902, 'name': 'spam'}),
NullableJSONModel.objects.create(value={'ord': -100291029, 'name': 'eggs'}),
]
query = NullableJSONModel.objects.filter(value__name__isnull=False).order_by('value__ord')
expected = [objs[4], objs[2], objs[3], objs[1], objs[0]]
mariadb = connection.vendor == 'mysql' and connection.mysql_is_mariadb
if mariadb or connection.vendor == 'oracle':
# MariaDB and Oracle return JSON values as strings.
expected = [objs[2], objs[4], objs[3], objs[1], objs[0]]
self.assertSequenceEqual(query, expected)
values = [
{'ord': 93, 'name': 'bar'},
{'ord': 22.1, 'name': 'foo'},
{'ord': -1, 'name': 'baz'},
{'ord': 21.931902, 'name': 'spam'},
{'ord': -100291029, 'name': 'eggs'},
]
for field_name in ['value', 'value_custom']:
with self.subTest(field=field_name):
objs = [
NullableJSONModel.objects.create(**{field_name: value})
for value in values
]
query = NullableJSONModel.objects.filter(
**{'%s__name__isnull' % field_name: False},
).order_by('%s__ord' % field_name)
expected = [objs[4], objs[2], objs[3], objs[1], objs[0]]
if mariadb or connection.vendor == 'oracle':
# MariaDB and Oracle return JSON values as strings.
expected = [objs[2], objs[4], objs[3], objs[1], objs[0]]
self.assertSequenceEqual(query, expected)
def test_ordering_grouping_by_key_transform(self):
base_qs = NullableJSONModel.objects.filter(value__d__0__isnull=False)