From 202157c77ba1110c51813f3ac0ea878d97ce164b Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Tue, 6 May 2014 19:50:49 -0400 Subject: [PATCH] Move this test case where it belongs. --- jsonschema/tests/test_exceptions.py | 122 ++++++++++++++++++++++++---- jsonschema/tests/test_validators.py | 92 --------------------- 2 files changed, 107 insertions(+), 107 deletions(-) diff --git a/jsonschema/tests/test_exceptions.py b/jsonschema/tests/test_exceptions.py index bbd562a..9e5793c 100644 --- a/jsonschema/tests/test_exceptions.py +++ b/jsonschema/tests/test_exceptions.py @@ -1,4 +1,7 @@ +import textwrap + from jsonschema import Draft4Validator, exceptions +from jsonschema.compat import PY3 from jsonschema.tests.compat import mock, unittest @@ -270,21 +273,110 @@ class TestErrorTree(unittest.TestCase): self.assertIsInstance(tree["foo"], exceptions.ErrorTree) - def test_str_works_with_instances_having_overriden_eq_operator(self): - """ - Checks for https://github.com/Julian/jsonschema/issues/164 which - rendered exceptions unusable when a `ValidationError` involved classes - withthe `eq` operator overridden (such as pandas.DataFrame), - caused by a `XX in YYY` check within `__unicode__`() method. - """ +class TestErrorReprStr(unittest.TestCase): + def make_error(self, **kwargs): + defaults = dict( + message=u"hello", + validator=u"type", + validator_value=u"string", + instance=5, + schema={u"type": u"string"}, + ) + defaults.update(kwargs) + return exceptions.ValidationError(**defaults) - class InstanceWithOverridenEq(object): - def __eq__(self, other): - raise Exception("Instance's __eq__()hould not have been called!") - inst = InstanceWithOverridenEq() - error = exceptions.ValidationError( - "a message", validator="foo", instance=inst, validator_value='some', schema='schema', + def assertShows(self, expected, **kwargs): + if PY3: + expected = expected.replace("u'", "'") + expected = textwrap.dedent(expected).rstrip("\n") + + error = self.make_error(**kwargs) + message_line, _, rest = str(error).partition("\n") + self.assertEqual(message_line, error.message) + self.assertEqual(rest, expected) + + def test_repr(self): + self.assertEqual( + repr(exceptions.ValidationError(message="Hello!")), + "" % "Hello!", ) - ex_str = str(error) - self.assertTrue(str(exceptions).find(type(inst).__name__), ex_str) + def test_unset_error(self): + error = exceptions.ValidationError("message") + self.assertEqual(str(error), "message") + + kwargs = { + "validator": "type", + "validator_value": "string", + "instance": 5, + "schema": {"type": "string"} + } + # Just the message should show if any of the attributes are unset + for attr in kwargs: + k = dict(kwargs) + del k[attr] + error = exceptions.ValidationError("message", **k) + self.assertEqual(str(error), "message") + + def test_empty_paths(self): + self.assertShows( + """ + Failed validating u'type' in schema: + {u'type': u'string'} + + On instance: + 5 + """, + path=[], + schema_path=[], + ) + + def test_one_item_paths(self): + self.assertShows( + """ + Failed validating u'type' in schema: + {u'type': u'string'} + + On instance[0]: + 5 + """, + path=[0], + schema_path=["items"], + ) + + def test_multiple_item_paths(self): + self.assertShows( + """ + Failed validating u'type' in schema[u'items'][0]: + {u'type': u'string'} + + On instance[0][u'a']: + 5 + """, + path=[0, u"a"], + schema_path=[u"items", 0, 1], + ) + + def test_uses_pprint(self): + with mock.patch("pprint.pformat") as pformat: + str(self.make_error()) + self.assertEqual(pformat.call_count, 2) # schema + instance + + def test_str_works_with_instances_having_overriden_eq_operator(self): + """ + Check for https://github.com/Julian/jsonschema/issues/164 which + rendered exceptions unusable when a `ValidationError` involved + instances with an `__eq__` method that returned truthy values. + + """ + + instance = mock.MagicMock() + error = exceptions.ValidationError( + "a message", + validator="foo", + instance=instance, + validator_value="some", + schema="schema", + ) + str(error) + self.assertFalse(instance.__eq__.called) diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 7c0bc60..f869238 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1,10 +1,8 @@ from collections import deque from contextlib import contextmanager import json -import textwrap from jsonschema import FormatChecker, ValidationError -from jsonschema.compat import PY3 from jsonschema.tests.compat import mock, unittest from jsonschema.validators import ( RefResolutionError, UnknownType, Draft3Validator, @@ -194,96 +192,6 @@ class TestValidationErrorMessages(unittest.TestCase): self.assertIn("is not a", message) -class TestErrorReprStr(unittest.TestCase): - def make_error(self, **kwargs): - defaults = dict( - message=u"hello", - validator=u"type", - validator_value=u"string", - instance=5, - schema={u"type": u"string"}, - ) - defaults.update(kwargs) - return ValidationError(**defaults) - - def assertShows(self, expected, **kwargs): - if PY3: - expected = expected.replace("u'", "'") - expected = textwrap.dedent(expected).rstrip("\n") - - error = self.make_error(**kwargs) - message_line, _, rest = str(error).partition("\n") - self.assertEqual(message_line, error.message) - self.assertEqual(rest, expected) - - def test_repr(self): - self.assertEqual( - repr(ValidationError(message="Hello!")), - "" % "Hello!", - ) - - def test_unset_error(self): - error = ValidationError("message") - self.assertEqual(str(error), "message") - - kwargs = { - "validator": "type", - "validator_value": "string", - "instance": 5, - "schema": {"type": "string"} - } - # Just the message should show if any of the attributes are unset - for attr in kwargs: - k = dict(kwargs) - del k[attr] - error = ValidationError("message", **k) - self.assertEqual(str(error), "message") - - def test_empty_paths(self): - self.assertShows( - """ - Failed validating u'type' in schema: - {u'type': u'string'} - - On instance: - 5 - """, - path=[], - schema_path=[], - ) - - def test_one_item_paths(self): - self.assertShows( - """ - Failed validating u'type' in schema: - {u'type': u'string'} - - On instance[0]: - 5 - """, - path=[0], - schema_path=["items"], - ) - - def test_multiple_item_paths(self): - self.assertShows( - """ - Failed validating u'type' in schema[u'items'][0]: - {u'type': u'string'} - - On instance[0][u'a']: - 5 - """, - path=[0, u"a"], - schema_path=[u"items", 0, 1], - ) - - def test_uses_pprint(self): - with mock.patch("pprint.pformat") as pformat: - str(self.make_error()) - self.assertEqual(pformat.call_count, 2) # schema + instance - - class TestValidationErrorDetails(unittest.TestCase): # TODO: These really need unit tests for each individual validator, rather # than just these higher level tests.