def test_it_can_register_checkers(self): checker = FormatChecker() checker.checks("new")(self.fn) self.assertEqual( checker.checkers, dict(FormatChecker.checkers, new=(self.fn, ())) )
def test_format_error_causes_become_validation_error_causes(self): checker = FormatChecker() checker.checks("foo", raises=ValueError)(self.fn) cause = self.fn.side_effect = ValueError() validator = Draft4Validator({"format": "foo"}, format_checker=checker) with self.assertRaises(ValidationError) as cm: validator.validate("bar") self.assertIs(cm.exception.__cause__, cause)
def test_format_error_causes_become_validation_error_causes(self): checker = FormatChecker() checker.checks("foo", raises=ValueError)(self.fn) cause = self.fn.side_effect = ValueError() validator = Draft4Validator({"format" : "foo"}, format_checker=checker) with self.assertRaises(ValidationError) as cm: validator.validate("bar") self.assertIs(cm.exception.__cause__, cause)
def test_invalid_format_default_message(self): checker = FormatChecker(formats=()) check_fn = mock.Mock(return_value=False) checker.checks("thing")(check_fn) schema = {"format" : "thing"} message = self.message_for("bla", schema, format_checker=checker) self.assertIn(repr("bla"), message) self.assertIn(repr("thing"), message) self.assertIn("is not a", message)
def test_invalid_format_default_message(self): checker = FormatChecker(formats=()) check_fn = mock.Mock(return_value=False) checker.checks("thing")(check_fn) schema = {"format": "thing"} message = self.message_for("bla", schema, format_checker=checker) self.assertIn(repr("bla"), message) self.assertIn(repr("thing"), message) self.assertIn("is not a", message)
def test_it_catches_registered_errors(self): checker = FormatChecker() cause = self.fn.side_effect = ValueError() checker.checks("foo", raises=ValueError)(self.fn) with self.assertRaises(FormatError) as cm: checker.check("bar", "foo") self.assertIs(cm.exception.cause, cause) self.assertIs(cm.exception.__cause__, cause) # Unregistered errors should not be caught self.fn.side_effect = AttributeError with self.assertRaises(AttributeError): checker.check("bar", "foo")
def calculatedProperty(validator, linkTo, instance, schema): yield ValidationError('submission of calculatedProperty disallowed') class SchemaValidator(Draft4Validator): VALIDATORS = Draft4Validator.VALIDATORS.copy() VALIDATORS['calculatedProperty'] = calculatedProperty VALIDATORS['linkTo'] = linkTo VALIDATORS['linkFrom'] = linkFrom VALIDATORS['permission'] = permission VALIDATORS['requestMethod'] = requestMethod VALIDATORS['validators'] = validators SERVER_DEFAULTS = SERVER_DEFAULTS format_checker = FormatChecker() def load_schema(filename): if isinstance(filename, dict): schema = filename resolver = NoRemoteResolver.from_schema(schema) else: utf8 = codecs.getreader("utf-8") asset = AssetResolver(caller_package()).resolve(filename) schema = json.load(utf8(asset.stream()), object_pairs_hook=collections.OrderedDict) resolver = RefResolver('file://' + asset.abspath(), schema) schema = mixinProperties(schema, resolver) # SchemaValidator is not thread safe for now
def test_it_can_register_checkers(self): checker = FormatChecker() checker.checks("new")(self.fn) self.assertEqual(checker.checkers, dict(FormatChecker.checkers, new=(self.fn, ())))
def test_it_can_register_cls_checkers(self): with mock.patch.dict(FormatChecker.checkers, clear=True): FormatChecker.cls_checks("new")(self.fn) self.assertEqual(FormatChecker.checkers, {"new": (self.fn, ())})
def test_it_raises_a_key_error_for_unknown_formats(self): with self.assertRaises(KeyError): FormatChecker(formats=["o noes"])
def test_it_can_validate_no_formats(self): checker = FormatChecker(formats=()) self.assertFalse(checker.checkers)
def test_it_can_register_cls_checkers(self): with mock.patch.dict(FormatChecker.checkers, clear=True): FormatChecker.cls_checks("new")(self.fn) self.assertEqual(FormatChecker.checkers, {"new" : (self.fn, ())})
def test_it_returns_true_for_formats_it_does_not_know_about(self): validator = self.validator_class( {"format": "carrot"}, format_checker=FormatChecker(), ) validator.validate("bugs")