From 5bc3123479bd97dc9d8a36fa9a3421a71063d1da Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 1 Apr 2015 15:42:09 -0400 Subject: Fixed #24558 -- Made dumpdata mapping ordering deterministic. Thanks to gfairchild for the report and Claude for the review. --- tests/serializers/tests.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests/timezones/tests.py | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/serializers/tests.py b/tests/serializers/tests.py index 5448b01b29..1d0a0bead5 100644 --- a/tests/serializers/tests.py +++ b/tests/serializers/tests.py @@ -266,6 +266,17 @@ class SerializersTestBase(object): obj.save() self.assertEqual(Category.objects.all().count(), 5) + def test_deterministic_mapping_ordering(self): + """Mapping such as fields should be deterministically ordered. (#24558)""" + output = serializers.serialize(self.serializer_name, [self.a1], indent=2) + categories = self.a1.categories.values_list('pk', flat=True) + self.assertEqual(output, self.mapping_ordering_str % { + 'article_pk': self.a1.pk, + 'author_pk': self.a1.author_id, + 'first_category_pk': categories[0], + 'second_category_pk': categories[1], + }) + class SerializersTransactionTestBase(object): @@ -303,6 +314,15 @@ class XmlSerializerTestCase(SerializersTestBase, TestCase): Non-fiction """ + mapping_ordering_str = """ + + + %(author_pk)s + Poker has no place on ESPN + 2006-06-16T11:00:00 + + +""" @staticmethod def _comparison_value(value): @@ -373,6 +393,22 @@ class JsonSerializerTestCase(SerializersTestBase, TestCase): "model": "serializers.category", "fields": {"name": "Non-fiction"} }]""" + mapping_ordering_str = """[ +{ + "model": "serializers.article", + "pk": %(article_pk)s, + "fields": { + "author": %(author_pk)s, + "headline": "Poker has no place on ESPN", + "pub_date": "2006-06-16T11:00:00", + "categories": [ + %(first_category_pk)s, + %(second_category_pk)s + ] + } +} +] +""" @staticmethod def _validate_output(serial_str): @@ -533,6 +569,15 @@ class YamlSerializerTestCase(SerializersTestBase, TestCase): name: Non-fiction model: serializers.category""" + mapping_ordering_str = """- model: serializers.article + pk: %(article_pk)s + fields: + author: %(author_pk)s + headline: Poker has no place on ESPN + pub_date: 2006-06-16 11:00:00 + categories: [%(first_category_pk)s, %(second_category_pk)s] +""" + @staticmethod def _validate_output(serial_str): try: diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py index d623492f68..d87e2d2566 100644 --- a/tests/timezones/tests.py +++ b/tests/timezones/tests.py @@ -594,7 +594,7 @@ class SerializationTests(TestCase): def assert_yaml_contains_datetime(self, yaml, dt): # Depending on the yaml dumper, '!timestamp' might be absent six.assertRegex(self, yaml, - r"- fields: {dt: !(!timestamp)? '%s'}" % re.escape(dt)) + r"\n fields: {dt: !(!timestamp)? '%s'}" % re.escape(dt)) def test_naive_datetime(self): dt = datetime.datetime(2011, 9, 1, 13, 20, 30) -- cgit v1.3