14
14
15
15
from jsonschema import (
16
16
FormatChecker ,
17
- SchemaError ,
18
- ValidationError ,
19
17
TypeChecker ,
20
18
_types ,
19
+ exceptions ,
21
20
validators ,
22
21
)
23
22
from jsonschema .compat import PY3 , pathname2url
24
23
25
24
26
25
def startswith (validator , startswith , instance , schema ):
27
26
if not instance .startswith (startswith ):
28
- yield ValidationError (u"Whoops!" )
27
+ yield exceptions . ValidationError (u"Whoops!" )
29
28
30
29
31
30
class TestCreateAndExtend (SynchronousTestCase ):
@@ -75,7 +74,7 @@ def test_iter_errors(self):
75
74
errors = list (iter_errors (u"hello" ))
76
75
self .assertEqual (errors , [])
77
76
78
- expected_error = ValidationError (
77
+ expected_error = exceptions . ValidationError (
79
78
u"Whoops!" ,
80
79
instance = u"goodbye" ,
81
80
schema = schema ,
@@ -339,7 +338,7 @@ def test_iter_errors_multiple_failures_one_validator(self):
339
338
class TestValidationErrorMessages (TestCase ):
340
339
def message_for (self , instance , schema , * args , ** kwargs ):
341
340
kwargs .setdefault ("cls" , validators .Draft3Validator )
342
- with self .assertRaises (ValidationError ) as e :
341
+ with self .assertRaises (exceptions . ValidationError ) as e :
343
342
validators .validate (instance , schema , * args , ** kwargs )
344
343
return e .exception .message
345
344
@@ -887,11 +886,11 @@ def test_additionalItems_with_items(self):
887
886
class MetaSchemaTestsMixin (object ):
888
887
# TODO: These all belong upstream
889
888
def test_invalid_properties (self ):
890
- with self .assertRaises (SchemaError ):
889
+ with self .assertRaises (exceptions . SchemaError ):
891
890
self .Validator .check_schema ({"properties" : {"test" : object ()}})
892
891
893
892
def test_minItems_invalid_string (self ):
894
- with self .assertRaises (SchemaError ):
893
+ with self .assertRaises (exceptions . SchemaError ):
895
894
# needs to be an integer
896
895
self .Validator .check_schema ({"minItems" : "1" })
897
896
@@ -919,7 +918,7 @@ def test_it_delegates_to_a_ref_resolver(self):
919
918
resolver = validators .RefResolver ("" , {}, store = {ref : schema })
920
919
validator = self .Validator ({"$ref" : ref }, resolver = resolver )
921
920
922
- with self .assertRaises (ValidationError ):
921
+ with self .assertRaises (exceptions . ValidationError ):
923
922
validator .validate (None )
924
923
925
924
def test_it_delegates_to_a_legacy_ref_resolver (self ):
@@ -938,7 +937,7 @@ def resolving(this, ref):
938
937
resolver = LegacyRefResolver ()
939
938
schema = {"$ref" : "the ref" }
940
939
941
- with self .assertRaises (ValidationError ):
940
+ with self .assertRaises (exceptions . ValidationError ):
942
941
self .Validator (schema , resolver = resolver ).validate (None )
943
942
944
943
def test_is_type_is_true_for_valid_type (self ):
@@ -1004,7 +1003,7 @@ def check(value):
1004
1003
)
1005
1004
1006
1005
validator .validate ("good" )
1007
- with self .assertRaises (ValidationError ) as cm :
1006
+ with self .assertRaises (exceptions . ValidationError ) as cm :
1008
1007
validator .validate ("bad" )
1009
1008
1010
1009
# Make sure original cause is attached
@@ -1035,7 +1034,7 @@ def test_it_properly_formats_tuples_in_errors(self):
1035
1034
lambda checker , thing : isinstance (thing , tuple ),
1036
1035
)
1037
1036
)
1038
- with self .assertRaises (ValidationError ) as e :
1037
+ with self .assertRaises (exceptions . ValidationError ) as e :
1039
1038
TupleValidator ({"uniqueItems" : True }).validate ((1 , 1 ))
1040
1039
self .assertIn ("(1, 1) has non-unique elements" , str (e .exception ))
1041
1040
@@ -1046,12 +1045,12 @@ class AntiDraft6LeakMixin(object):
1046
1045
"""
1047
1046
1048
1047
def test_True_is_not_a_schema (self ):
1049
- with self .assertRaises (SchemaError ) as e :
1048
+ with self .assertRaises (exceptions . SchemaError ) as e :
1050
1049
self .Validator .check_schema (True )
1051
1050
self .assertIn ("True is not of type" , str (e .exception ))
1052
1051
1053
1052
def test_False_is_not_a_schema (self ):
1054
- with self .assertRaises (SchemaError ) as e :
1053
+ with self .assertRaises (exceptions . SchemaError ) as e :
1055
1054
self .Validator .check_schema (False )
1056
1055
self .assertIn ("False is not of type" , str (e .exception ))
1057
1056
@@ -1089,7 +1088,7 @@ def test_any_type_is_redefinable(self):
1089
1088
)
1090
1089
validator = Crazy ({"type" : "any" })
1091
1090
validator .validate (12 )
1092
- with self .assertRaises (ValidationError ):
1091
+ with self .assertRaises (exceptions . ValidationError ):
1093
1092
validator .validate ("foo" )
1094
1093
1095
1094
def test_is_type_is_true_for_any_type (self ):
@@ -1310,15 +1309,15 @@ def test_draft7_validator_is_the_default(self):
1310
1309
self .assertUses (schema = {}, Validator = validators .Draft7Validator )
1311
1310
1312
1311
def test_validation_error_message (self ):
1313
- with self .assertRaises (ValidationError ) as e :
1312
+ with self .assertRaises (exceptions . ValidationError ) as e :
1314
1313
validators .validate (12 , {"type" : "string" })
1315
1314
self .assertRegexpMatches (
1316
1315
str (e .exception ),
1317
1316
"(?s)Failed validating u?'.*' in schema.*On instance" ,
1318
1317
)
1319
1318
1320
1319
def test_schema_error_message (self ):
1321
- with self .assertRaises (SchemaError ) as e :
1320
+ with self .assertRaises (exceptions . SchemaError ) as e :
1322
1321
validators .validate (12 , {"type" : 12 })
1323
1322
self .assertRegexpMatches (
1324
1323
str (e .exception ),
@@ -1499,15 +1498,15 @@ def handler(url):
1499
1498
1500
1499
ref = "foo://bar"
1501
1500
resolver = validators .RefResolver ("" , {}, handlers = {"foo" : handler })
1502
- with self .assertRaises (validators .RefResolutionError ) as err :
1501
+ with self .assertRaises (exceptions .RefResolutionError ) as err :
1503
1502
with resolver .resolving (ref ):
1504
1503
self .fail ("Shouldn't get this far!" ) # pragma: no cover
1505
- self .assertEqual (err .exception , validators .RefResolutionError (error ))
1504
+ self .assertEqual (err .exception , exceptions .RefResolutionError (error ))
1506
1505
1507
1506
def test_helpful_error_message_on_failed_pop_scope (self ):
1508
1507
resolver = validators .RefResolver ("" , {})
1509
1508
resolver .pop_scope ()
1510
- with self .assertRaises (validators .RefResolutionError ) as exc :
1509
+ with self .assertRaises (exceptions .RefResolutionError ) as exc :
1511
1510
resolver .pop_scope ()
1512
1511
self .assertIn ("Failed to pop the scope" , str (exc .exception ))
1513
1512
0 commit comments