Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Don't rely on dictionary ordering in tests

Backport of b9fc701 from master
  • Loading branch information...
commit cade3405c05097c13d316e8423f70474f4bed8c3 1 parent 95b4f34
Ian Clelland authored spookylukey committed
19  tests/regressiontests/fixtures_regress/tests.py
@@ -18,6 +18,7 @@
18 18
 from django.utils._os import upath
19 19
 from django.utils import six
20 20
 from django.utils.six import PY3, StringIO
  21
+import json
21 22
 
22 23
 from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget,
23 24
     Store, Person, Book, NKChild, RefToNKChild, Circle1, Circle2, Circle3,
@@ -334,15 +335,17 @@ def test_dumpdata_uses_default_manager(self):
334 335
         # between different Python versions.
335 336
         data = re.sub('0{6,}\d', '', data)
336 337
 
337  
-        lion_json = '{"pk": 1, "model": "fixtures_regress.animal", "fields": {"count": 3, "weight": 1.2, "name": "Lion", "latin_name": "Panthera leo"}}'
338  
-        emu_json = '{"pk": 10, "model": "fixtures_regress.animal", "fields": {"count": 42, "weight": 1.2, "name": "Emu", "latin_name": "Dromaius novaehollandiae"}}'
339  
-        platypus_json = '{"pk": %d, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.2, "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}}'
340  
-        platypus_json = platypus_json % animal.pk
  338
+        animals_data = sorted([
  339
+            {"pk": 1, "model": "fixtures_regress.animal", "fields": {"count": 3, "weight": 1.2, "name": "Lion", "latin_name": "Panthera leo"}},
  340
+            {"pk": 10, "model": "fixtures_regress.animal", "fields": {"count": 42, "weight": 1.2, "name": "Emu", "latin_name": "Dromaius novaehollandiae"}},
  341
+            {"pk": animal.pk, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.2, "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}},
  342
+        ], key=lambda x: x["pk"])
  343
+
  344
+        data = sorted(json.loads(data), key=lambda x: x["pk"])
  345
+
  346
+        self.maxDiff = 1024
  347
+        self.assertEqual(data, animals_data)
341 348
 
342  
-        self.assertEqual(len(data), len('[%s]' % ', '.join([lion_json, emu_json, platypus_json])))
343  
-        self.assertTrue(lion_json in data)
344  
-        self.assertTrue(emu_json in data)
345  
-        self.assertTrue(platypus_json in data)
346 349
 
347 350
     def test_proxy_model_included(self):
348 351
         """
12  tests/regressiontests/forms/tests/forms.py
@@ -11,6 +11,7 @@
11 11
 from django.test.utils import str_prefix
12 12
 from django.utils.datastructures import MultiValueDict, MergeDict
13 13
 from django.utils.safestring import mark_safe
  14
+from django.utils import six
14 15
 
15 16
 
16 17
 class Person(Form):
@@ -136,11 +137,7 @@ def test_unicode_values(self):
136 137
         self.assertEqual(p.errors['first_name'], ['This field is required.'])
137 138
         self.assertEqual(p.errors['birthday'], ['This field is required.'])
138 139
         self.assertFalse(p.is_valid())
139  
-        self.assertHTMLEqual(p.errors.as_ul(), '<ul class="errorlist"><li>first_name<ul class="errorlist"><li>This field is required.</li></ul></li><li>birthday<ul class="errorlist"><li>This field is required.</li></ul></li></ul>')
140  
-        self.assertEqual(p.errors.as_text(), """* first_name
141  
-  * This field is required.
142  
-* birthday
143  
-  * This field is required.""")
  140
+        self.assertDictEqual(p.errors, {'birthday': ['This field is required.'], 'first_name': ['This field is required.']})
144 141
         self.assertEqual(p.cleaned_data, {'last_name': 'Lennon'})
145 142
         self.assertEqual(p['first_name'].errors, ['This field is required.'])
146 143
         self.assertHTMLEqual(p['first_name'].errors.as_ul(), '<ul class="errorlist"><li>This field is required.</li></ul>')
@@ -1495,7 +1492,7 @@ def my_function(method, post_data):
1495 1492
                 form = UserRegistration(auto_id=False)
1496 1493
 
1497 1494
             if form.is_valid():
1498  
-                return 'VALID: %r' % form.cleaned_data
  1495
+                return 'VALID: %r' % sorted(six.iteritems(form.cleaned_data))
1499 1496
 
1500 1497
             t = Template('<form action="" method="post">\n<table>\n{{ form }}\n</table>\n<input type="submit" />\n</form>')
1501 1498
             return t.render(Context({'form': form}))
@@ -1520,7 +1517,8 @@ def my_function(method, post_data):
1520 1517
 <input type="submit" />
1521 1518
 </form>""")
