當前位置: 首頁>>代碼示例>>Python>>正文


Python RefResolver.resolving方法代碼示例

本文整理匯總了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)
開發者ID:alexstrat,項目名稱:jsonschema,代碼行數:12,代碼來源:tests.py

示例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)
開發者ID:alexstrat,項目名稱:jsonschema,代碼行數:12,代碼來源:tests.py

示例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)
開發者ID:alexstrat,項目名稱:jsonschema,代碼行數:10,代碼來源:tests.py

示例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?")
開發者ID:sverch,項目名稱:willitlink,代碼行數:10,代碼來源:tests.py

示例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)
開發者ID:btweller,項目名稱:webapp2ext.swagger,代碼行數:104,代碼來源:__init__.py

示例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)
開發者ID:alexstrat,項目名稱:jsonschema,代碼行數:91,代碼來源:tests.py


注:本文中的jsonschema.RefResolver.resolving方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。