Skip to content

Commit fc4f687

Browse files
committed
Add some tests for the new error string representations
1 parent 1e73d97 commit fc4f687

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

tests.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import subprocess
1010
import sys
11+
import textwrap
1112

1213
if sys.version_info[:2] < (2, 7): # pragma: no cover
1314
import unittest2 as unittest
@@ -361,6 +362,169 @@ def test_repr(self):
361362
self.assertEqual(repr(error), "<ValidationError: %r>" % message)
362363

363364

365+
class TestErrorStr(unittest.TestCase):
366+
def test_just_message(self):
367+
error = ValidationError("message")
368+
message = textwrap.dedent("""\
369+
ValidationError: message
370+
Failed validating '<unset>' in schema:
371+
<unset>
372+
On instance:
373+
<unset>
374+
""")
375+
self.assertEqual(str(error), message)
376+
377+
def test_just_message(self):
378+
error = ValidationError("message", validator="additionalProperties")
379+
message = textwrap.dedent("""\
380+
ValidationError: message
381+
Failed validating 'additionalProperties' in schema:
382+
<unset>
383+
On instance:
384+
<unset>
385+
""")
386+
self.assertEqual(str(error), message)
387+
388+
def test_empty_paths(self):
389+
error = ValidationError("message", path=[], schema_path=[])
390+
message = textwrap.dedent("""\
391+
ValidationError: message
392+
Failed validating '<unset>' in schema:
393+
<unset>
394+
On instance:
395+
<unset>
396+
""")
397+
self.assertEqual(str(error), message)
398+
399+
def test_one_item_paths(self):
400+
error = ValidationError("message", path=[0], schema_path=["items"])
401+
message = textwrap.dedent("""\
402+
ValidationError: message
403+
Failed validating '<unset>' in schema:
404+
<unset>
405+
On instance[0]:
406+
<unset>
407+
""")
408+
self.assertEqual(str(error), message)
409+
410+
def test_two_item_paths(self):
411+
error = ValidationError(
412+
"message", path=[0, 1], schema_path=["items", "additionalItems"]
413+
)
414+
message = textwrap.dedent("""\
415+
ValidationError: message
416+
Failed validating '<unset>' in schema['items']:
417+
<unset>
418+
On instance[0][1]:
419+
<unset>
420+
""")
421+
self.assertEqual(str(error), message)
422+
423+
def test_mixed_type_paths(self):
424+
error = ValidationError(
425+
"message", path=["a", 1, "b"], schema_path=["anyOf", 5, "type"]
426+
)
427+
message = textwrap.dedent("""\
428+
ValidationError: message
429+
Failed validating '<unset>' in schema['anyOf'][5]:
430+
<unset>
431+
On instance['a'][1]['b']:
432+
<unset>
433+
""")
434+
self.assertEqual(str(error), message)
435+
436+
def test_simple_schema(self):
437+
error = ValidationError("message", schema={"type": "string"})
438+
message = textwrap.dedent("""\
439+
ValidationError: message
440+
Failed validating '<unset>' in schema:
441+
{'type': 'string'}
442+
On instance:
443+
<unset>
444+
""")
445+
self.assertEqual(str(error), message)
446+
447+
def test_pprint_schema(self):
448+
schema = {
449+
"type": ["string"],
450+
"items": {
451+
"allOf": [
452+
{"type": "string", "maxLength": 2},
453+
{"type": "integer", "minimum": 5},
454+
{"items": [{"type": "string"}, {"type": "integer"}],
455+
"type": "array"}
456+
]
457+
}
458+
}
459+
error = ValidationError("message", schema=schema)
460+
message = textwrap.dedent("""\
461+
ValidationError: message
462+
Failed validating '<unset>' in schema:
463+
{'items': {'allOf': [{'maxLength': 2, 'type': 'string'},
464+
{'minimum': 5, 'type': 'integer'},
465+
{'items': [{'type': 'string'},
466+
{'type': 'integer'}],
467+
'type': 'array'}]},
468+
'type': ['string']}
469+
On instance:
470+
<unset>
471+
""")
472+
self.assertEqual(str(error), message)
473+
474+
def test_simple_instance(self):
475+
error = ValidationError("message", instance=["foo", {"bar": 42}])
476+
message = textwrap.dedent("""\
477+
ValidationError: message
478+
Failed validating '<unset>' in schema:
479+
<unset>
480+
On instance:
481+
['foo', {'bar': 42}]
482+
""")
483+
self.assertEqual(str(error), message)
484+
485+
def test_pprint_instance(self):
486+
instance = {
487+
"type": ["string"],
488+
"items": {
489+
"allOf": [
490+
{"type": "string", "maxLength": 2},
491+
{"type": "integer", "minimum": 5},
492+
{"items": [{"type": "string"}, {"type": "integer"}],
493+
"type": "array"}
494+
]
495+
}
496+
}
497+
error = ValidationError("message", instance=instance)
498+
message = textwrap.dedent("""\
499+
ValidationError: message
500+
Failed validating '<unset>' in schema:
501+
<unset>
502+
On instance:
503+
{'items': {'allOf': [{'maxLength': 2, 'type': 'string'},
504+
{'minimum': 5, 'type': 'integer'},
505+
{'items': [{'type': 'string'},
506+
{'type': 'integer'}],
507+
'type': 'array'}]},
508+
'type': ['string']}
509+
""")
510+
self.assertEqual(str(error), message)
511+
512+
def test_all_values(self):
513+
error = ValidationError(
514+
"the message", validator="type", path=[3, 0], cause=Exception(),
515+
context=[ValidationError("b")], validator_value="string",
516+
instance=3, schema={"type": "string"},
517+
schema_path=["items", "allOf", 2, "type"]
518+
)
519+
message = textwrap.dedent("""\
520+
ValidationError: the message
521+
Failed validating 'type' in schema['items']['allOf'][2]:
522+
{'type': 'string'}
523+
On instance[3][0]:
524+
3
525+
""")
526+
assert str(error) == message
527+
364528

365529
class TestValidationErrorDetails(unittest.TestCase):
366530
def setUp(self):

0 commit comments

Comments
 (0)