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
1 changed files with 21 additions and 13 deletions

View File

@ -313,16 +313,24 @@ 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
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]]