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"}
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"
def uri() -> URI: return URI.from_string("base/file1.json#/")
def ref_dict(): return RefDict(URI.from_string("base/file1.json#/definitions"))
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"}
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"
def test_get_uri_with_newline(reference: str): uri = URI.from_string(reference) result = resolve_uri(uri) assert result == {"foo": "bar"}
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"
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"}
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"}
def test_get_backref(): backref_uri = URI.from_string("base/file1.json#/definitions/backref") backref = resolve_uri(backref_uri) assert backref == {"type": "null"}
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"}
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"}
def test_get_no_ref(): uri = URI.from_string("base/file1.json#/definitions/foo") data = resolve_uri(uri) assert data == {"type": "string"}
def test_from_string_fails_for_bad_references(reference: str): with pytest.raises(ReferenceParseError): _ = URI.from_string(reference)
def test_from_string_modifies_bad_references(reference: str): uri = URI.from_string(reference) assert str(uri).endswith("#/")