当前位置: 首页>>代码示例>>Python>>正文


Python RefResolver.resolve方法代码示例

本文整理汇总了Python中jsonschema.RefResolver.resolve方法的典型用法代码示例。如果您正苦于以下问题:Python RefResolver.resolve方法的具体用法?Python RefResolver.resolve怎么用?Python RefResolver.resolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jsonschema.RefResolver的用法示例。


在下文中一共展示了RefResolver.resolve方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestRefResolver

# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolve [as 别名]
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)
开发者ID:depassp,项目名称:jsonschema,代码行数:34,代码来源:tests.py

示例2: TestRefResolver

# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolve [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:
            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)
开发者ID:rsternagel,项目名称:jsonschema,代码行数:50,代码来源:tests.py

示例3: test_it_retrieves_non_local_refs

# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolve [as 别名]
    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)
开发者ID:depassp,项目名称:jsonschema,代码行数:12,代码来源:tests.py

示例4: resolve_reference

# 需要导入模块: from jsonschema import RefResolver [as 别名]
# 或者: from jsonschema.RefResolver import resolve [as 别名]
    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
开发者ID:NCI-GDC,项目名称:gdcdictionary,代码行数:24,代码来源:__init__.py


注:本文中的jsonschema.RefResolver.resolve方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。