Example #1
0
 def test_ref_pointer_returns_default_if_no_match(
     uri: URI, document: Dict[str, Any]
 ):
     default = "default"
     assert RefPointer(uri.get("nonexistent")).resolve(
         document, default=default
     )
Example #2
0
 def test_to_last(
     uri: URI,
     document: Dict[str, Any],
     path: Iterable[str],
     expected: Tuple[Any, Union[str, int]],
 ):
     pointer = RefPointer(uri.get("definitions", *path))
     assert pointer.to_last(document) == expected
Example #3
0
 def test_ref_pointer_resolve(
     uri: URI,
     document: Dict[str, Any],
     method: str,
     path: Iterable[str],
     expected: Any,
 ):
     pointer = RefPointer(uri.get("definitions", *path))
     assert getattr(pointer, method)(document) == expected
Example #4
0
 def test_get_remote_ref_cache_clear_reloads():
     remote_ref_uri = URI.from_string(
         "base/file1.json#/definitions/remote_ref")
     resolve_uri.cache_clear()
     with patch(f"{resolve_uri.__module__}.get_document") as mock_doc:
         mock_doc.side_effect = get_document
         _ = [resolve_uri(remote_ref_uri) for _ in range(3)]
         assert mock_doc.call_count == 2  # file1 and file2 loaded
         resolve_uri.cache_clear()
         remote_ref = resolve_uri(remote_ref_uri)
         assert mock_doc.call_count == 4
         assert remote_ref == {"type": "integer"}
Example #5
0
 def test_get_nested_remote_ref():
     nested_remote_uri = URI.from_string(
         "base/file1.json#/definitions/remote_nested/foo")
     nested_remote = resolve_uri(nested_remote_uri)
     assert nested_remote == {"type": "array"}
Example #6
0
 def uri() -> URI:
     return URI.from_string("base/file1.json#/")
Example #7
0
 def ref_dict():
     return RefDict(URI.from_string("base/file1.json#/definitions"))
Example #8
0
 def test_get_ref_with_escaped_chars(field: str):
     uri = URI.from_string(
         f"base/with-escaped-chars.json#/properties/{field}")
     result = resolve_uri(uri)
     assert result == {"type": "integer"}
Example #9
0
 def test_get_ref_with_newline(base: str):
     uri = URI.from_string(f"{base}#/top/ref\nto\nnewline/foo")
     result = resolve_uri(uri)
     assert result == "bar"
Example #10
0
def test_from_string_modifies_bad_references(reference: str):
    uri = URI.from_string(reference)
    assert str(uri).endswith("#/")
Example #11
0
 def test_ref_pointer_returns_non_dict_values(uri: URI,
                                              document: Dict[str, Any]):
     uri = uri.get("definitions").get("foo").get("type")
     assert RefPointer(uri).resolve(document) == "string"
Example #12
0
def test_relative_raises_type_error_if_passed_bad_reference():
    with pytest.raises(TypeError):
        _ = URI(uri_base="foo", uri_name="bar", pointer="/foo/bar").relative({})
Example #13
0
 def test_re_pointer_path(uri: URI, path: Iterable[str], expected: str):
     pointer = RefPointer(uri.get("definitions", *path))
     assert pointer.path == expected
Example #14
0
 def test_get_backref():
     backref_uri = URI.from_string("base/file1.json#/definitions/backref")
     backref = resolve_uri(backref_uri)
     assert backref == {"type": "null"}
Example #15
0
 def test_get_remote_ref():
     remote_ref_uri = URI.from_string(
         "base/file1.json#/definitions/remote_ref")
     remote_ref = resolve_uri(remote_ref_uri)
     assert remote_ref == {"type": "integer"}
Example #16
0
 def test_get_local_ref():
     local_ref_uri = URI.from_string(
         "base/file1.json#/definitions/local_ref")
     local_ref = resolve_uri(local_ref_uri)
     assert local_ref == {"type": "number"}
Example #17
0
 def test_get_no_ref():
     uri = URI.from_string("base/file1.json#/definitions/foo")
     data = resolve_uri(uri)
     assert data == {"type": "string"}
Example #18
0
def test_from_string_fails_for_bad_references(reference: str):
    with pytest.raises(ReferenceParseError):
        _ = URI.from_string(reference)
Example #19
0
 def test_set_fails(uri: URI, document: Dict[str, Any],
                    path: Iterable[str]):
     pointer = RefPointer(uri.get("definitions", *path))
     with pytest.raises(NotImplementedError):
         pointer.set(document, "foo")
Example #20
0
 def test_get_non_reference():
     non_ref_uri = URI.from_string("base/nonref.json#/definitions")
     non_ref = resolve_uri(non_ref_uri)
     assert non_ref["$ref"] == {"type": "string"}
Example #21
0
 def test_ref_pointer_raises_on_no_match(uri: URI, document: Dict[str,
                                                                  Any]):
     with pytest.raises(JsonPointerException):
         _ = RefPointer(uri.get("nonexistent")).resolve(document)
Example #22
0
 def test_get_ref_with_spaces(base: str):
     uri = URI.from_string(f"{base}#/top/ref to spaces/foo")
     result = resolve_uri(uri)
     assert result == "bar"
Example #23
0
 def test_ref_to_primitive():
     uri = URI.from_string(
         "base/ref-to-primitive.json#/top/ref_to_primitive")
     document = loader(uri.root)
     assert RefPointer(uri).resolve(document) == "foo"
Example #24
0
 def test_get_uri_with_newline(reference: str):
     uri = URI.from_string(reference)
     result = resolve_uri(uri)
     assert result == {"foo": "bar"}