예제 #1
0
 def test_if_you_give_it_junk_you_get_a_resolution_error(self):
     ref = "foo://bar"
     foo_handler = mock.Mock(side_effect=ValueError("Oh no! What's this?"))
     resolver = RefResolver("", {}, handlers={"foo": foo_handler})
     with self.assertRaises(RefResolutionError) as err:
         with resolver.resolving(ref):
             pass
     self.assertEqual(str(err.exception), "Oh no! What's this?")
예제 #2
0
 def test_custom_uri_scheme_handlers(self):
     schema = {"foo": "bar"}
     ref = "foo://bar"
     foo_handler = mock.Mock(return_value=schema)
     resolver = RefResolver("", {}, handlers={"foo": foo_handler})
     with resolver.resolving(ref) as resolved:
         self.assertEqual(resolved, schema)
     foo_handler.assert_called_once_with(ref)
예제 #3
0
    def test_invalid_instances_are_not_valid(self):
        errors = iter([mock.Mock()])

        with mock.patch.object(
                self.validator,
                "iter_errors",
                return_value=errors,
        ):
            self.assertFalse(
                self.validator.is_valid(self.instance, self.schema))
예제 #4
0
    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)
예제 #5
0
    def test_extend(self):
        validators = dict(self.Validator.VALIDATORS)
        new = mock.Mock()

        Extended = extend(self.Validator, validators={"a new one": new})

        validators.update([("a new one", new)])
        self.assertEqual(Extended.VALIDATORS, validators)
        self.assertNotIn("a new one", self.Validator.VALIDATORS)

        self.assertEqual(Extended.META_SCHEMA, self.Validator.META_SCHEMA)
        self.assertEqual(Extended.DEFAULT_TYPES, self.Validator.DEFAULT_TYPES)
예제 #6
0
 def test_cache_remote_off(self):
     ref = "foo://bar"
     foo_handler = mock.Mock()
     resolver = RefResolver(
         "",
         {},
         cache_remote=False,
         handlers={"foo": foo_handler},
     )
     with resolver.resolving(ref):
         pass
     with resolver.resolving(ref):
         pass
     self.assertEqual(foo_handler.call_count, 2)
예제 #7
0
 def test_cache_remote_on(self):
     ref = "foo://bar"
     foo_handler = mock.Mock()
     resolver = RefResolver(
         "",
         {},
         cache_remote=True,
         handlers={"foo": foo_handler},
     )
     with resolver.resolving(ref):
         pass
     with resolver.resolving(ref):
         pass
     foo_handler.assert_called_once_with(ref)
예제 #8
0
    def test_it_delegates_to_a_ref_resolver(self):
        resolver = RefResolver("", {})
        schema = {"$ref": mock.Mock()}

        @contextlib.contextmanager
        def resolving():
            yield {"type": "integer"}

        with mock.patch.object(resolver, "resolving") as resolve:
            resolve.return_value = resolving()
            with self.assertRaises(ValidationError):
                self.validator_class(schema, resolver=resolver).validate(None)

        resolve.assert_called_once_with(schema["$ref"])
예제 #9
0
    def test_iter_errors(self):
        instance = "hello"

        self.smelly.return_value = []
        self.assertEqual(list(self.validator.iter_errors(instance)), [])

        error = mock.Mock()
        self.smelly.return_value = [error]
        self.assertEqual(list(self.validator.iter_errors(instance)), [error])

        self.smelly.assert_called_with(
            self.validator,
            self.validator_value,
            instance,
            self.schema,
        )
예제 #10
0
    def test_it_validates_formats_if_a_checker_is_provided(self):
        checker = mock.Mock(spec=FormatChecker)
        validator = self.validator_class(
            {"format": "foo"},
            format_checker=checker,
        )

        validator.validate("bar")

        checker.check.assert_called_once_with("bar", "foo")

        cause = ValueError()
        checker.check.side_effect = FormatError('aoeu', cause=cause)

        with self.assertRaises(ValidationError) as cm:
            validator.validate("bar")
        # Make sure original cause is attached
        self.assertIs(cm.exception.cause, cause)
예제 #11
0
 def setUp(self):
     self.fn = mock.Mock()
예제 #12
0
 def resolve(self, reference):
     _, _, reference = reference.partition("http://localhost:1234/")
     return mock.Mock(**{"json.return_value": REMOTES.get(reference)})
예제 #13
0
 def test_any_type_is_valid_for_type_any(self):
     validator = self.validator_class({"type": "any"})
     validator.validate(mock.Mock())
예제 #14
0
 def test_is_type_is_true_for_any_type(self):
     self.assertTrue(self.validator.is_valid(mock.Mock(), {"type": "any"}))
예제 #15
0
 def test_non_existent_properties_are_ignored(self):
     instance, my_property, my_value = mock.Mock(), mock.Mock(), mock.Mock()
     validate(instance=instance, schema={my_property: my_value})
예제 #16
0
 def setUp(self):
     self.instance = mock.Mock()
     self.schema = {}
     self.resolver = mock.Mock()
     self.validator = self.validator_class(self.schema)