Skip to content

Commit

Permalink
[1.5.x] Use new TestCase methods for equality comparisons
Browse files Browse the repository at this point in the history
Backport of 8d35fd4 from master
  • Loading branch information
clelland authored and spookylukey committed Dec 24, 2012
1 parent 5eba053 commit 515cf94
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion django/contrib/formtools/tests/wizard/cookiestorage.py
Expand Up @@ -43,5 +43,5 @@ def test_reset_cookie(self):
storage.init_data() storage.init_data()
storage.update_response(response) storage.update_response(response)
unsigned_cookie_data = cookie_signer.unsign(response.cookies[storage.prefix].value) unsigned_cookie_data = cookie_signer.unsign(response.cookies[storage.prefix].value)
self.assertEqual(json.loads(unsigned_cookie_data), self.assertJSONEqual(unsigned_cookie_data,
{"step_files": {}, "step": None, "extra_data": {}, "step_data": {}}) {"step_files": {}, "step": None, "extra_data": {}, "step_data": {}})
6 changes: 5 additions & 1 deletion tests/modeltests/field_subclassing/tests.py
Expand Up @@ -61,7 +61,11 @@ def test_custom_field(self):


# Serialization works, too. # Serialization works, too.
stream = serializers.serialize("json", MyModel.objects.all()) stream = serializers.serialize("json", MyModel.objects.all())
self.assertEqual(stream, '[{"pk": %d, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]' % m1.pk) self.assertJSONEqual(stream, [{
"pk": m1.pk,
"model": "field_subclassing.mymodel",
"fields": {"data": "12", "name": "m"}
}])


obj = list(serializers.deserialize("json", stream))[0] obj = list(serializers.deserialize("json", stream))[0]
self.assertEqual(obj.object, m) self.assertEqual(obj.object, m)
Expand Down
19 changes: 11 additions & 8 deletions tests/modeltests/fixtures/tests.py
Expand Up @@ -22,7 +22,7 @@ def testClassFixtures(self):
]) ])




class FixtureLoadingTests(TestCase): class DumpDataAssertMixin(object):


def _dumpdata_assert(self, args, output, format='json', natural_keys=False, def _dumpdata_assert(self, args, output, format='json', natural_keys=False,
use_base_manager=False, exclude_list=[]): use_base_manager=False, exclude_list=[]):
Expand All @@ -34,7 +34,15 @@ def _dumpdata_assert(self, args, output, format='json', natural_keys=False,
'use_base_manager': use_base_manager, 'use_base_manager': use_base_manager,
'exclude': exclude_list}) 'exclude': exclude_list})
command_output = new_io.getvalue().strip() command_output = new_io.getvalue().strip()
self.assertEqual(command_output, output) if format == "json":
self.assertJSONEqual(command_output, output)
elif format == "xml":
self.assertXMLEqual(command_output, output)
else:
self.assertEqual(command_output, output)


class FixtureLoadingTests(DumpDataAssertMixin, TestCase):


