Skip to content

Commit 202157c

Browse files
committed
Move this test case where it belongs.
1 parent 7cebc7b commit 202157c

File tree

2 files changed

+104
-104
lines changed

2 files changed

+104
-104
lines changed

jsonschema/tests/test_exceptions.py

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import textwrap
2+
13
from jsonschema import Draft4Validator, exceptions
4+
from jsonschema.compat import PY3
25
from jsonschema.tests.compat import mock, unittest
36

47

@@ -270,21 +273,110 @@ def test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error(self):
270273
self.assertIsInstance(tree["foo"], exceptions.ErrorTree)
271274

272275

276+
class TestErrorReprStr(unittest.TestCase):
277+
def make_error(self, **kwargs):
278+
defaults = dict(
279+
message=u"hello",
280+
validator=u"type",
281+
validator_value=u"string",
282+
instance=5,
283+
schema={u"type": u"string"},
284+
)
285+
defaults.update(kwargs)
286+
return exceptions.ValidationError(**defaults)
287+
288+
def assertShows(self, expected, **kwargs):
289+
if PY3:
290+
expected = expected.replace("u'", "'")
291+
expected = textwrap.dedent(expected).rstrip("\n")
292+
293+
error = self.make_error(**kwargs)
294+
message_line, _, rest = str(error).partition("\n")
295+
self.assertEqual(message_line, error.message)
296+
self.assertEqual(rest, expected)
297+
298+
def test_repr(self):
299+
self.assertEqual(
300+
repr(exceptions.ValidationError(message="Hello!")),
301+
"<ValidationError: %r>" % "Hello!",
302+
)
303+
304+
def test_unset_error(self):
305+
error = exceptions.ValidationError("message")
306+
self.assertEqual(str(error), "message")
307+
308+
kwargs = {
309+
"validator": "type",
310+
"validator_value": "string",
311+
"instance": 5,
312+
"schema": {"type": "string"}
313+
}
314+
# Just the message should show if any of the attributes are unset
315+
for attr in kwargs:
316+
k = dict(kwargs)
317+
del k[attr]
318+
error = exceptions.ValidationError("message", **k)
319+
self.assertEqual(str(error), "message")
320+
321+
def test_empty_paths(self):
322+
self.assertShows(
323+
"""
324+
Failed validating u'type' in schema:
325+
{u'type': u'string'}
326+
327+
On instance:
328+
5
329+
""",
330+
path=[],
331+
schema_path=[],
332+
)
333+
334+
def test_one_item_paths(self):
335+
self.assertShows(
336+
"""
337+
Failed validating u'type' in schema:
338+
{u'type': u'string'}
339+
340+
On instance[0]:
341+
5
342+
""",
343+
path=[0],
344+
schema_path=["items"],
345+
)
346+
347+
def test_multiple_item_paths(self):
348+
self.assertShows(
349+
"""
350+
Failed validating u'type' in schema[u'items'][0]:
351+
{u'type': u'string'}
352+
353+
On instance[0][u'a']:
354+
5
355+
""",
356+
path=[0, u"a"],
357+
schema_path=[u"items", 0, 1],
358+
)
359+
360+
def test_uses_pprint(self):
361+
with mock.patch("pprint.pformat") as pformat:
362+
str(self.make_error())
363+
self.assertEqual(pformat.call_count, 2) # schema + instance
364+
273365
def test_str_works_with_instances_having_overriden_eq_operator(self):
274366
"""
275-
Checks for https://github.com/Julian/jsonschema/issues/164 which
276-
rendered exceptions unusable when a `ValidationError` involved classes
277-
withthe `eq` operator overridden (such as pandas.DataFrame),
278-
caused by a `XX in YYY` check within `__unicode__`() method.
367+
Check for https://github.com/Julian/jsonschema/issues/164 which
368+
rendered exceptions unusable when a `ValidationError` involved
369+
instances with an `__eq__` method that returned truthy values.
370+
279371
"""
280372

