Skip to content

Commit fe7cec1

Browse files
committed
Fix up the tests for error strings
1 parent a6e0bfc commit fe7cec1

File tree

1 file changed

+77
-101
lines changed

1 file changed

+77
-101
lines changed

tests.py

Lines changed: 77 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -363,84 +363,85 @@ def test_repr(self):
363363

364364

365365
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)
366+
def make_error(self, **kwargs):
367+
# Creates an error with all the required attributes set
368+
default = {
369+
"message": "message",
370+
"validator": "type",
371+
"validator_value": "string",
372+
"instance": 5,
373+
"schema": {"type": "string"}
374+
}
375+
default.update(kwargs)
376+
return ValidationError(**default)
376377

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)
378+
def prep_message(self, message):
379+
# Strips leading whitespace, dedents, replaces u' with ' on py3
380+
if PY3:
381+
message = message.replace("u'", "'")
382+
return textwrap.dedent(message).lstrip()
383+
384+
def test_unset_error(self):
385+
error = ValidationError("message")
386+
self.assertEqual(str(error), "message")
387+
kwargs = {
388+
"validator": "type",
389+
"validator_value": "string",
390+
"instance": 5,
391+
"schema": {"type": "string"}
392+
}
393+
# Just the message should show if any of the attributes are unset
394+
for attr in kwargs:
395+
k = dict(kwargs)
396+
del k[attr]
397+
error = ValidationError("message", **k)
398+
self.assertEqual(str(error), "message")
387399

388400
def test_empty_paths(self):
389-
error = ValidationError("message", path=[], schema_path=[])
390-
message = textwrap.dedent("""\
401+
error = self.make_error(path=[], schema_path=[])
402+
message = self.prep_message("""
391403
ValidationError: message
392-
Failed validating '<unset>' in schema:
393-
<unset>
404+
Failed validating 'type' in schema:
405+
{u'type': u'string'}
394406
On instance:
395-
<unset>
407+
5
396408
""")
397409
self.assertEqual(str(error), message)
398410

399411
def test_one_item_paths(self):
400-
error = ValidationError("message", path=[0], schema_path=["items"])
401-
message = textwrap.dedent("""\
412+
error = self.make_error(path=[0], schema_path=["items"])
413+
message = self.prep_message("""
402414
ValidationError: message
403-
Failed validating '<unset>' in schema:
404-
<unset>
415+
Failed validating 'type' in schema:
416+
{u'type': u'string'}
405417
On instance[0]:
406-
<unset>
418+
5
407419
""")
408420
self.assertEqual(str(error), message)
409421

410422
def test_two_item_paths(self):
411-
error = ValidationError(
412-
"message", path=[0, 1], schema_path=["items", "additionalItems"]
423+
error = self.make_error(
424+
path=[0, 1], schema_path=["items", "additionalItems"]
413425
)
414-
message = textwrap.dedent("""\
426+
message = self.prep_message("""
415427
ValidationError: message
416-
Failed validating '<unset>' in schema['items']:
417-
<unset>
428+
Failed validating 'type' in schema[u'items']:
429+
{u'type': u'string'}
418430
On instance[0][1]:
419-
<unset>
431+
5
420432
""")
421433
self.assertEqual(str(error), message)
422434

423435
def test_mixed_type_paths(self):
424-
error = ValidationError(
425-
"message", path=["a", 1, "b"], schema_path=["anyOf", 5, "type"]
436+
error = self.make_error(
437+
path=["a", 1, "b"], schema_path=["anyOf", 5, "type"]
426438
)
427-
message = textwrap.dedent("""\
439+
message = self.prep_message("""
428440
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>
441+
Failed validating 'type' in schema[u'anyOf'][5]:
442+
{u'type': u'string'}
443+
On instance[u'a'][1][u'b']:
444+
5
444445
""")
445446
self.assertEqual(str(error), message)
446447

@@ -456,29 +457,18 @@ def test_pprint_schema(self):
456457
]
457458
}
458459
}
459-
error = ValidationError("message", schema=schema)
460-
message = textwrap.dedent("""\
460+
error = self.make_error(schema=schema)
461+
message = self.prep_message("""
461462
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']}
463+
Failed validating 'type' in schema:
464+
{u'items': {u'allOf': [{u'maxLength': 2, u'type': u'string'},
465+
{u'minimum': 5, u'type': u'integer'},
466+
{u'items': [{u'type': u'string'},
467+
{u'type': u'integer'}],
468+
u'type': u'array'}]},
469+
u'type': [u'string']}
469470
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}]
471+
5
482472
""")
483473
self.assertEqual(str(error), message)
484474

@@ -494,37 +484,23 @@ def test_pprint_instance(self):
494484
]
495485
}
496486
}
497-
error = ValidationError("message", instance=instance)
498-
message = textwrap.dedent("""\
487+
error = self.make_error(instance=instance)
488+
message = self.prep_message("""
499489
ValidationError: message
500-
Failed validating '<unset>' in schema:
501-
<unset>
490+
Failed validating 'type' in schema:
491+
{u'type': u'string'}
502492
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']}
493+
{u'items': {u'allOf': [{u'maxLength': 2, u'type': u'string'},
494+
{u'minimum': 5, u'type': u'integer'},
495+
{u'items': [{u'type': u'string'},
496+
{u'type': u'integer'}],
497+
u'type': u'array'}]},
498+
u'type': [u'string']}
509499
""")
500+
print(error)
501+
print(message)
510502
self.assertEqual(str(error), message)
511503

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-
528504

529505
class TestValidationErrorDetails(unittest.TestCase):
530506
def setUp(self):

0 commit comments

Comments
 (0)