def test_initial_data(self): def test_initial_data(self):
# syncdb introduces 1 initial data object from initial_data.json. # syncdb introduces 1 initial data object from initial_data.json.
Expand Down Expand Up @@ -290,12 +298,7 @@ def test_output_formats(self):
<django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker has no place on ESPN</field><field type="DateTimeField" name="pub_date">2006-06-16T12:00:00</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Time to reform copyright</field><field type="DateTimeField" name="pub_date">2006-06-16T13:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">law</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Prince</field></object><object pk="10" model="fixtures.book"><field type="CharField" name="name">Achieving self-awareness of Python programs</field><field to="fixtures.person" name="authors" rel="ManyToManyRel"></field></object></django-objects>""", format='xml', natural_keys=True) <django-objects version="1.0"><object pk="1" model="fixtures.category"><field type="CharField" name="title">News Stories</field><field type="TextField" name="description">Latest news stories</field></object><object pk="2" model="fixtures.article"><field type="CharField" name="headline">Poker has no place on ESPN</field><field type="DateTimeField" name="pub_date">2006-06-16T12:00:00</field></object><object pk="3" model="fixtures.article"><field type="CharField" name="headline">Time to reform copyright</field><field type="DateTimeField" name="pub_date">2006-06-16T13:00:00</field></object><object pk="1" model="fixtures.tag"><field type="CharField" name="name">copyright</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="2" model="fixtures.tag"><field type="CharField" name="name">law</field><field to="contenttypes.contenttype" name="tagged_type" rel="ManyToOneRel"><natural>fixtures</natural><natural>article</natural></field><field type="PositiveIntegerField" name="tagged_id">3</field></object><object pk="1" model="fixtures.person"><field type="CharField" name="name">Django Reinhardt</field></object><object pk="2" model="fixtures.person"><field type="CharField" name="name">Stephane Grappelli</field></object><object pk="3" model="fixtures.person"><field type="CharField" name="name">Prince</field></object><object pk="10" model="fixtures.book"><field type="CharField" name="name">Achieving self-awareness of Python programs</field><field to="fixtures.person" name="authors" rel="ManyToManyRel"></field></object></django-objects>""", format='xml', natural_keys=True)




class FixtureTransactionTests(TransactionTestCase): class FixtureTransactionTests(DumpDataAssertMixin, TransactionTestCase):
def _dumpdata_assert(self, args, output, format='json'):
new_io = six.StringIO()
management.call_command('dumpdata', *args, **{'format': format, 'stdout': new_io})
command_output = new_io.getvalue().strip()
self.assertEqual(command_output, output)


@skipUnlessDBFeature('supports_forward_references') @skipUnlessDBFeature('supports_forward_references')
def test_format_discovery(self): def test_format_discovery(self):
Expand Down
7 changes: 3 additions & 4 deletions tests/regressiontests/admin_changelist/tests.py
Expand Up @@ -122,12 +122,11 @@ def test_result_list_editable_html(self):
table_output = template.render(context) table_output = template.render(context)
# make sure that hidden fields are in the correct place # make sure that hidden fields are in the correct place
hiddenfields_div = '<div class="hiddenfields"><input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /></div>' % new_child.id hiddenfields_div = '<div class="hiddenfields"><input type="hidden" name="form-0-id" value="%d" id="id_form-0-id" /></div>' % new_child.id
self.assertFalse(table_output.find(hiddenfields_div) == -1, self.assertInHTML(hiddenfields_div, table_output, msg_prefix='Failed to find hidden fields')
'Failed to find hidden fields in: %s' % table_output)
# make sure that list editable fields are rendered in divs correctly # make sure that list editable fields are rendered in divs correctly
editable_name_field = '<input name="form-0-name" value="name" class="vTextField" maxlength="30" type="text" id="id_form-0-name" />' editable_name_field = '<input name="form-0-name" value="name" class="vTextField" maxlength="30" type="text" id="id_form-0-name" />'
self.assertFalse('<td>%s</td>' % editable_name_field == -1, self.assertInHTML('<td>%s</td>' % editable_name_field, table_output, msg_prefix='Failed to find "name" list_editable field')
'Failed to find "name" list_editable field in: %s' % table_output)


def test_result_list_editable(self): def test_result_list_editable(self):
""" """
Expand Down
4 changes: 2 additions & 2 deletions tests/regressiontests/fixtures_regress/tests.py
Expand Up @@ -358,7 +358,7 @@ def test_proxy_model_included(self):
format='json', format='json',
stdout=stdout stdout=stdout
) )
self.assertEqual( self.assertJSONEqual(
stdout.getvalue(), stdout.getvalue(),
"""[{"pk": %d, "model": "fixtures_regress.widget", "fields": {"name": "grommet"}}]""" """[{"pk": %d, "model": "fixtures_regress.widget", "fields": {"name": "grommet"}}]"""
% widget.pk % widget.pk
Expand Down Expand Up @@ -519,7 +519,7 @@ def test_nk_on_serialize(self):
use_natural_keys=True, use_natural_keys=True,
stdout=stdout, stdout=stdout,
) )
self.assertEqual( self.assertJSONEqual(
stdout.getvalue(), stdout.getvalue(),
"""[{"pk": 2, "model": "fixtures_regress.store", "fields": {"main": null, "name": "Amazon"}}, {"pk": 3, "model": "fixtures_regress.store", "fields": {"main": null, "name": "Borders"}}, {"pk": 4, "model": "fixtures_regress.person", "fields": {"name": "Neal Stephenson"}}, {"pk": 1, "model": "fixtures_regress.book", "fields": {"stores": [["Amazon"], ["Borders"]], "name": "Cryptonomicon", "author": ["Neal Stephenson"]}}]""" """[{"pk": 2, "model": "fixtures_regress.store", "fields": {"main": null, "name": "Amazon"}}, {"pk": 3, "model": "fixtures_regress.store", "fields": {"main": null, "name": "Borders"}}, {"pk": 4, "model": "fixtures_regress.person", "fields": {"name": "Neal Stephenson"}}, {"pk": 1, "model": "fixtures_regress.book", "fields": {"stores": [["Amazon"], ["Borders"]], "name": "Cryptonomicon", "author": ["Neal Stephenson"]}}]"""
) )
Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/i18n/tests.py
Expand Up @@ -634,7 +634,7 @@ def test_localized_input(self):
self.assertEqual(datetime.datetime(2009, 12, 31, 6, 0, 0), form6.cleaned_data['date_added']) self.assertEqual(datetime.datetime(2009, 12, 31, 6, 0, 0), form6.cleaned_data['date_added'])
with self.settings(USE_THOUSAND_SEPARATOR=True): with self.settings(USE_THOUSAND_SEPARATOR=True):
# Checking for the localized "products_delivered" field # Checking for the localized "products_delivered" field
self.assertTrue('<input type="text" name="products_delivered" value="12.000" id="id_products_delivered" />' in form6.as_ul()) self.assertInHTML('<input type="text" name="products_delivered" value="12.000" id="id_products_delivered" />', form6.as_ul())


def test_iter_format_modules(self): def test_iter_format_modules(self):
""" """
Expand Down
6 changes: 3 additions & 3 deletions tests/regressiontests/m2m_through_regress/tests.py
Expand Up @@ -71,12 +71,12 @@ def test_serialization(self):


out = StringIO() out = StringIO()
management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out) management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
self.assertEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks) self.assertJSONEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks)


out = StringIO() out = StringIO()
management.call_command("dumpdata", "m2m_through_regress", format="xml", management.call_command("dumpdata", "m2m_through_regress", format="xml",
indent=2, stdout=out) indent=2, stdout=out)
self.assertEqual(out.getvalue().strip(), """ self.assertXMLEqual(out.getvalue().strip(), """
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0"> <django-objects version="1.0">
<object pk="%(m_pk)s" model="m2m_through_regress.membership"> <object pk="%(m_pk)s" model="m2m_through_regress.membership">
Expand Down Expand Up @@ -229,4 +229,4 @@ def test_sequence_creation(self):
"Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107" "Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107"
out = StringIO() out = StringIO()
management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out) management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
self.assertEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""") self.assertJSONEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""")

0 comments on commit 515cf94

Please sign in to comment.