當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。