本文整理汇总了Python中jsonschema.RefResolver.resolving方法的典型用法代码示例。如果您正苦于以下问题:Python RefResolver.resolving方法的具体用法?Python RefResolver.resolving怎么用?Python RefResolver.resolving使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jsonschema.RefResolver
的用法示例。
在下文中一共展示了RefResolver.resolving方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cache_remote_off
# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolving [as 别名]
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)
示例2: test_cache_remote_on
# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolving [as 别名]
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)
示例3: test_custom_uri_scheme_handlers
# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolving [as 别名]
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)
示例4: test_if_you_give_it_junk_you_get_a_resolution_error
# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolving [as 别名]
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?")
示例5: Api
# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolving [as 别名]
#.........这里部分代码省略.........
def schema_handler(self, request):
"""http handler for the schema request.
"""
return self._json_handler(self.schemas())
def api_doc_handler(self, request):
"""http handler for the route api-doc request.
"""
return self._json_handler(self.api_doc())
def apis_handler(self, request, path):
"""http handler for a resource api-doc request.
"""
resource = self.resources.get("/%s" % path, None)
if resource is None:
return self._json_handler({"error": "resource not found"}, 404)
return self._json_handler(resource.api_doc())
def routes(self):
"""Return a route collection for an api
(including the api-doc and schema):
- the request handler routes are define by the
`swagger.ApiRequestHandler.path` class attributes.
- the api-doc path `<api.path>/api-docs`
- the schema path `<api.path>/json-schemas/`
"""
rel_routes = []
rel_routes.append(webapp2.Route("/api-docs", self.api_doc_handler, methods=["GET"]))
rel_routes.append(webapp2.Route("/api-docs/<path:.+>", self.apis_handler, methods=["GET"]))
rel_routes.append(webapp2.Route("/json-schemas", self.schema_handler, methods=["GET"]))
for resource in self.resources.itervalues():
for api in resource.apis.itervalues():
rel_routes.append(webapp2.Route(api.path, api.handler))
return routes.PathPrefixRoute(self.path, rel_routes)
def resource(self, path, desc=None):
"""Define a new resource.
"""
if path not in self.resources:
self.resources[path] = _Resource(self, path, desc)
return self.resources[path]
def schema(self, name, properties=None, additional_properties=False, **kw):
"""Create a new schema definition.
The base schema can currently only be defined as objects
(swagger only define models as object).
"""
properties = {} if properties is None else properties
kw.setdefault("required", [])
for prop_name, prop in properties.iteritems():
if prop.required:
kw["required"].append(prop_name)
prop.required = None
definition = Object(id=name, properties=properties, additional_properties=additional_properties, **kw)
self._schemas[name] = definition
self._update_resolver()
def schemas(self):
"""Json-schema for all complex type defined in an API.
"""
schemas = {"id": "%s#" % self.schema_path, "$schema": "http://json-schema.org/draft-04/schema#"}
for s_id, s in self._schemas.iteritems():
if s is None:
continue
schemas[s_id] = s
return to_dict(schemas, ctx=_Context(self))
def ref(self, name, required=False):
"""Return an object with "$ref" attribute.
Suitable to be used in json schema document. Use Api.model if
the reference is to be used in swagger api document.
"""
self._schemas.setdefault(name, None)
return _Ref(name, required=required)
def _update_resolver(self):
self._resolver.store[self.schema_path] = self.schemas()
def validate(self, schema, data):
"""Create json-schema validator for a complex type.
"""
with self._resolver.resolving("#/%s" % schema) as schema:
validator = Draft4Validator(schema, resolver=self._resolver)
validator.validate(data)
示例6: TestRefResolver
# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolving [as 别名]
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)