2021-09-27 18:57:17 +00:00
|
|
|
defmodule RDF.Star.Description.Test do
|
|
|
|
use RDF.Test.Case
|
|
|
|
|
|
|
|
describe "new/1" do
|
|
|
|
test "with a valid triple as subject" do
|
|
|
|
assert description_of_subject(
|
|
|
|
Description.new(statement()),
|
|
|
|
statement()
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Description.new(statement(), init: {EX.ap(), EX.ao()})
|
|
|
|
|> description_includes_predication({EX.ap(), EX.ao()})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a coercible triple as subject" do
|
|
|
|
assert description_of_subject(
|
|
|
|
Description.new(coercible_statement()),
|
|
|
|
statement()
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Description.new(statement(), init: {EX.ap(), EX.ao()})
|
|
|
|
|> description_includes_predication({EX.ap(), EX.ao()})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "subject/1" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.subject(empty_annotation_description()) == statement()
|
2021-09-27 18:57:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "change_subject/2" do
|
|
|
|
changed = Description.change_subject(description(), coercible_statement())
|
|
|
|
assert changed.subject == statement()
|
|
|
|
assert Description.change_subject(changed, description().subject) == description()
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "add/3" do
|
|
|
|
test "with a proper triple as a subject" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert empty_annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Description.add({statement(), EX.ap(), EX.ao()})
|
|
|
|
|> description_includes_predication({EX.ap(), EX.ao()})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a proper triple as a object" do
|
|
|
|
assert description()
|
|
|
|
|> Description.add({EX.Subject, EX.ap(), statement()})
|
|
|
|
|> description_includes_predication({EX.ap(), statement()})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a proper triple as a subject and object" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert empty_annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Description.add({statement(), EX.ap(), statement()})
|
|
|
|
|> description_includes_predication({EX.ap(), statement()})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of proper objects" do
|
|
|
|
description =
|
|
|
|
description()
|
|
|
|
|> Description.add({EX.Subject, EX.ap(), [statement(), {EX.s(), EX.p(), EX.o()}]})
|
|
|
|
|
|
|
|
assert description_includes_predication(description, {EX.ap(), statement()})
|
|
|
|
assert description_includes_predication(description, {EX.ap(), {EX.s(), EX.p(), EX.o()}})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of predicate-object tuples" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert empty_annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Description.add([{EX.ap(), statement()}])
|
|
|
|
|> description_includes_predication({EX.ap(), statement()})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a description map" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert empty_annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Description.add(%{EX.ap() => statement()})
|
|
|
|
|> description_includes_predication({EX.ap(), statement()})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with coercible triples" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert empty_annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Description.add({coercible_statement(), EX.ap(), coercible_statement()})
|
|
|
|
|> description_includes_predication({EX.ap(), statement()})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "put/3" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Description.put({statement(), EX.ap(), EX.ao2()})
|
|
|
|
|> description_includes_predication({EX.ap(), EX.ao2()})
|
|
|
|
|
2021-11-16 22:05:09 +00:00
|
|
|
assert annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Description.put({statement(), EX.ap(), statement()})
|
|
|
|
|> description_includes_predication({EX.ap(), statement()})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete/3" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.delete(annotation_description(), {statement(), EX.ap(), EX.ao()}) ==
|
|
|
|
empty_annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.delete(
|
|
|
|
description_with_quoted_triple_object(),
|
|
|
|
{EX.As, EX.ap(), statement()}
|
|
|
|
) ==
|
2021-09-27 18:57:17 +00:00
|
|
|
Description.new(EX.As)
|
|
|
|
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.delete(description_with_quoted_triple_object(), {EX.ap(), statement()}) ==
|
2021-09-27 18:57:17 +00:00
|
|
|
Description.new(EX.As)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "delete_predicates/2" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.delete_predicates(annotation_description(), EX.ap()) ==
|
|
|
|
empty_annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.delete_predicates(description_with_quoted_triple_object(), EX.ap()) ==
|
2021-09-27 18:57:17 +00:00
|
|
|
Description.new(EX.As)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fetch/2" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.fetch(annotation_description(), EX.ap()) == {:ok, [EX.ao()]}
|
|
|
|
|
|
|
|
assert Description.fetch(description_with_quoted_triple_object(), EX.ap()) ==
|
|
|
|
{:ok, [statement()]}
|
2021-09-27 18:57:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "get/2" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.get(annotation_description(), EX.ap()) == [EX.ao()]
|
|
|
|
assert Description.get(description_with_quoted_triple_object(), EX.ap()) == [statement()]
|
2021-09-27 18:57:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "first/2" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.first(annotation_description(), EX.ap()) == EX.ao()
|
|
|
|
assert Description.first(description_with_quoted_triple_object(), EX.ap()) == statement()
|
2021-09-27 18:57:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "pop/2" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.pop(annotation_description(), EX.ap()) ==
|
|
|
|
{[EX.ao()], empty_annotation_description()}
|
2021-09-27 18:57:17 +00:00
|
|
|
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.pop(description_with_quoted_triple_object(), EX.ap()) ==
|
2021-09-27 18:57:17 +00:00
|
|
|
{[statement()], Description.new(EX.As)}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "update/4" do
|
|
|
|
assert (description =
|
2021-11-16 22:05:09 +00:00
|
|
|
Description.update(empty_annotation_description(), EX.ap(), statement(), fn _ ->
|
2021-09-27 18:57:17 +00:00
|
|
|
raise "unexpected"
|
|
|
|
end)) ==
|
2021-11-16 22:05:09 +00:00
|
|
|
empty_annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Description.add(%{EX.ap() => statement()})
|
|
|
|
|
|
|
|
assert Description.update(description, EX.ap(), statement(), fn
|
|
|
|
[{s, p, _} = statement] ->
|
|
|
|
assert statement == statement()
|
|
|
|
[statement, {s, p, EX.O}]
|
|
|
|
end) ==
|
2021-11-16 22:05:09 +00:00
|
|
|
empty_annotation_description()
|
2021-09-27 18:57:17 +00:00
|
|
|
|> Description.add(%{EX.ap() => statement()})
|
|
|
|
|> Description.add(%{EX.ap() => {EX.S, EX.P, EX.O}})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "objects/1" do
|
|
|
|
assert Description.new(statement(), init: {EX.ap(), statement()})
|
|
|
|
|> Description.objects() == MapSet.new([statement()])
|
|
|
|
end
|
|
|
|
|
|
|
|
test "resources/1" do
|
|
|
|
assert Description.new(statement(), init: {EX.ap(), statement()})
|
|
|
|
|> Description.resources() == MapSet.new([statement(), EX.ap()])
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "statements/1" do
|
|
|
|
test "without the filter_star flag" do
|
|
|
|
assert Description.new(statement(), init: {EX.ap(), statement()})
|
|
|
|
|> Description.statements() == [{statement(), EX.ap(), statement()}]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with the filter_star flag" do
|
|
|
|
assert Description.new(statement(),
|
|
|
|
init: [
|
|
|
|
{EX.ap(), EX.ao()},
|
|
|
|
{EX.ap(), statement()}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Description.statements(filter_star: true) == []
|
|
|
|
|
|
|
|
assert Description.new(EX.s(),
|
|
|
|
init: [
|
|
|
|
{EX.p(), EX.o()},
|
|
|
|
{EX.ap(), statement()}
|
|
|
|
]
|
|
|
|
)
|
2021-11-20 20:30:29 +00:00
|
|
|
|> Description.triples(filter_star: true) == [{EX.s(), EX.p(), EX.o()}]
|
2021-09-27 18:57:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "statement_count/1" do
|
|
|
|
assert Description.new(statement(), init: {EX.ap(), statement()})
|
|
|
|
|> Description.statement_count() == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
test "include?/2" do
|
|
|
|
assert Description.new(statement(), init: {EX.ap(), statement()})
|
|
|
|
|> Description.include?({statement(), EX.ap(), statement()})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "describes?/2" do
|
2021-11-16 22:05:09 +00:00
|
|
|
assert Description.describes?(annotation_description(), statement())
|
|
|
|
assert Description.describes?(annotation_description(), coercible_statement())
|
2021-09-27 18:57:17 +00:00
|
|
|
end
|
|
|
|
|
2021-11-20 20:40:35 +00:00
|
|
|
describe "without_quoted_triple_objects/1" do
|
2021-11-14 13:30:30 +00:00
|
|
|
test "empty description" do
|
2021-11-20 20:40:35 +00:00
|
|
|
assert Description.without_quoted_triple_objects(description()) ==
|
|
|
|
Description.new(description())
|
2021-11-14 13:30:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "when the description has no quoted triples on object position" do
|
|
|
|
description =
|
|
|
|
Description.new(statement(),
|
|
|
|
init: [
|
|
|
|
{EX.ap1(), EX.ao()},
|
|
|
|
{EX.ap2(), EX.ao()}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2021-11-20 20:40:35 +00:00
|
|
|
assert Description.without_quoted_triple_objects(description) == description
|
2021-11-14 13:30:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "when the description has quoted triples" do
|
|
|
|
assert Description.new(statement(),
|
|
|
|
init: [
|
|
|
|
{EX.ap1(), EX.ao()},
|
|
|
|
{EX.ap1(), statement()},
|
|
|
|
{EX.ap2(), statement()}
|
|
|
|
]
|
|
|
|
)
|
2021-11-20 20:40:35 +00:00
|
|
|
|> Description.without_quoted_triple_objects() ==
|
2021-11-14 13:30:30 +00:00
|
|
|
Description.new(statement(), init: {EX.ap1(), EX.ao()})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-27 18:57:17 +00:00
|
|
|
test "values/2" do
|
|
|
|
assert Description.new(statement(), init: {EX.ap(), statement()})
|
|
|
|
|> Description.values() == %{}
|
|
|
|
|
|
|
|
assert Description.new(EX.s(),
|
|
|
|
init: [
|
|
|
|
{EX.p(), ~L"Foo"},
|
|
|
|
{EX.ap(), statement()}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Description.values() ==
|
|
|
|
%{RDF.Term.value(EX.p()) => ["Foo"]}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "map/2" do
|
|
|
|
mapping = fn
|
|
|
|
{:predicate, predicate} ->
|
|
|
|
predicate |> to_string() |> String.split("/") |> List.last() |> String.to_atom()
|
|
|
|
|
|
|
|
{_, term} ->
|
|
|
|
RDF.Term.value(term)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert Description.new(statement(), init: {EX.ap(), statement()})
|
|
|
|
|> Description.map(mapping) == %{}
|
|
|
|
|
|
|
|
assert Description.new(EX.s(),
|
|
|
|
init: [
|
|
|
|
{EX.p(), ~L"Foo"},
|
|
|
|
{EX.ap(), statement()}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|> Description.map(mapping) ==
|
|
|
|
%{p: ["Foo"]}
|
|
|
|
end
|
|
|
|
end
|