mirror of https://github.com/django/django.git
Fixed #22465 -- New assertion assertJSONNotEqual
This commit is contained in:
parent
9bc377d7d0
commit
8394e570ba
|
@ -676,6 +676,11 @@ class SimpleTestCase(unittest.TestCase):
|
||||||
msg_prefix + "Couldn't find '%s' in response" % needle)
|
msg_prefix + "Couldn't find '%s' in response" % needle)
|
||||||
|
|
||||||
def assertJSONEqual(self, raw, expected_data, msg=None):
|
def assertJSONEqual(self, raw, expected_data, msg=None):
|
||||||
|
"""
|
||||||
|
Asserts that the JSON fragments raw and expected_data are equal.
|
||||||
|
Usual JSON non-significant whitespace rules apply as the heavyweight
|
||||||
|
is delegated to the json library.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
data = json.loads(raw)
|
data = json.loads(raw)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -687,6 +692,23 @@ class SimpleTestCase(unittest.TestCase):
|
||||||
self.fail("Second argument is not valid JSON: %r" % expected_data)
|
self.fail("Second argument is not valid JSON: %r" % expected_data)
|
||||||
self.assertEqual(data, expected_data, msg=msg)
|
self.assertEqual(data, expected_data, msg=msg)
|
||||||
|
|
||||||
|
def assertJSONNotEqual(self, raw, expected_data, msg=None):
|
||||||
|
"""
|
||||||
|
Asserts that the JSON fragments raw and expected_data are not equal.
|
||||||
|
Usual JSON non-significant whitespace rules apply as the heavyweight
|
||||||
|
is delegated to the json library.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
data = json.loads(raw)
|
||||||
|
except ValueError:
|
||||||
|
self.fail("First argument is not valid JSON: %r" % raw)
|
||||||
|
if isinstance(expected_data, six.string_types):
|
||||||
|
try:
|
||||||
|
expected_data = json.loads(expected_data)
|
||||||
|
except ValueError:
|
||||||
|
self.fail("Second argument is not valid JSON: %r" % expected_data)
|
||||||
|
self.assertNotEqual(data, expected_data, msg=msg)
|
||||||
|
|
||||||
def assertXMLEqual(self, xml1, xml2, msg=None):
|
def assertXMLEqual(self, xml1, xml2, msg=None):
|
||||||
"""
|
"""
|
||||||
Asserts that two XML snippets are semantically the same.
|
Asserts that two XML snippets are semantically the same.
|
||||||
|
|
|
@ -155,6 +155,9 @@ Tests
|
||||||
:meth:`~django.test.SimpleTestCase.assertTemplateUsed`. This allows you to
|
:meth:`~django.test.SimpleTestCase.assertTemplateUsed`. This allows you to
|
||||||
assert that a template was rendered a specific number of times.
|
assert that a template was rendered a specific number of times.
|
||||||
|
|
||||||
|
* :meth:`~django.test.SimpleTestCase.assertJSONNotEqual` new assertion allow
|
||||||
|
you to test that two JSON fragments are not equal.
|
||||||
|
|
||||||
Validators
|
Validators
|
||||||
^^^^^^^^^^
|
^^^^^^^^^^
|
||||||
|
|
||||||
|
|
|
@ -1458,6 +1458,13 @@ your test suite.
|
||||||
|
|
||||||
Output in case of error can be customized with the ``msg`` argument.
|
Output in case of error can be customized with the ``msg`` argument.
|
||||||
|
|
||||||
|
.. method:: SimpleTestCase.assertJSONNotEqual(raw, expected_data, msg=None)
|
||||||
|
|
||||||
|
Asserts that the JSON fragments ``raw`` and ``expected_data`` are *not* equal.
|
||||||
|
See :meth:`~SimpleTestCase.assertJSONEqual` for further details.
|
||||||
|
|
||||||
|
Output in case of error can be customized with the ``msg`` argument.
|
||||||
|
|
||||||
.. method:: TransactionTestCase.assertQuerysetEqual(qs, values, transform=repr, ordered=True, msg=None)
|
.. method:: TransactionTestCase.assertQuerysetEqual(qs, values, transform=repr, ordered=True, msg=None)
|
||||||
|
|
||||||
Asserts that a queryset ``qs`` returns a particular list of values ``values``.
|
Asserts that a queryset ``qs`` returns a particular list of values ``values``.
|
||||||
|
|
|
@ -542,6 +542,51 @@ class HTMLEqualTests(TestCase):
|
||||||
self.assertContains(response, '<p class="help">Some help text for the title (with unicode ŠĐĆŽćžšđ)</p>', html=True)
|
self.assertContains(response, '<p class="help">Some help text for the title (with unicode ŠĐĆŽćžšđ)</p>', html=True)
|
||||||
|
|
||||||
|
|
||||||
|
class JSONEqualTests(TestCase):
|
||||||
|
def test_simple_equal(self):
|
||||||
|
json1 = '{"attr1": "foo", "attr2":"baz"}'
|
||||||
|
json2 = '{"attr1": "foo", "attr2":"baz"}'
|
||||||
|
self.assertJSONEqual(json1, json2)
|
||||||
|
|
||||||
|
def test_simple_equal_unordered(self):
|
||||||
|
json1 = '{"attr1": "foo", "attr2":"baz"}'
|
||||||
|
json2 = '{"attr2":"baz", "attr1": "foo"}'
|
||||||
|
self.assertJSONEqual(json1, json2)
|
||||||
|
|
||||||
|
def test_simple_equal_raise(self):
|
||||||
|
json1 = '{"attr1": "foo", "attr2":"baz"}'
|
||||||
|
json2 = '{"attr2":"baz"}'
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertJSONEqual(json1, json2)
|
||||||
|
|
||||||
|
def test_equal_parsing_errors(self):
|
||||||
|
invalid_json = '{"attr1": "foo, "attr2":"baz"}'
|
||||||
|
valid_json = '{"attr1": "foo", "attr2":"baz"}'
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertJSONEqual(invalid_json, valid_json)
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertJSONEqual(valid_json, invalid_json)
|
||||||
|
|
||||||
|
def test_simple_not_equal(self):
|
||||||
|
json1 = '{"attr1": "foo", "attr2":"baz"}'
|
||||||
|
json2 = '{"attr2":"baz"}'
|
||||||
|
self.assertJSONNotEqual(json1, json2)
|
||||||
|
|
||||||
|
def test_simple_not_equal_raise(self):
|
||||||
|
json1 = '{"attr1": "foo", "attr2":"baz"}'
|
||||||
|
json2 = '{"attr1": "foo", "attr2":"baz"}'
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertJSONNotEqual(json1, json2)
|
||||||
|
|
||||||
|
def test_not_equal_parsing_errors(self):
|
||||||
|
invalid_json = '{"attr1": "foo, "attr2":"baz"}'
|
||||||
|
valid_json = '{"attr1": "foo", "attr2":"baz"}'
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertJSONNotEqual(invalid_json, valid_json)
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
self.assertJSONNotEqual(valid_json, invalid_json)
|
||||||
|
|
||||||
|
|
||||||
class XMLEqualTests(TestCase):
|
class XMLEqualTests(TestCase):
|
||||||
def test_simple_equal(self):
|
def test_simple_equal(self):
|
||||||
xml1 = "<elem attr1='a' attr2='b' />"
|
xml1 = "<elem attr1='a' attr2='b' />"
|
||||||
|
|
Loading…
Reference in New Issue