Refs -- Added test for ordering by JSONField with a custom decoder.
@@ -313,16 +313,24 @@ class TestQuerying(TestCase):
)
def test_ordering_by_transform(self):
+ 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(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'}),
+ NullableJSONModel.objects.create(**{field_name: value})
+ for value in values
]
- query = NullableJSONModel.objects.filter(value__name__isnull=False).order_by('value__ord')
+ 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]]
- 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]]
