本文整理汇总了Python中jsonschema.RefResolver类的典型用法代码示例。如果您正苦于以下问题:Python RefResolver类的具体用法?Python RefResolver怎么用?Python RefResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RefResolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestRefResolver
class TestRefResolver(TestCase):
def setUp(self):
self.resolver = RefResolver()
self.schema = mock.MagicMock()
def test_it_resolves_local_refs(self):
ref = "#/properties/foo"
resolved = self.resolver.resolve(self.schema, ref)
self.assertEqual(resolved, self.schema["properties"]["foo"])
def test_it_retrieves_non_local_refs(self):
schema = '{"type" : "integer"}'
get_page = mock.Mock(return_value=StringIO(schema))
resolver = RefResolver(get_page=get_page)
url = "http://example.com/schema"
resolved = resolver.resolve(mock.Mock(), url)
self.assertEqual(resolved, json.loads(schema))
get_page.assert_called_once_with(url)
def test_it_uses_urlopen_by_default_for_nonlocal_refs(self):
self.assertEqual(self.resolver.get_page, urlopen)
def test_it_accepts_a_ref_store(self):
store = mock.Mock()
self.assertEqual(RefResolver(store).store, store)
def test_it_retrieves_stored_refs(self):
ref = self.resolver.store["cached_ref"] = mock.Mock()
resolved = self.resolver.resolve(self.schema, "cached_ref")
self.assertEqual(resolved, ref)
示例2: test_custom_uri_scheme_handlers
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: test_if_you_give_it_junk_you_get_a_resolution_error
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?")
示例4: test_cache_remote_on
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)
示例5: test_cache_remote_off
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)
示例6: test_it_retrieves_non_local_refs
def test_it_retrieves_non_local_refs(self):
schema = '{"type" : "integer"}'
get_page = mock.Mock(return_value=StringIO(schema))
resolver = RefResolver(get_page=get_page)
url = "http://example.com/schema"
resolved = resolver.resolve(mock.Mock(), url)
self.assertEqual(resolved, json.loads(schema))
get_page.assert_called_once_with(url)
示例7: TestRefResolver
class TestRefResolver(unittest.TestCase):
def setUp(self):
self.base_uri = ""
self.referrer = {}
self.store = {}
self.resolver = RefResolver(self.base_uri, self.referrer, self.store)
def test_it_does_not_retrieve_schema_urls_from_the_network(self):
ref = Draft3Validator.META_SCHEMA["id"]
with mock.patch.object(self.resolver, "resolve_remote") as remote:
resolved = self.resolver.resolve(ref)
self.assertEqual(resolved, Draft3Validator.META_SCHEMA)
self.assertFalse(remote.called)
def test_it_resolves_local_refs(self):
ref = "#/properties/foo"
self.referrer["properties"] = {"foo" : object()}
resolved = self.resolver.resolve(ref)
self.assertEqual(resolved, self.referrer["properties"]["foo"])
def test_it_retrieves_stored_refs(self):
self.resolver.store["cached_ref"] = {"foo" : 12}
resolved = self.resolver.resolve("cached_ref#/foo")
self.assertEqual(resolved, 12)
def test_it_retrieves_unstored_refs_via_urlopen(self):
ref = "http://bar#baz"
schema = {"baz" : 12}
with mock.patch("jsonschema.urlopen") as urlopen:
urlopen.return_value.read.return_value = json.dumps(schema)
resolved = self.resolver.resolve(ref)
self.assertEqual(resolved, 12)
urlopen.assert_called_once_with("http://bar")
def test_it_can_construct_a_base_uri_from_a_schema(self):
schema = {"id" : "foo"}
resolver = RefResolver.from_schema(schema)
self.assertEqual(resolver.base_uri, "foo")
self.assertEqual(resolver.referrer, schema)
def test_it_can_construct_a_base_uri_from_a_schema_without_id(self):
schema = {}
resolver = RefResolver.from_schema(schema)
self.assertEqual(resolver.base_uri, "")
self.assertEqual(resolver.referrer, schema)
示例8: is_valid
def is_valid(self):
handlers = {'https': self.session_request_json,
'http': self.session_request_json}
resolver = RefResolver.from_schema(self.schema.raw_schema,
handlers=handlers)
try:
validate(self.data, self.schema.raw_schema, resolver=resolver)
except (SchemaError, ValidationError):
return False
return True
示例9: validate
def validate(self):
"""
Validate that this instance matches its schema.
"""
schema = Schema(self.__class__.SCHEMA)
resolver = RefResolver.from_schema(
schema,
store=REGISTRY,
)
validate(self, schema, resolver=resolver)
示例10: resolve_reference
def resolve_reference(self, value, root):
"""Resolves a reference.
:param value: The actual reference, e.g. ``_yaml.yaml#/def``
:param root:
The containing root of :param:`value`. This needs to be
passed in order to resolve self-referential $refs,
e.g. ``#/def``.
:returns: JSON Schema pointed to by :param:`value`
"""
base, ref = value.split('#', 1)
if base:
resolver, new_root = self.resolvers[base]
referrer, resolution = resolver.resolve(value)
self.resolve_schema(resolution, new_root)
else:
resolver = RefResolver('#', root)
referrer, resolution = resolver.resolve(value)
return resolution
示例11: resolve_remote
def resolve_remote (self, uri):
"""
Overrides superclass resolve_remote, processing "jsdb:" URI,
otherwise calls superclass to fetch the schema.
"""
if uri[0:5] == "jsdb:":
document = self.resolve_jsdb (uri)
# duplicate caching logic from superclass
if self.cache_remote:
self.store[uri] = document
return document
else:
return RefResolver.resolve_remote (self, uri)
示例12: validate
def validate(self, instance, schema_id, skip_http=True):
"""
Validate an instance against a registered schema.
"""
schema = self[schema_id]
handlers = {}
if skip_http:
handlers.update(
http=do_not_resolve,
https=do_not_resolve,
)
resolver = RefResolver.from_schema(
schema,
store=self,
handlers=handlers,
)
return validate(instance, schema, resolver=resolver)
示例13: __init__
def __init__(self, host, path, version):
"""Api constructor.
`host`: used for the schema URI.
`path`: used a prefix for the route.
`version`: used for the api doc `apiVersion` attribute
"""
if path[0] != "/":
raise ValueError("path cannot be relative")
self.host = host.rstrip("/")
self.path = path.rstrip("/")
self.version = version
self.resources = {}
self._schemas = {}
self._resolver = RefResolver(self.schema_path, {}, store={})
示例14: TestRefResolver
class TestRefResolver(unittest.TestCase):
def setUp(self):
self.base_uri = ""
self.referrer = {}
self.store = {}
self.resolver = RefResolver(self.base_uri, self.referrer, self.store)
def test_it_does_not_retrieve_schema_urls_from_the_network(self):
ref = Draft3Validator.META_SCHEMA["id"]
with mock.patch.object(self.resolver, "resolve_remote") as remote:
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, Draft3Validator.META_SCHEMA)
self.assertFalse(remote.called)
def test_it_resolves_local_refs(self):
ref = "#/properties/foo"
self.referrer["properties"] = {"foo" : object()}
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, self.referrer["properties"]["foo"])
def test_it_retrieves_stored_refs(self):
self.resolver.store["cached_ref"] = {"foo" : 12}
with self.resolver.resolving("cached_ref#/foo") as resolved:
self.assertEqual(resolved, 12)
def test_it_retrieves_unstored_refs_via_requests(self):
ref = "http://bar#baz"
schema = {"baz" : 12}
with mock.patch("jsonschema.requests") as requests:
requests.get.return_value.json.return_value = schema
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, 12)
requests.get.assert_called_once_with("http://bar")
def test_it_retrieves_unstored_refs_via_urlopen(self):
ref = "http://bar#baz"
schema = {"baz" : 12}
with mock.patch("jsonschema.requests", None):
with mock.patch("jsonschema.urlopen") as urlopen:
urlopen.return_value.read.return_value = (
json.dumps(schema).encode("utf8"))
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, 12)
urlopen.assert_called_once_with("http://bar")
def test_it_can_construct_a_base_uri_from_a_schema(self):
schema = {"id" : "foo"}
resolver = RefResolver.from_schema(schema)
self.assertEqual(resolver.base_uri, "foo")
self.assertEqual(resolver.referrer, schema)
def test_it_can_construct_a_base_uri_from_a_schema_without_id(self):
schema = {}
resolver = RefResolver.from_schema(schema)
self.assertEqual(resolver.base_uri, "")
self.assertEqual(resolver.referrer, schema)
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)
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)
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)
示例15: setUp
def setUp(self):
self.referrer = {}
self.store = {self.stored_uri : self.stored_schema}
self.resolver = RefResolver(self.base_uri, self.referrer, self.store)