281-
class InstanceWithOverridenEq(object):
282-
def __eq__(self, other):
283-
raise Exception("Instance's __eq__()hould not have been called!")
284-
inst = InstanceWithOverridenEq()
373+
instance = mock.MagicMock()
285374
error = exceptions.ValidationError(
286-
"a message", validator="foo", instance=inst, validator_value='some', schema='schema',
375+
"a message",
376+
validator="foo",
377+
instance=instance,
378+
validator_value="some",
379+
schema="schema",
287380
)
288-
289-
ex_str = str(error)
290-
self.assertTrue(str(exceptions).find(type(inst).__name__), ex_str)
381+
str(error)
382+
self.assertFalse(instance.__eq__.called)

jsonschema/tests/test_validators.py

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from collections import deque
22
from contextlib import contextmanager
33
import json
4-
import textwrap
54

65
from jsonschema import FormatChecker, ValidationError
7-
from jsonschema.compat import PY3
86
from jsonschema.tests.compat import mock, unittest
97
from jsonschema.validators import (
108
RefResolutionError, UnknownType, Draft3Validator,
@@ -194,96 +192,6 @@ def test_invalid_format_default_message(self):
194192
self.assertIn("is not a", message)
195193

196194

197-
class TestErrorReprStr(unittest.TestCase):
198-
def make_error(self, **kwargs):
199-
defaults = dict(
200-
message=u"hello",
201-
validator=u"type",
202-
validator_value=u"string",
203-
instance=5,
204-
schema={u"type": u"string"},
205-
)
206-
defaults.update(kwargs)
207-
return ValidationError(**defaults)
208-
209-
def assertShows(self, expected, **kwargs):
210-
if PY3:
211-
expected = expected.replace("u'", "'")
212-
expected = textwrap.dedent(expected).rstrip("\n")
213-
214-
error = self.make_error(**kwargs)
215-
message_line, _, rest = str(error).partition("\n")
216-
self.assertEqual(message_line, error.message)
217-
self.assertEqual(rest, expected)
218-
219-
def test_repr(self):
220-
self.assertEqual(
221-
repr(ValidationError(message="Hello!")),
222-
"<ValidationError: %r>" % "Hello!",
223-
)
224-
225-
def test_unset_error(self):
226-
error = ValidationError("message")
227-
self.assertEqual(str(error), "message")
228-
229-
kwargs = {
230-
"validator": "type",
231-
"validator_value": "string",
232-
"instance": 5,
233-
"schema": {"type": "string"}
234-
}
235-
# Just the message should show if any of the attributes are unset
236-
for attr in kwargs:
237-
k = dict(kwargs)
238-
del k[attr]
239-
error = ValidationError("message", **k)
240-
self.assertEqual(str(error), "message")
241-
242-
def test_empty_paths(self):
243-
self.assertShows(
244-
"""
245-
Failed validating u'type' in schema:
246-
{u'type': u'string'}
247-
248-
On instance:
249-
5
250-
""",
251-
path=[],
252-
schema_path=[],
253-
)
254-
255-
def test_one_item_paths(self):
256-
self.assertShows(
257-
"""
258-
Failed validating u'type' in schema:
259-
{u'type': u'string'}
260-
261-
On instance[0]:
262-
5
263-
""",
264-
path=[0],
265-
schema_path=["items"],
266-
)
267-
268-
def test_multiple_item_paths(self):
269-
self.assertShows(
270-
"""
271-
Failed validating u'type' in schema[u'items'][0]:
272-
{u'type': u'string'}
273-
274-
On instance[0][u'a']:
275-
5
276-
""",
277-
path=[0, u"a"],
278-
schema_path=[u"items", 0, 1],
279-
)
280-
281-
def test_uses_pprint(self):
282-
with mock.patch("pprint.pformat") as pformat:
283-
str(self.make_error())
284-
self.assertEqual(pformat.call_count, 2) # schema + instance
285-
286-
287195
class TestValidationErrorDetails(unittest.TestCase):
288196
# TODO: These really need unit tests for each individual validator, rather
289197
# than just these higher level tests.

0 commit comments

Comments
 (0)