From 22105391424cc56f29f153bb76d6a15246152674 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Fri, 28 Aug 2020 08:33:07 +0200 Subject: [PATCH] Refs #31956 -- Added test for ordering by JSONField with a custom decoder. --- tests/model_fields/test_jsonfield.py | 34 +++++++++++++++++----------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/tests/model_fields/test_jsonfield.py b/tests/model_fields/test_jsonfield.py index a250827635..320d518554 100644 --- a/tests/model_fields/test_jsonfield.py +++ b/tests/model_fields/test_jsonfield.py @@ -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)