Don't rely on dictionary ordering in tests

This commit is contained in:
Ian Clelland 2012-09-28 00:27:38 -07:00 committed by Luke Plant
parent 585aa11d23
commit b9fc70141a
4 changed files with 35 additions and 25 deletions

View File

@ -18,6 +18,7 @@ from django.utils.encoding import force_text
from django.utils._os import upath from django.utils._os import upath
from django.utils import six from django.utils import six
from django.utils.six import PY3, StringIO from django.utils.six import PY3, StringIO
import json
from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget, from .models import (Animal, Stuff, Absolute, Parent, Child, Article, Widget,
Store, Person, Book, NKChild, RefToNKChild, Circle1, Circle2, Circle3, Store, Person, Book, NKChild, RefToNKChild, Circle1, Circle2, Circle3,
@ -334,15 +335,17 @@ class TestFixtures(TestCase):
# between different Python versions. # between different Python versions.
data = re.sub('0{6,}\d', '', data) 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"}}' animals_data = sorted([
emu_json = '{"pk": 10, "model": "fixtures_regress.animal", "fields": {"count": 42, "weight": 1.2, "name": "Emu", "latin_name": "Dromaius novaehollandiae"}}' {"pk": 1, "model": "fixtures_regress.animal", "fields": {"count": 3, "weight": 1.2, "name": "Lion", "latin_name": "Panthera leo"}},
platypus_json = '{"pk": %d, "model": "fixtures_regress.animal", "fields": {"count": 2, "weight": 2.2, "name": "Platypus", "latin_name": "Ornithorhynchus anatinus"}}' {"pk": 10, "model": "fixtures_regress.animal", "fields": {"count": 42, "weight": 1.2, "name": "Emu", "latin_name": "Dromaius novaehollandiae"}},
platypus_json = platypus_json % animal.pk {"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): def test_proxy_model_included(self):
""" """

View File

@ -11,6 +11,7 @@ from django.test import TestCase
from django.test.utils import str_prefix from django.test.utils import str_prefix
from django.utils.datastructures import MultiValueDict, MergeDict from django.utils.datastructures import MultiValueDict, MergeDict
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils import six
class Person(Form): class Person(Form):
@ -136,11 +137,7 @@ class FormsTestCase(TestCase):
self.assertEqual(p.errors['first_name'], ['This field is required.']) self.assertEqual(p.errors['first_name'], ['This field is required.'])
self.assertEqual(p.errors['birthday'], ['This field is required.']) self.assertEqual(p.errors['birthday'], ['This field is required.'])
self.assertFalse(p.is_valid()) self.assertFalse(p.is_valid())
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>') self.assertDictEqual(p.errors, {'birthday': ['This field is required.'], 'first_name': ['This field is required.']})
self.assertEqual(p.errors.as_text(), """* first_name
* This field is required.
* birthday
* This field is required.""")
self.assertEqual(p.cleaned_data, {'last_name': 'Lennon'}) self.assertEqual(p.cleaned_data, {'last_name': 'Lennon'})
self.assertEqual(p['first_name'].errors, ['This field is required.']) self.assertEqual(p['first_name'].errors, ['This field is required.'])
self.assertHTMLEqual(p['first_name'].errors.as_ul(), '<ul class="errorlist"><li>This field is required.</li></ul>') self.assertHTMLEqual(p['first_name'].errors.as_ul(), '<ul class="errorlist"><li>This field is required.</li></ul>')
@ -1495,7 +1492,7 @@ class FormsTestCase(TestCase):
form = UserRegistration(auto_id=False) form = UserRegistration(auto_id=False)
if form.is_valid(): if form.is_valid():
return 'VALID: %r' % form.cleaned_data return 'VALID: %r' % sorted(six.iteritems(form.cleaned_data))
t = Template('<form action="" method="post">\n<table>\n{{ form }}\n</table>\n<input type="submit" />\n</form>') t = Template('<form action="" method="post">\n<table>\n{{ form }}\n</table>\n<input type="submit" />\n</form>')
return t.render(Context({'form': form})) return t.render(Context({'form': form}))
@ -1520,7 +1517,8 @@ class FormsTestCase(TestCase):
<input type="submit" /> <input type="submit" />
</form>""") </form>""")
# Case 3: POST with valid data (the success message).) # 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): def test_templates_with_forms(self):
class UserRegistration(Form): class UserRegistration(Form):

View File

@ -130,15 +130,14 @@ class QueryDictTests(unittest.TestCase):
self.assertTrue(q.has_key('foo')) self.assertTrue(q.has_key('foo'))
self.assertTrue('foo' in q) self.assertTrue('foo' in q)
self.assertEqual(sorted(list(six.iteritems(q))), self.assertListEqual(sorted(list(six.iteritems(q))),
[('foo', 'another'), ('name', 'john')]) [('foo', 'another'), ('name', 'john')])
self.assertEqual(sorted(list(six.iterlists(q))), self.assertListEqual(sorted(list(six.iterlists(q))),
[('foo', ['bar', 'baz', 'another']), ('name', ['john'])]) [('foo', ['bar', 'baz', 'another']), ('name', ['john'])])
self.assertEqual(sorted(list(six.iterkeys(q))), self.assertListEqual(sorted(list(six.iterkeys(q))),
['foo', 'name']) ['foo', 'name'])
self.assertEqual(sorted(list(six.itervalues(q))), self.assertListEqual(sorted(list(six.itervalues(q))),
['another', 'john']) ['another', 'john'])
self.assertEqual(len(q), 2)
q.update({'foo': 'hello'}) q.update({'foo': 'hello'})
self.assertEqual(q['foo'], 'hello') self.assertEqual(q['foo'], 'hello')

View File

@ -90,7 +90,17 @@ class MailTests(TestCase):
""" """
headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} 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) 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): def test_from_header(self):
""" """