1522 1519
         # Case 3: POST with valid data (the success message).)
1523  
-        self.assertHTMLEqual(my_function('POST', {'username': 'adrian', 'password1': 'secret', 'password2': 'secret'}), str_prefix("VALID: {'username': %(_)s'adrian', 'password1': %(_)s'secret', 'password2': %(_)s'secret'}"))
  1520
+        self.assertEqual(my_function('POST', {'username': 'adrian', 'password1': 'secret', 'password2': 'secret'}),
  1521
+                    str_prefix("VALID: [('password1', %(_)s'secret'), ('password2', %(_)s'secret'), ('username', %(_)s'adrian')]"))
1524 1522
 
1525 1523
     def test_templates_with_forms(self):
1526 1524
         class UserRegistration(Form):
17  tests/regressiontests/httpwrappers/tests.py
@@ -130,15 +130,14 @@ def test_basic_mutable_operations(self):
130 130
             self.assertTrue(q.has_key('foo'))
131 131
         self.assertTrue('foo' in q)
132 132
 
133  
-        self.assertEqual(sorted(list(six.iteritems(q))),
134  
-                         [('foo', 'another'), ('name', 'john')])
135  
-        self.assertEqual(sorted(list(six.iterlists(q))),
136  
-                         [('foo', ['bar', 'baz', 'another']), ('name', ['john'])])
137  
-        self.assertEqual(sorted(list(six.iterkeys(q))),
138  
-                         ['foo', 'name'])
139  
-        self.assertEqual(sorted(list(six.itervalues(q))),
140  
-                         ['another', 'john'])
141  
-        self.assertEqual(len(q), 2)
  133
+        self.assertListEqual(sorted(list(six.iteritems(q))),
  134
+                             [('foo', 'another'), ('name', 'john')])
  135
+        self.assertListEqual(sorted(list(six.iterlists(q))),
  136
+                             [('foo', ['bar', 'baz', 'another']), ('name', ['john'])])
  137
+        self.assertListEqual(sorted(list(six.iterkeys(q))),
  138
+                             ['foo', 'name'])
  139
+        self.assertListEqual(sorted(list(six.itervalues(q))),
  140
+                             ['another', 'john'])
142 141
 
143 142
         q.update({'foo': 'hello'})
144 143
         self.assertEqual(q['foo'], 'hello')
12  tests/regressiontests/mail/tests.py
@@ -90,7 +90,17 @@ def test_message_header_overrides(self):
90 90
         """
91 91
         headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
92 92
         email = EmailMessage('subject', 'content', 'from@example.com', ['to@example.com'], headers=headers)
93  
-        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')
  93
+
  94
+        self.assertEqual(sorted(email.message().items()), [
  95
+            ('Content-Transfer-Encoding', '7bit'),
  96
+            ('Content-Type', 'text/plain; charset="utf-8"'),
  97
+            ('From', 'from@example.com'),
  98
+            ('MIME-Version', '1.0'),
  99
+            ('Message-ID', 'foo'),
  100
+            ('Subject', 'subject'),
  101
+            ('To', 'to@example.com'),
  102
+            ('date', 'Fri, 09 Nov 2001 01:08:47 -0000'),
  103
+        ])
94 104
 
95 105
     def test_from_header(self):
96 106
         """

0 notes on commit cade340

Please sign in to comment.
Something went wrong with that request. Please try again.