jsonld-ex/test/unit/compaction_test.exs

596 lines
17 KiB
Elixir
Raw Normal View History

2017-03-03 20:51:16 +00:00
defmodule JSON.LD.CompactionTest do
use ExUnit.Case, async: false
alias RDF.NS.{RDFS, XSD}
2017-03-03 20:51:16 +00:00
test "Flattened form of a JSON-LD document (EXAMPLES 57-59 of https://www.w3.org/TR/json-ld/#compacted-document-form)" do
2020-06-20 02:25:58 +00:00
input =
Jason.decode!("""
2017-03-03 20:51:16 +00:00
[
{
"http://xmlns.com/foaf/0.1/name": [ "Manu Sporny" ],
"http://xmlns.com/foaf/0.1/homepage": [
{
"@id": "http://manu.sporny.org/"
}
]
}
]
2020-06-20 02:25:58 +00:00
""")
context =
Jason.decode!("""
2017-03-03 20:51:16 +00:00
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
}
}
2020-06-20 02:25:58 +00:00
""")
2017-03-03 20:51:16 +00:00
2020-06-20 02:25:58 +00:00
assert JSON.LD.compact(input, context) ==
Jason.decode!("""
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"homepage": {
"@id": "http://xmlns.com/foaf/0.1/homepage",
"@type": "@id"
}
},
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/"
}
""")
end
2017-03-03 20:51:16 +00:00
%{
"prefix" => %{
input: %{
"@id" => "http://example.com/a",
"http://example.com/b" => %{"@id" => "http://example.com/c"}
},
context: %{"ex" => "http://example.com/"},
output: %{
"@context" => %{"ex" => "http://example.com/"},
"@id" => "ex:a",
"ex:b" => %{"@id" => "ex:c"}
}
},
"term" => %{
input: %{
"@id" => "http://example.com/a",
"http://example.com/b" => %{"@id" => "http://example.com/c"}
},
context: %{"b" => "http://example.com/b"},
output: %{
"@context" => %{"b" => "http://example.com/b"},
"@id" => "http://example.com/a",
"b" => %{"@id" => "http://example.com/c"}
}
},
"integer value" => %{
input: %{
"@id" => "http://example.com/a",
"http://example.com/b" => %{"@value" => 1}
},
context: %{"b" => "http://example.com/b"},
output: %{
"@context" => %{"b" => "http://example.com/b"},
"@id" => "http://example.com/a",
"b" => 1
}
},
"boolean value" => %{
input: %{
"@id" => "http://example.com/a",
"http://example.com/b" => %{"@value" => true}
},
context: %{"b" => "http://example.com/b"},
output: %{
"@context" => %{"b" => "http://example.com/b"},
"@id" => "http://example.com/a",
"b" => true
}
},
"@id" => %{
input: %{"@id" => "http://example.org/test#example"},
context: %{},
output: %{}
},
"@id coercion" => %{
input: %{
"@id" => "http://example.com/a",
"http://example.com/b" => %{"@id" => "http://example.com/c"}
},
context: %{"b" => %{"@id" => "http://example.com/b", "@type" => "@id"}},
output: %{
"@context" => %{"b" => %{"@id" => "http://example.com/b", "@type" => "@id"}},
"@id" => "http://example.com/a",
"b" => "http://example.com/c"
}
},
"xsd:date coercion" => %{
input: %{
2020-06-20 02:25:58 +00:00
"http://example.com/b" => %{"@value" => "2012-01-04", "@type" => to_string(XSD.date())}
2017-03-03 20:51:16 +00:00
},
context: %{
2020-06-20 02:25:58 +00:00
"xsd" => XSD.__base_iri__(),
2017-03-03 20:51:16 +00:00
"b" => %{"@id" => "http://example.com/b", "@type" => "xsd:date"}
},
output: %{
"@context" => %{
2020-06-20 02:25:58 +00:00
"xsd" => XSD.__base_iri__(),
2017-03-03 20:51:16 +00:00
"b" => %{"@id" => "http://example.com/b", "@type" => "xsd:date"}
},
"b" => "2012-01-04"
}
},
"@list coercion" => %{
input: %{
"http://example.com/b" => %{"@list" => ["c", "d"]}
},
context: %{"b" => %{"@id" => "http://example.com/b", "@container" => "@list"}},
output: %{
"@context" => %{"b" => %{"@id" => "http://example.com/b", "@container" => "@list"}},
"b" => ["c", "d"]
}
},
"@list coercion (integer)" => %{
input: %{
"http://example.com/term" => [
2020-06-20 02:25:58 +00:00
%{"@list" => [1]}
2017-03-03 20:51:16 +00:00
]
},
context: %{
"term4" => %{"@id" => "http://example.com/term", "@container" => "@list"},
"@language" => "de"
},
output: %{
"@context" => %{
"term4" => %{"@id" => "http://example.com/term", "@container" => "@list"},
"@language" => "de"
},
2020-06-20 02:25:58 +00:00
"term4" => [1]
2017-03-03 20:51:16 +00:00
}
},
"@set coercion" => %{
input: %{
"http://example.com/b" => %{"@set" => ["c"]}
},
context: %{"b" => %{"@id" => "http://example.com/b", "@container" => "@set"}},
output: %{
"@context" => %{"b" => %{"@id" => "http://example.com/b", "@container" => "@set"}},
"b" => ["c"]
}
},
"empty @set coercion" => %{
input: %{
"http://example.com/b" => []
},
context: %{"b" => %{"@id" => "http://example.com/b", "@container" => "@set"}},
output: %{
"@context" => %{"b" => %{"@id" => "http://example.com/b", "@container" => "@set"}},
"b" => []
}
},
"@type with string @id" => %{
input: %{
"@id" => "http://example.com/",
2020-06-20 02:25:58 +00:00
"@type" => RDFS.Resource |> RDF.uri() |> to_string
2017-03-03 20:51:16 +00:00
},
context: %{},
output: %{
"@id" => "http://example.com/",
2020-06-20 02:25:58 +00:00
"@type" => RDFS.Resource |> RDF.uri() |> to_string
}
2017-03-03 20:51:16 +00:00
},
"@type with array @id" => %{
input: %{
"@id" => "http://example.com/",
2020-06-20 02:25:58 +00:00
"@type" => RDFS.Resource |> RDF.uri() |> to_string
2017-03-03 20:51:16 +00:00
},
context: %{},
output: %{
"@id" => "http://example.com/",
2020-06-20 02:25:58 +00:00
"@type" => RDFS.Resource |> RDF.uri() |> to_string
}
2017-03-03 20:51:16 +00:00
},
"default language" => %{
input: %{
"http://example.com/term" => [
"v5",
%{"@value" => "plain literal"}
]
},
context: %{
"term5" => %{"@id" => "http://example.com/term", "@language" => nil},
"@language" => "de"
},
output: %{
"@context" => %{
"term5" => %{"@id" => "http://example.com/term", "@language" => nil},
"@language" => "de"
},
2020-06-20 02:25:58 +00:00
"term5" => ["v5", "plain literal"]
2017-03-03 20:51:16 +00:00
}
2020-06-20 02:25:58 +00:00
}
2017-03-03 20:51:16 +00:00
}
2020-06-20 02:25:58 +00:00
|> Enum.each(fn {title, data} ->
@tag data: data
test title, %{data: data} do
assert JSON.LD.compact(data.input, data.context) == data.output
end
end)
2017-03-03 20:51:16 +00:00
describe "keyword aliasing" do
%{
"@id" => %{
input: %{
"@id" => "",
2020-06-20 02:25:58 +00:00
"@type" => RDFS.Resource |> RDF.uri() |> to_string
2017-03-03 20:51:16 +00:00
},
context: %{"id" => "@id"},
output: %{
"@context" => %{"id" => "@id"},
"id" => "",
2020-06-20 02:25:58 +00:00
"@type" => RDFS.Resource |> RDF.uri() |> to_string
2017-03-03 20:51:16 +00:00
}
},
"@type" => %{
input: %{
2020-06-20 02:25:58 +00:00
"@type" => RDFS.Resource |> RDF.uri() |> to_string,
2017-03-03 20:51:16 +00:00
"http://example.org/foo" => %{"@value" => "bar", "@type" => "http://example.com/type"}
},
context: %{"type" => "@type"},
output: %{
"@context" => %{"type" => "@type"},
2020-06-20 02:25:58 +00:00
"type" => RDFS.Resource |> RDF.uri() |> to_string,
2017-03-03 20:51:16 +00:00
"http://example.org/foo" => %{"@value" => "bar", "type" => "http://example.com/type"}
}
},
"@language" => %{
input: %{
"http://example.org/foo" => %{"@value" => "bar", "@language" => "baz"}
},
context: %{"language" => "@language"},
output: %{
"@context" => %{"language" => "@language"},
"http://example.org/foo" => %{"@value" => "bar", "language" => "baz"}
}
},
"@value" => %{
input: %{
"http://example.org/foo" => %{"@value" => "bar", "@language" => "baz"}
},
context: %{"literal" => "@value"},
output: %{
"@context" => %{"literal" => "@value"},
"http://example.org/foo" => %{"literal" => "bar", "@language" => "baz"}
}
},
"@list" => %{
input: %{
"http://example.org/foo" => %{"@list" => ["bar"]}
},
context: %{"list" => "@list"},
output: %{
"@context" => %{"list" => "@list"},
"http://example.org/foo" => %{"list" => ["bar"]}
}
2020-06-20 02:25:58 +00:00
}
2017-03-03 20:51:16 +00:00
}
2020-06-20 02:25:58 +00:00
|> Enum.each(fn {title, data} ->
@tag data: data
test title, %{data: data} do
assert JSON.LD.compact(data.input, data.context) == data.output
end
end)
2017-03-03 20:51:16 +00:00
end
describe "term selection" do
%{
"Uses term with nil language when two terms conflict on language" => %{
2020-06-20 02:25:58 +00:00
input: [
%{
"http://example.com/term" => %{"@value" => "v1"}
}
],
2017-03-03 20:51:16 +00:00
context: %{
2020-06-20 02:25:58 +00:00
"term5" => %{"@id" => "http://example.com/term", "@language" => nil},
2017-03-03 20:51:16 +00:00
"@language" => "de"
},
output: %{
"@context" => %{
2020-06-20 02:25:58 +00:00
"term5" => %{"@id" => "http://example.com/term", "@language" => nil},
2017-03-03 20:51:16 +00:00
"@language" => "de"
},
2020-06-20 02:25:58 +00:00
"term5" => "v1"
2017-03-03 20:51:16 +00:00
}
},
"Uses subject alias" => %{
2020-06-20 02:25:58 +00:00
input: [
%{
"@id" => "http://example.com/id1",
"http://example.com/id1" => %{"@value" => "foo", "@language" => "de"}
}
],
2017-03-03 20:51:16 +00:00
context: %{
"id1" => "http://example.com/id1",
"@language" => "de"
},
output: %{
"@context" => %{
"id1" => "http://example.com/id1",
"@language" => "de"
},
"@id" => "http://example.com/id1",
"id1" => "foo"
}
},
"compact-0007" => %{
2020-06-20 02:25:58 +00:00
input:
Jason.decode!("""
{"http://example.org/vocab#contains": "this-is-not-an-IRI"}
"""),
context:
Jason.decode!("""
{
"ex": "http://example.org/vocab#",
"ex:contains": {"@type": "@id"}
}
"""),
output:
Jason.decode!("""
{
"@context": {
"ex": "http://example.org/vocab#",
"ex:contains": {"@type": "@id"}
},
"http://example.org/vocab#contains": "this-is-not-an-IRI"
}
""")
2017-03-03 20:51:16 +00:00
}
}
2020-06-20 02:25:58 +00:00
|> Enum.each(fn {title, data} ->
@tag data: data
test title, %{data: data} do
assert JSON.LD.compact(data.input, data.context) == data.output
end
end)
2017-03-03 20:51:16 +00:00
end
describe "@reverse" do
%{
"compact-0033" => %{
2020-06-20 02:25:58 +00:00
input:
Jason.decode!("""
[
{
"@id": "http://example.com/people/markus",
"@reverse": {
"http://xmlns.com/foaf/0.1/knows": [
{
"@id": "http://example.com/people/dave",
"http://xmlns.com/foaf/0.1/name": [ { "@value": "Dave Longley" } ]
}
]
},
"http://xmlns.com/foaf/0.1/name": [ { "@value": "Markus Lanthaler" } ]
}
]
"""),
context:
Jason.decode!("""
2017-03-03 20:51:16 +00:00
{
"name": "http://xmlns.com/foaf/0.1/name",
2020-06-20 02:25:58 +00:00
"isKnownBy": { "@reverse": "http://xmlns.com/foaf/0.1/knows" }
}
"""),
output:
Jason.decode!("""
{
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"isKnownBy": {
"@reverse": "http://xmlns.com/foaf/0.1/knows"
}
},
"@id": "http://example.com/people/markus",
"name": "Markus Lanthaler",
2017-03-03 20:51:16 +00:00
"isKnownBy": {
2020-06-20 02:25:58 +00:00
"@id": "http://example.com/people/dave",
"name": "Dave Longley"
2017-03-03 20:51:16 +00:00
}
}
2020-06-20 02:25:58 +00:00
""")
2017-03-03 20:51:16 +00:00
}
}
2020-06-20 02:25:58 +00:00
|> Enum.each(fn {title, data} ->
@tag data: data
test title, %{data: data} do
assert JSON.LD.compact(data.input, data.context) == data.output
end
end)
2017-03-03 20:51:16 +00:00
end
describe "context as value" do
test "it includes the context in the output document" do
context = %{
"foo" => "http://example.com/"
}
2020-06-20 02:25:58 +00:00
2017-03-03 20:51:16 +00:00
input = %{
"http://example.com/" => "bar"
}
2020-06-20 02:25:58 +00:00
2017-03-03 20:51:16 +00:00
expected = %{
"@context" => %{
"foo" => "http://example.com/"
},
"foo" => "bar"
}
2020-06-20 02:25:58 +00:00
2017-03-03 20:51:16 +00:00
assert JSON.LD.compact(input, context) == expected
end
end
2020-06-20 02:25:58 +00:00
# TODO:
# describe "context as reference" do
# let(:remote_doc) do
# JSON::LD::API::RemoteDocument.new("http://example.com/context", %q({"@context": {"b": "http://example.com/b"}}))
# end
# test "uses referenced context" do
# input = %{
# "http://example.com/b" => "c"
# }
# expected = %{
# "@context" => "http://example.com/context",
# "b" => "c"
# }
# allow(JSON::LD::API).to receive(:documentLoader).with("http://example.com/context", anything).and_yield(remote_doc)
# jld = JSON::LD::API.compact(input, "http://example.com/context", logger: logger, validate: true)
# expect(jld).to produce(expected, logger)
# end
# end
2017-03-03 20:51:16 +00:00
describe "@list" do
%{
"1 term 2 lists 2 languages" => %{
2020-06-20 02:25:58 +00:00
input: [
%{
"http://example.com/foo" => [
%{"@list" => [%{"@value" => "en", "@language" => "en"}]},
%{"@list" => [%{"@value" => "de", "@language" => "de"}]}
]
}
],
2017-03-03 20:51:16 +00:00
context: %{
2020-06-20 02:25:58 +00:00
"foo_en" => %{
"@id" => "http://example.com/foo",
"@container" => "@list",
"@language" => "en"
},
"foo_de" => %{
"@id" => "http://example.com/foo",
"@container" => "@list",
"@language" => "de"
}
2017-03-03 20:51:16 +00:00
},
output: %{
"@context" => %{
2020-06-20 02:25:58 +00:00
"foo_en" => %{
"@id" => "http://example.com/foo",
"@container" => "@list",
"@language" => "en"
},
"foo_de" => %{
"@id" => "http://example.com/foo",
"@container" => "@list",
"@language" => "de"
}
2017-03-03 20:51:16 +00:00
},
"foo_en" => ["en"],
"foo_de" => ["de"]
}
2020-06-20 02:25:58 +00:00
}
2017-03-03 20:51:16 +00:00
}
2020-06-20 02:25:58 +00:00
|> Enum.each(fn {title, data} ->
@tag data: data
test title, %{data: data} do
assert JSON.LD.compact(data.input, data.context) == data.output
end
end)
2017-03-03 20:51:16 +00:00
end
describe "language maps" do
%{
"compact-0024" => %{
input: [
%{
"@id" => "http://example.com/queen",
"http://example.com/vocab/label" => [
%{"@value" => "The Queen", "@language" => "en"},
%{"@value" => "Die Königin", "@language" => "de"},
%{"@value" => "Ihre Majestät", "@language" => "de"}
]
}
],
context: %{
"vocab" => "http://example.com/vocab/",
"label" => %{"@id" => "vocab:label", "@container" => "@language"}
},
output: %{
"@context" => %{
"vocab" => "http://example.com/vocab/",
"label" => %{"@id" => "vocab:label", "@container" => "@language"}
},
"@id" => "http://example.com/queen",
"label" => %{
"en" => "The Queen",
"de" => ["Die Königin", "Ihre Majestät"]
}
}
2020-06-20 02:25:58 +00:00
}
2017-03-03 20:51:16 +00:00
}
2020-06-20 02:25:58 +00:00
|> Enum.each(fn {title, data} ->
@tag data: data
test title, %{data: data} do
assert JSON.LD.compact(data.input, data.context) == data.output
end
end)
2017-03-03 20:51:16 +00:00
end
describe "@graph" do
%{
"Uses @graph given mutliple inputs" => %{
input: [
%{"http://example.com/foo" => ["foo"]},
%{"http://example.com/bar" => ["bar"]}
],
context: %{"ex" => "http://example.com/"},
output: %{
"@context" => %{"ex" => "http://example.com/"},
"@graph" => [
2020-06-20 02:25:58 +00:00
%{"ex:foo" => "foo"},
2017-03-03 20:51:16 +00:00
%{"ex:bar" => "bar"}
]
}
2020-06-20 02:25:58 +00:00
}
2017-03-03 20:51:16 +00:00
}
2020-06-20 02:25:58 +00:00
|> Enum.each(fn {title, data} ->
@tag data: data
test title, %{data: data} do
assert JSON.LD.compact(data.input, data.context) == data.output
end
end)
2017-03-03 20:51:16 +00:00
end
describe "exceptions" do
%{
"@list containing @list" => %{
input: %{
"http://example.org/foo" => %{"@list" => [%{"@list" => ["baz"]}]}
},
exception: JSON.LD.ListOfListsError
},
"@list containing @list (with coercion)" => %{
input: %{
"@context" => %{"http://example.org/foo" => %{"@container" => "@list"}},
"http://example.org/foo" => [%{"@list" => ["baz"]}]
},
exception: JSON.LD.ListOfListsError
2020-06-20 02:25:58 +00:00
}
2017-03-03 20:51:16 +00:00
}
2020-06-20 02:25:58 +00:00
|> Enum.each(fn {title, data} ->
@tag data: data
test title, %{data: data} do
assert_raise data.exception, fn -> JSON.LD.compact(data.input, %{}) end
end
end)
2017-03-03 20:51:16 +00:00
end
end