From cade3405c05097c13d316e8423f70474f4bed8c3 Mon Sep 17 00:00:00 2001 From: Ian Clelland Date: Fri, 28 Sep 2012 00:27:38 -0700 Subject: [PATCH] [1.5.x] Don't rely on dictionary ordering in tests Backport of b9fc70141abd4c812b03df50452a0d3ed8cd62d2 from master --- .../regressiontests/fixtures_regress/tests.py | 19 +++++++++++-------- tests/regressiontests/forms/tests/forms.py | 12 +++++------- tests/regressiontests/httpwrappers/tests.py | 17 ++++++++--------- tests/regressiontests/mail/tests.py | 12 +++++++++++- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/tests/regressiontests/fixtures_regress/tests.py b/tests/regressiontests/fixtures_regress/tests.py index 6791143473..61dc4460df 100644 --- a/tests/regressiontests/fixtures_regress/tests.py +++ b/tests/regressiontests/fixtures_regress/tests.py @@ -18,6 +18,7 @@ from django.utils.encoding import force_text from django.utils._os import upath from django.utils import six from django.utils.six import PY3, StringIO +import json from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget, Store, Person, Book, NKChild, RefToNKChild, Circle1, Circle2, Circle3, @@ -334,15 +335,17 @@ class TestFixtures(TestCase): # between different Python versions. data = re.sub('0{6,}\d', '', data) - lion_json = '{"pk": 1, "model": "fixtures_regress.animal", "fields": {"count": 3, "weight": 1.2, "name": "Lion", "latin_name": "Panthera leo"}}' - emu_json = '{"pk": 10, "model": "fixtures_regress.animal", "fields": {"count": 42, "weight": 1.2, "name": "Emu", "latin_name": "Dromaius novaehollandiae"}}' - platypus_json = '{"pk": %d, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.2, "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}}' - platypus_json = platypus_json % animal.pk + animals_data = sorted([ + {"pk": 1, "model": "fixtures_regress.animal", "fields": {"count": 3, "weight": 1.2, "name": "Lion", "latin_name": "Panthera leo"}}, + {"pk": 10, "model": "fixtures_regress.animal", "fields": {"count": 42, "weight": 1.2, "name": "Emu", "latin_name": "Dromaius novaehollandiae"}}, + {"pk": animal.pk, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.2, "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}}, + ], key=lambda x: x["pk"]) + + data = sorted(json.loads(data), key=lambda x: x["pk"]) + + self.maxDiff = 1024 + self.assertEqual(data, animals_data) - self.assertEqual(len(data), len('[%s]' % ', '.join([lion_json, emu_json, platypus_json]))) - self.assertTrue(lion_json in data) - self.assertTrue(emu_json in data) - self.assertTrue(platypus_json in data) def test_proxy_model_included(self): """ diff --git a/tests/regressiontests/forms/tests/forms.py b/tests/regressiontests/forms/tests/forms.py index 1c83ed04d4..ade06845f8 100644 --- a/tests/regressiontests/forms/tests/forms.py +++ b/tests/regressiontests/forms/tests/forms.py @@ -11,6 +11,7 @@ from django.test import TestCase from django.test.utils import str_prefix from django.utils.datastructures import MultiValueDict, MergeDict from django.utils.safestring import mark_safe +from django.utils import six class Person(Form): @@ -136,11 +137,7 @@ class FormsTestCase(TestCase): self.assertEqual(p.errors['first_name'], ['This field is required.']) self.assertEqual(p.errors['birthday'], ['This field is required.']) self.assertFalse(p.is_valid()) - self.assertHTMLEqual(p.errors.as_ul(), '') - self.assertEqual(p.errors.as_text(), """* first_name - * This field is required. -* birthday - * This field is required.""") + self.assertDictEqual(p.errors, {'birthday': ['This field is required.'], 'first_name': ['This field is required.']}) self.assertEqual(p.cleaned_data, {'last_name': 'Lennon'}) self.assertEqual(p['first_name'].errors, ['This field is required.']) self.assertHTMLEqual(p['first_name'].errors.as_ul(), '') @@ -1495,7 +1492,7 @@ class FormsTestCase(TestCase): form = UserRegistration(auto_id=False) if form.is_valid(): - return 'VALID: %r' % form.cleaned_data + return 'VALID: %r' % sorted(six.iteritems(form.cleaned_data)) t = Template('
\n\n{{ form }}\n
\n\n
') return t.render(Context({'form': form})) @@ -1520,7 +1517,8 @@ class FormsTestCase(TestCase): """) # Case 3: POST with valid data (the success message).) - self.assertHTMLEqual(my_function('POST', {'username': 'adrian', 'password1': 'secret', 'password2': 'secret'}), str_prefix("VALID: {'username': %(_)s'adrian', 'password1': %(_)s'secret', 'password2': %(_)s'secret'}")) + self.assertEqual(my_function('POST', {'username': 'adrian', 'password1': 'secret', 'password2': 'secret'}), + str_prefix("VALID: [('password1', %(_)s'secret'), ('password2', %(_)s'secret'), ('username', %(_)s'adrian')]")) def test_templates_with_forms(self): class UserRegistration(Form): diff --git a/tests/regressiontests/httpwrappers/tests.py b/tests/regressiontests/httpwrappers/tests.py index a601e541c6..d26a728e7b 100644 --- a/tests/regressiontests/httpwrappers/tests.py +++ b/tests/regressiontests/httpwrappers/tests.py @@ -130,15 +130,14 @@ class QueryDictTests(unittest.TestCase): self.assertTrue(q.has_key('foo')) self.assertTrue('foo' in q) - self.assertEqual(sorted(list(six.iteritems(q))), - [('foo', 'another'), ('name', 'john')]) - self.assertEqual(sorted(list(six.iterlists(q))), - [('foo', ['bar', 'baz', 'another']), ('name', ['john'])]) - self.assertEqual(sorted(list(six.iterkeys(q))), - ['foo', 'name']) - self.assertEqual(sorted(list(six.itervalues(q))), - ['another', 'john']) - self.assertEqual(len(q), 2) + self.assertListEqual(sorted(list(six.iteritems(q))), + [('foo', 'another'), ('name', 'john')]) + self.assertListEqual(sorted(list(six.iterlists(q))), + [('foo', ['bar', 'baz', 'another']), ('name', ['john'])]) + self.assertListEqual(sorted(list(six.iterkeys(q))), + ['foo', 'name']) + self.assertListEqual(sorted(list(six.itervalues(q))), + ['another', 'john']) q.update({'foo': 'hello'}) self.assertEqual(q['foo'], 'hello') diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py index b798cb21aa..a8fbf20fd6 100644 --- a/tests/regressiontests/mail/tests.py +++ b/tests/regressiontests/mail/tests.py @@ -90,7 +90,17 @@ class MailTests(TestCase): """ headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers) - self.assertEqual(email.message().as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: subject\nFrom: from@example.com\nTo: to@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent') + + self.assertEqual(sorted(email.message().items()), [ + ('Content-Transfer-Encoding', '7bit'), + ('Content-Type', 'text/plain; charset="utf-8"'), + ('From', 'from@example.com'), + ('MIME-Version', '1.0'), + ('Message-ID', 'foo'), + ('Subject', 'subject'), + ('To', 'to@example.com'), + ('date', 'Fri, 09 Nov 2001 01:08:47 -0000'), + ]) def test_from_header(self): """