summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoramatellanes <dev.amatellanes@gmail.com>2014-04-17 11:44:30 +0200
committerLoic Bistuer <loic.bistuer@gmail.com>2014-04-18 15:11:08 +0700
commit8394e570baf91cff5e5349c0ef06c4f06f06d0b1 (patch)
treee48a289964cc5f275079aedf21a9daa479e155b8
parent9bc377d7d0c33697326172513aac6e8dad58fe96 (diff)
Fixed #22465 -- New assertion assertJSONNotEqual
-rw-r--r--django/test/testcases.py22
-rw-r--r--docs/releases/1.8.txt3
-rw-r--r--docs/topics/testing/tools.txt7
-rw-r--r--tests/test_utils/tests.py45
4 files changed, 77 insertions, 0 deletions
diff --git a/django/test/testcases.py b/django/test/testcases.py
index a7986dd028..46237aecd2 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -676,6 +676,11 @@ class SimpleTestCase(unittest.TestCase):
msg_prefix + "Couldn't find '%s' in response" % needle)
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:
data = json.loads(raw)
except ValueError:
@@ -687,6 +692,23 @@ class SimpleTestCase(unittest.TestCase):
self.fail("Second argument is not valid JSON: %r" % expected_data)
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):
"""
Asserts that two XML snippets are semantically the same.
diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt
index 35a2534792..9c8bb0c03e 100644
--- a/docs/releases/1.8.txt
+++ b/docs/releases/1.8.txt
@@ -155,6 +155,9 @@ Tests
:meth:`~django.test.SimpleTestCase.assertTemplateUsed`. This allows you to
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
^^^^^^^^^^
diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt
index 29a94b713c..188da1bbaf 100644
--- a/docs/topics/testing/tools.txt
+++ b/docs/topics/testing/tools.txt
@@ -1458,6 +1458,13 @@ your test suite.
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)
Asserts that a queryset ``qs`` returns a particular list of values ``values``.
diff --git a/tests/test_utils/tests.py b/tests/test_utils/tests.py
index e9f6da7279..1465c2be71 100644
--- a/tests/test_utils/tests.py
+++ b/tests/test_utils/tests.py
@@ -542,6 +542,51 @@ class HTMLEqualTests(TestCase):
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):
def test_simple_equal(self):
xml1 = "<elem attr1='a' attr2='b' />"