def test_remove(self): self.assertEqual( TypeChecker({ "two": equals_2 }).remove("two"), TypeChecker(), )
def test_remove_multiple(self): self.assertEqual( TypeChecker({ "foo": int, "bar": str }).remove("foo", "bar"), TypeChecker(), )
def test_redefine_many(self): self.assertEqual( TypeChecker().redefine_many({ "foo": int, "bar": str }), TypeChecker().redefine("foo", int).redefine("bar", str), )
def test_is_type(self): checker = TypeChecker({"two": equals_2}) self.assertEqual( ( checker.is_type(instance=2, type="two"), checker.is_type(instance="bar", type="two"), ), (True, False), )
def test_is_unknown_type(self): with self.assertRaises(UndefinedTypeCheck) as e: TypeChecker().is_type(4, "foobar") self.assertIn( "'foobar' is unknown to this type checker", str(e.exception), ) self.assertTrue( e.exception.__suppress_context__, msg="Expected the internal KeyError to be hidden.", )
def test_type_check_can_raise_key_error(self): """ Make sure no one writes: try: self._type_checkers[type](...) except KeyError: ignoring the fact that the function itself can raise that. """ error = KeyError("Stuff") def raises_keyerror(checker, instance): raise error with self.assertRaises(KeyError) as context: TypeChecker({"foo": raises_keyerror}).is_type(4, "foo") self.assertIs(context.exception, error)
def test_remove_unknown_type(self): with self.assertRaises(UndefinedTypeCheck) as context: TypeChecker().remove("foobar") self.assertIn("foobar", str(context.exception))
def test_redefine_existing_type(self): self.assertEqual( TypeChecker().redefine("two", object()).redefine("two", equals_2), TypeChecker().redefine("two", equals_2), )
def test_checks_can_be_added_at_init(self): checker = TypeChecker({"two": equals_2}) self.assertEqual(checker, TypeChecker().redefine("two", equals_2))
from jsonschema._types import ( TypeChecker, is_any, is_array, is_bool, is_integer, is_object, is_number, ) from six import text_type, binary_type def is_string(checker, instance): return isinstance(instance, (text_type, binary_type)) oas30_type_checker = TypeChecker( { u"string": is_string, u"number": is_number, u"integer": is_integer, u"boolean": is_bool, u"array": is_array, u"object": is_object, }, )