2016-10-15 16:26:56 +00:00
|
|
|
defmodule RDF.DescriptionTest do
|
2017-02-13 00:21:32 +00:00
|
|
|
use RDF.Test.Case
|
2016-11-02 02:19:19 +00:00
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
doctest RDF.Description
|
|
|
|
|
2017-03-31 14:27:34 +00:00
|
|
|
describe "new" do
|
2017-08-20 20:35:14 +00:00
|
|
|
test "with a subject IRI" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert description_of_subject(
|
|
|
|
Description.new(~I<http://example.com/description/subject>),
|
|
|
|
~I<http://example.com/description/subject>
|
|
|
|
)
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
test "with a raw subject IRI string" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert description_of_subject(
|
|
|
|
Description.new("http://example.com/description/subject"),
|
|
|
|
~I<http://example.com/description/subject>
|
|
|
|
)
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
test "with an unresolved subject IRI term atom" do
|
|
|
|
assert description_of_subject(Description.new(EX.Bar), iri(EX.Bar))
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with a BlankNode subject" do
|
|
|
|
assert description_of_subject(Description.new(bnode(:foo)), bnode(:foo))
|
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "with another description" do
|
|
|
|
existing_description = description({EX.Subject, EX.predicate(), EX.Object})
|
|
|
|
new_description = Description.new(existing_description)
|
|
|
|
assert description_of_subject(new_description, iri(EX.Subject))
|
|
|
|
refute description_includes_predication(new_description, {EX.predicate(), iri(EX.Object)})
|
2017-05-25 13:29:39 +00:00
|
|
|
end
|
2020-09-25 14:07:37 +00:00
|
|
|
|
|
|
|
test "with init data" do
|
|
|
|
desc = Description.new(EX.Subject, init: {EX.Subject, EX.predicate(), EX.Object})
|
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object)})
|
|
|
|
|
|
|
|
desc =
|
|
|
|
Description.new(
|
|
|
|
EX.Subject,
|
|
|
|
init: [
|
|
|
|
{EX.Subject, EX.predicate1(), EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2(), EX.Object2}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
|
|
|
|
|
|
|
other_desc = Description.new(EX.Subject2, init: {EX.Subject2, EX.predicate(), EX.Object})
|
|
|
|
desc = Description.new(EX.Subject, init: other_desc)
|
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object)})
|
2020-10-09 14:32:24 +00:00
|
|
|
|
|
|
|
desc =
|
|
|
|
Description.new(
|
|
|
|
EX.Subject,
|
|
|
|
init: %{
|
|
|
|
p1: EX.Object1,
|
|
|
|
p2: EX.Object2
|
|
|
|
},
|
|
|
|
context: %{
|
|
|
|
p1: EX.predicate1(),
|
|
|
|
p2: EX.predicate2()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
2020-09-25 14:07:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with an initializer function" do
|
|
|
|
desc = Description.new(EX.Subject, init: fn -> {EX.Subject, EX.predicate(), EX.Object} end)
|
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object)})
|
|
|
|
end
|
2016-11-24 07:44:07 +00:00
|
|
|
end
|
|
|
|
|
2020-07-28 12:45:12 +00:00
|
|
|
test "subject/1" do
|
|
|
|
assert Description.subject(description()) == description().subject
|
|
|
|
end
|
|
|
|
|
|
|
|
test "change_subject/2" do
|
|
|
|
assert Description.change_subject(description(), EX.NewSubject).subject ==
|
|
|
|
iri(EX.NewSubject)
|
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
describe "add/3" do
|
|
|
|
test "with a triple" do
|
|
|
|
assert Description.add(description(), {iri(EX.Subject), EX.predicate(), iri(EX.Object)})
|
2020-06-29 08:37:42 +00:00
|
|
|
|> description_includes_predication({EX.predicate(), iri(EX.Object)})
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
assert Description.add(description(), {iri(EX.Subject), EX.predicate(), bnode(:foo)})
|
|
|
|
|> description_includes_predication({EX.predicate(), bnode(:foo)})
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
assert Description.add(description(), {iri(EX.Subject), EX.predicate(), literal(42)})
|
2020-06-29 08:37:42 +00:00
|
|
|
|> description_includes_predication({EX.predicate(), literal(42)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
test "with a quad" do
|
|
|
|
assert Description.add(
|
|
|
|
description(),
|
|
|
|
{iri(EX.Subject), EX.predicate(), iri(EX.Object), EX.Graph}
|
|
|
|
)
|
|
|
|
|> description_includes_predication({EX.predicate(), iri(EX.Object)})
|
|
|
|
|
|
|
|
assert Description.add(description(), {EX.Subject, EX.predicate(), 42, nil})
|
|
|
|
|> description_includes_predication({EX.predicate(), literal(42)})
|
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "with a predicate-object tuple" do
|
|
|
|
assert Description.add(description(), {EX.predicate(), iri(EX.Object)})
|
2020-06-29 08:37:42 +00:00
|
|
|
|> description_includes_predication({EX.predicate(), iri(EX.Object)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "with a predicate-object tuple and a list of objects" do
|
|
|
|
desc = Description.add(description(), {EX.p(), [iri(EX.O1), iri(EX.O2)]})
|
|
|
|
assert description_includes_predication(desc, {EX.p(), iri(EX.O1)})
|
|
|
|
assert description_includes_predication(desc, {EX.p(), iri(EX.O2)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "with a list of predicate-object tuples" do
|
2020-06-29 08:37:42 +00:00
|
|
|
desc =
|
2020-07-27 21:09:23 +00:00
|
|
|
Description.add(description(), [
|
|
|
|
{EX.predicate(), EX.Object1},
|
|
|
|
{EX.predicate(), EX.Object2}
|
|
|
|
])
|
2020-06-29 08:37:42 +00:00
|
|
|
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object2)})
|
2020-07-27 21:09:23 +00:00
|
|
|
|
|
|
|
desc =
|
|
|
|
Description.add(description(), [
|
|
|
|
{EX.p1(), EX.O1},
|
|
|
|
{EX.p2(), [EX.O2, ~L"foo", "bar", 42]}
|
|
|
|
])
|
|
|
|
|
|
|
|
assert description_includes_predication(desc, {EX.p1(), iri(EX.O1)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), iri(EX.O2)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), ~L"foo"})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), ~L"bar"})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), RDF.literal(42)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "with a list of triples" do
|
2020-06-29 08:37:42 +00:00
|
|
|
desc =
|
|
|
|
Description.add(description(), [
|
|
|
|
{EX.Subject, EX.predicate1(), EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2(), EX.Object2}
|
|
|
|
])
|
|
|
|
|
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
2020-07-27 21:09:23 +00:00
|
|
|
|
|
|
|
desc =
|
|
|
|
Description.add(description(), [
|
|
|
|
{EX.Subject, EX.predicate1(), EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2(), [EX.Object2, EX.Object3]}
|
|
|
|
])
|
|
|
|
|
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object3)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a list of mixed triples and predicate-object-pairs" do
|
2020-06-29 08:37:42 +00:00
|
|
|
desc =
|
|
|
|
Description.add(description(), [
|
|
|
|
{EX.predicate(), EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate(), EX.Object2},
|
|
|
|
{EX.Other, EX.predicate(), EX.Object3}
|
|
|
|
])
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
2020-06-29 08:37:42 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object2)})
|
|
|
|
refute description_includes_predication(desc, {EX.predicate(), iri(EX.Object3)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "with a description map with coercible RDF terms" do
|
2020-06-29 08:37:42 +00:00
|
|
|
desc =
|
|
|
|
description([{EX.predicate1(), EX.Object1}, {EX.predicate2(), EX.Object2}])
|
|
|
|
|> Description.add(%{EX.predicate3() => EX.Object3})
|
2016-11-24 07:44:07 +00:00
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
2020-06-29 08:37:42 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate3(), iri(EX.Object3)})
|
|
|
|
|
|
|
|
desc =
|
|
|
|
Description.add(desc, %{
|
|
|
|
EX.predicate1() => EX.Object1,
|
|
|
|
EX.predicate2() => [EX.Object2, 42],
|
|
|
|
EX.predicate3() => [bnode(:foo)]
|
|
|
|
})
|
2016-11-24 07:44:07 +00:00
|
|
|
|
|
|
|
assert Description.count(desc) == 5
|
2020-06-29 08:37:42 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), literal(42)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate3(), iri(EX.Object3)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate3(), bnode(:foo)})
|
2016-11-24 07:44:07 +00:00
|
|
|
end
|
|
|
|
|
2017-08-11 20:22:27 +00:00
|
|
|
test "a map of predications with non-coercible RDF terms" do
|
2017-08-20 20:35:14 +00:00
|
|
|
assert_raise RDF.IRI.InvalidError, fn ->
|
|
|
|
Description.add(description(), %{"not a IRI" => iri(EX.Object)})
|
2016-11-24 07:44:07 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
assert_raise RDF.Literal.InvalidError, fn ->
|
2020-06-29 08:37:42 +00:00
|
|
|
Description.add(description(), %{EX.prop() => self()})
|
2016-11-24 07:44:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "with an empty map" do
|
|
|
|
assert Description.add(description(), %{}) == description()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with empty object lists" do
|
|
|
|
assert Description.add(description(), {EX.p(), []}) == description()
|
|
|
|
assert Description.add(description(), %{EX.p() => []}) == description()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with another description" do
|
|
|
|
desc =
|
|
|
|
description([{EX.predicate1(), EX.Object1}, {EX.predicate2(), EX.Object2}])
|
2020-09-25 14:07:37 +00:00
|
|
|
|> Description.add(
|
|
|
|
Description.new(EX.Other, init: {EX.Other, EX.predicate3(), EX.Object3})
|
|
|
|
)
|
2020-07-27 21:09:23 +00:00
|
|
|
|
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate3(), iri(EX.Object3)})
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
desc =
|
|
|
|
Description.add(
|
|
|
|
desc,
|
|
|
|
Description.new(EX.Other, init: {EX.Other, EX.predicate1(), EX.Object4})
|
|
|
|
)
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate3(), iri(EX.Object3)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object4)})
|
|
|
|
end
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
test "with a context" do
|
|
|
|
context =
|
|
|
|
PropertyMap.new(
|
|
|
|
p1: EX.p1(),
|
|
|
|
p2: EX.p2()
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Description.add(description(), {iri(EX.Subject), :p, literal(42)},
|
|
|
|
context: [p: EX.predicate()]
|
|
|
|
)
|
|
|
|
|> description_includes_predication({EX.predicate(), literal(42)})
|
|
|
|
|
|
|
|
assert Description.add(description(), {iri(EX.Subject), :p, literal(42), EX.Graph},
|
|
|
|
context: %{p: EX.predicate()}
|
|
|
|
)
|
|
|
|
|> description_includes_predication({EX.predicate(), literal(42)})
|
|
|
|
|
|
|
|
desc =
|
|
|
|
Description.add(
|
|
|
|
description(),
|
|
|
|
[
|
|
|
|
p1: EX.O1,
|
|
|
|
p2: [EX.O2, ~L"foo", "bar", 42]
|
|
|
|
],
|
|
|
|
context: context
|
|
|
|
)
|
|
|
|
|
|
|
|
assert description_includes_predication(desc, {EX.p1(), iri(EX.O1)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), iri(EX.O2)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), ~L"foo"})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), ~L"bar"})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), RDF.literal(42)})
|
|
|
|
|
|
|
|
desc =
|
|
|
|
Description.add(
|
|
|
|
description(),
|
|
|
|
%{
|
|
|
|
p1: EX.Object1,
|
|
|
|
p2: [EX.Object2, 42, bnode(:foo)]
|
|
|
|
},
|
|
|
|
context: context
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Description.count(desc) == 4
|
|
|
|
assert description_includes_predication(desc, {EX.p1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), literal(42)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), bnode(:foo)})
|
|
|
|
|
|
|
|
desc =
|
|
|
|
Description.add(
|
|
|
|
description(),
|
|
|
|
[
|
|
|
|
{:p1, EX.Object1},
|
|
|
|
{EX.Subject, :p2, EX.Object2},
|
|
|
|
%{p2: EX.Object3},
|
|
|
|
EX.predicate(EX.Other, EX.Object4)
|
|
|
|
],
|
|
|
|
context: context
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Description.count(desc) == 4
|
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.p1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), iri(EX.Object3)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object4)})
|
2020-10-11 09:42:30 +00:00
|
|
|
|
|
|
|
desc = Description.add(description(), [type: EX.Class], context: RDF.NS.RDF)
|
|
|
|
assert Description.count(desc) == 1
|
|
|
|
assert description_includes_predication(desc, {RDF.type(), iri(EX.Class)})
|
2020-10-09 14:32:24 +00:00
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "triples with another subject are ignored" do
|
|
|
|
assert empty_description(
|
|
|
|
Description.add(description(), {EX.Other, EX.predicate(), iri(EX.Object)})
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
test "duplicates are ignored" do
|
2020-06-29 08:37:42 +00:00
|
|
|
desc = Description.add(description(), {EX.predicate(), EX.Object})
|
|
|
|
assert Description.add(desc, {EX.predicate(), EX.Object}) == desc
|
|
|
|
assert Description.add(desc, {EX.Subject, EX.predicate(), EX.Object}) == desc
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
desc = Description.add(description(), {EX.predicate(), 42})
|
|
|
|
assert Description.add(desc, {EX.predicate(), literal(42)}) == desc
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "coercion" do
|
|
|
|
assert Description.add(description(), {EX.Subject, EX.P, EX.O})
|
|
|
|
|> description_includes_predication({iri(EX.P), iri(EX.O)})
|
|
|
|
|
|
|
|
assert Description.add(description(), {"http://example.com/predicate", EX.Object})
|
|
|
|
|> description_includes_predication({EX.predicate(), iri(EX.Object)})
|
|
|
|
|
|
|
|
desc = Description.add(description(), {"http://example.com/predicate", [42, true]})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), literal(42)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), literal(true)})
|
|
|
|
end
|
|
|
|
|
2017-08-11 20:22:27 +00:00
|
|
|
test "non-coercible Triple elements are causing an error" do
|
2017-08-20 20:35:14 +00:00
|
|
|
assert_raise RDF.IRI.InvalidError, fn ->
|
|
|
|
Description.add(description(), {"not a IRI", iri(EX.Object)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
assert_raise RDF.Literal.InvalidError, fn ->
|
2020-06-29 08:37:42 +00:00
|
|
|
Description.add(description(), {EX.prop(), self()})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
end
|
2020-10-05 10:41:55 +00:00
|
|
|
|
|
|
|
test "structs are causing an error" do
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.add(description(), Date.utc_today())
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.add(description(), RDF.graph())
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.add(description(), RDF.dataset())
|
|
|
|
end
|
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
describe "put/3" do
|
|
|
|
test "with a triple" do
|
2020-10-04 00:52:56 +00:00
|
|
|
desc =
|
|
|
|
description({iri(EX.Subject), EX.predicate(), iri(EX.Object1)})
|
|
|
|
|> Description.put({iri(EX.Subject), EX.predicate(), iri(EX.Object2)})
|
|
|
|
|
|
|
|
assert Description.count(desc) == 1
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object2)})
|
2020-07-27 21:09:23 +00:00
|
|
|
end
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
test "with a quad" do
|
|
|
|
desc =
|
|
|
|
description({iri(EX.Subject), EX.predicate(), iri(EX.Object1)})
|
|
|
|
|> Description.put({iri(EX.Subject), EX.predicate(), iri(EX.Object2), EX.Graph})
|
|
|
|
|
|
|
|
assert Description.count(desc) == 1
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object2)})
|
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "with a predicate-object tuple" do
|
2020-10-04 00:52:56 +00:00
|
|
|
desc =
|
|
|
|
description({iri(EX.Subject), EX.p(), iri(EX.O1)})
|
|
|
|
|> Description.put({EX.p(), [iri(EX.O2), iri(EX.O3)]})
|
|
|
|
|
|
|
|
assert Description.count(desc) == 2
|
2020-07-27 21:09:23 +00:00
|
|
|
assert description_includes_predication(desc, {EX.p(), iri(EX.O2)})
|
2020-10-04 00:52:56 +00:00
|
|
|
assert description_includes_predication(desc, {EX.p(), iri(EX.O3)})
|
2020-07-27 21:09:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of predicate-object tuples" do
|
|
|
|
desc =
|
2020-10-04 00:52:56 +00:00
|
|
|
description({iri(EX.Subject), EX.p2(), iri(EX.O1)})
|
|
|
|
|> Description.put([
|
2020-07-27 21:09:23 +00:00
|
|
|
{EX.p1(), EX.O1},
|
|
|
|
{EX.p2(), [EX.O2]},
|
|
|
|
{EX.p2(), [~L"foo", "bar", 42]}
|
|
|
|
])
|
|
|
|
|
2020-10-04 00:52:56 +00:00
|
|
|
assert Description.count(desc) == 5
|
2020-07-27 21:09:23 +00:00
|
|
|
assert description_includes_predication(desc, {EX.p1(), iri(EX.O1)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), iri(EX.O2)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), ~L"foo"})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), ~L"bar"})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), RDF.literal(42)})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of triples" do
|
|
|
|
desc =
|
2020-10-04 00:52:56 +00:00
|
|
|
description([
|
|
|
|
{iri(EX.Subject), EX.predicate1(), iri(EX.Object)},
|
|
|
|
{iri(EX.Subject), EX.predicate2(), iri(EX.Object)}
|
|
|
|
])
|
|
|
|
|> Description.put([
|
2020-07-27 21:09:23 +00:00
|
|
|
{EX.Subject, EX.predicate1(), EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2(), [EX.Object2, EX.Object3]},
|
|
|
|
{EX.Subject, EX.predicate2(), [EX.Object4]}
|
|
|
|
])
|
|
|
|
|
2020-10-04 00:52:56 +00:00
|
|
|
assert Description.count(desc) == 4
|
2020-07-27 21:09:23 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object3)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object4)})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a description map with coercible RDF terms" do
|
|
|
|
desc =
|
|
|
|
description([{EX.predicate1(), EX.Object1}, {EX.predicate2(), EX.Object2}])
|
|
|
|
|> Description.put(%{
|
|
|
|
EX.predicate2() => [EX.Object3, 42],
|
|
|
|
EX.predicate3() => bnode(:foo)
|
|
|
|
})
|
|
|
|
|
|
|
|
assert Description.count(desc) == 4
|
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object3)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), literal(42)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate3(), bnode(:foo)})
|
|
|
|
refute description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with an empty map" do
|
|
|
|
desc = description([{EX.predicate(), EX.Object}])
|
|
|
|
assert Description.put(desc, %{}) == desc
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a description on the same subject" do
|
|
|
|
desc =
|
|
|
|
description([{EX.predicate1(), EX.Object1}, {EX.predicate2(), EX.Object2}])
|
|
|
|
|> Description.put(
|
|
|
|
description([
|
|
|
|
{EX.predicate1(), EX.Object4},
|
|
|
|
{EX.predicate3(), EX.Object3}
|
|
|
|
])
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Description.count(desc) == 3
|
|
|
|
assert description_includes_predication(desc, {EX.predicate1(), iri(EX.Object4)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate3(), iri(EX.Object3)})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a description on another subject" do
|
|
|
|
desc = description([{EX.predicate1(), EX.Object1}, {EX.predicate2(), EX.Object2}])
|
|
|
|
|
|
|
|
assert Description.put(
|
|
|
|
desc,
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.Other, init: {EX.Other, EX.predicate(), iri(EX.Object)})
|
2020-07-27 21:09:23 +00:00
|
|
|
) == desc
|
|
|
|
end
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
test "with a context" do
|
|
|
|
desc =
|
|
|
|
Description.put(
|
|
|
|
description(),
|
|
|
|
[
|
|
|
|
{:p1, EX.Object1},
|
|
|
|
{EX.Subject, :p2, EX.Object2},
|
|
|
|
%{p3: EX.Object3},
|
|
|
|
EX.predicate(EX.Other, EX.Object4)
|
|
|
|
],
|
|
|
|
context: [
|
|
|
|
p1: EX.p1(),
|
|
|
|
p2: EX.p2(),
|
|
|
|
p3: EX.p3()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Description.count(desc) == 4
|
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.p1(), iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.p2(), iri(EX.Object2)})
|
|
|
|
assert description_includes_predication(desc, {EX.p3(), iri(EX.Object3)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate(), iri(EX.Object4)})
|
|
|
|
end
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
test "triples with another subject are ignored" do
|
|
|
|
assert empty_description(
|
|
|
|
Description.put(description(), {EX.Other, EX.predicate(), iri(EX.Object)})
|
|
|
|
)
|
|
|
|
end
|
2020-10-05 10:41:55 +00:00
|
|
|
|
|
|
|
test "structs are causing an error" do
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.put(description(), Date.utc_today())
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.put(description(), RDF.graph())
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.put(description(), RDF.dataset())
|
|
|
|
end
|
|
|
|
end
|
2020-07-27 21:09:23 +00:00
|
|
|
end
|
|
|
|
|
2017-05-23 00:03:41 +00:00
|
|
|
describe "delete" do
|
|
|
|
setup do
|
|
|
|
{:ok,
|
2020-06-29 08:37:42 +00:00
|
|
|
empty_description: Description.new(EX.S),
|
2020-09-25 14:07:37 +00:00
|
|
|
description1: Description.new(EX.S, init: {EX.S, EX.p(), EX.O}),
|
|
|
|
description2: Description.new(EX.S, init: {EX.S, EX.p(), [EX.O1, EX.O2]}),
|
2020-06-29 08:37:42 +00:00
|
|
|
description3:
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S,
|
|
|
|
init: [
|
|
|
|
{EX.p1(), [EX.O1, EX.O2]},
|
|
|
|
{EX.p2(), EX.O3},
|
|
|
|
{EX.p3(), [~B<foo>, ~L"bar"]}
|
|
|
|
]
|
|
|
|
)}
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
test "predicate-object tuples",
|
2020-06-29 08:37:42 +00:00
|
|
|
%{
|
|
|
|
empty_description: empty_description,
|
|
|
|
description1: description1,
|
|
|
|
description2: description2
|
|
|
|
} do
|
|
|
|
assert Description.delete(empty_description, {EX.p(), EX.O}) == empty_description
|
|
|
|
assert Description.delete(description1, {EX.p(), EX.O}) == empty_description
|
|
|
|
|
|
|
|
assert Description.delete(description2, {EX.p(), EX.O2}) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: {EX.S, EX.p(), EX.O1})
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
test "statements",
|
2020-06-29 08:37:42 +00:00
|
|
|
%{
|
|
|
|
empty_description: empty_description,
|
|
|
|
description1: description1,
|
|
|
|
description2: description2
|
|
|
|
} do
|
|
|
|
assert Description.delete(empty_description, {EX.S, EX.p(), EX.O}) == empty_description
|
|
|
|
assert Description.delete(description1, {EX.S, EX.p(), EX.O}) == empty_description
|
2020-10-04 23:28:24 +00:00
|
|
|
assert Description.delete(description1, {EX.S, EX.p(), EX.O, EX.Graph}) == empty_description
|
2020-06-29 08:37:42 +00:00
|
|
|
|
|
|
|
assert Description.delete(description2, {EX.S, EX.p(), EX.O2}) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: {EX.S, EX.p(), EX.O1})
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
test "statements with another subject",
|
2020-06-29 08:37:42 +00:00
|
|
|
%{
|
|
|
|
empty_description: empty_description,
|
|
|
|
description1: description1,
|
|
|
|
description2: description2
|
|
|
|
} do
|
|
|
|
assert Description.delete(empty_description, {EX.Other, EX.p(), EX.O}) == empty_description
|
|
|
|
assert Description.delete(description1, {EX.Other, EX.p(), EX.O}) == description1
|
|
|
|
assert Description.delete(description2, {EX.Other, EX.p(), EX.O2}) == description2
|
2020-10-04 23:28:24 +00:00
|
|
|
assert Description.delete(description2, {EX.Other, EX.p(), EX.O2, EX.Graph}) == description2
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
test "predicate-object tuples with object lists",
|
2020-06-29 08:37:42 +00:00
|
|
|
%{
|
|
|
|
empty_description: empty_description,
|
|
|
|
description1: description1,
|
|
|
|
description2: description2
|
|
|
|
} do
|
|
|
|
assert Description.delete(empty_description, {EX.p(), [EX.O1, EX.O2]}) == empty_description
|
|
|
|
assert Description.delete(description1, {EX.p(), [EX.O, EX.O2]}) == empty_description
|
|
|
|
assert Description.delete(description2, {EX.p(), [EX.O1, EX.O2]}) == empty_description
|
2020-10-04 23:28:24 +00:00
|
|
|
assert Description.delete(description2, {EX.p(), [EX.O1, EX.O2]}) == empty_description
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
test "list of statements",
|
2020-06-29 08:37:42 +00:00
|
|
|
%{empty_description: empty_description, description3: description3} do
|
|
|
|
assert Description.delete(empty_description, [{EX.p(), [EX.O1, EX.O2]}]) ==
|
|
|
|
empty_description
|
|
|
|
|
2017-05-23 00:03:41 +00:00
|
|
|
assert Description.delete(description3, [
|
2020-06-29 08:37:42 +00:00
|
|
|
{EX.p1(), EX.O1},
|
|
|
|
{EX.p2(), [EX.O2, EX.O3]},
|
2020-10-04 23:28:24 +00:00
|
|
|
{EX.S, EX.p3(), [~B<foo>]},
|
|
|
|
{EX.S, EX.p3(), ~L"bar", EX.Graph}
|
2020-09-25 14:07:37 +00:00
|
|
|
]) == Description.new(EX.S, init: {EX.S, EX.p1(), EX.O2})
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
test "description maps",
|
2020-06-29 08:37:42 +00:00
|
|
|
%{empty_description: empty_description, description3: description3} do
|
|
|
|
assert Description.delete(empty_description, %{EX.p() => EX.O1}) == empty_description
|
|
|
|
|
2017-05-23 00:03:41 +00:00
|
|
|
assert Description.delete(description3, %{
|
2020-06-29 08:37:42 +00:00
|
|
|
EX.p1() => EX.O1,
|
|
|
|
EX.p2() => [EX.O2, EX.O3],
|
|
|
|
EX.p3() => [~B<foo>, ~L"bar"]
|
2020-09-25 14:07:37 +00:00
|
|
|
}) == Description.new(EX.S, init: {EX.S, EX.p1(), EX.O2})
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
2017-06-02 13:12:53 +00:00
|
|
|
|
2020-10-04 23:28:24 +00:00
|
|
|
test "another description",
|
2020-06-29 08:37:42 +00:00
|
|
|
%{
|
|
|
|
empty_description: empty_description,
|
|
|
|
description1: description1,
|
|
|
|
description3: description3
|
|
|
|
} do
|
2017-06-02 13:12:53 +00:00
|
|
|
assert Description.delete(empty_description, description1) == empty_description
|
2020-06-29 08:37:42 +00:00
|
|
|
|
|
|
|
assert Description.delete(
|
|
|
|
description3,
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S,
|
|
|
|
init: %{
|
|
|
|
EX.p1() => EX.O1,
|
|
|
|
EX.p2() => [EX.O2, EX.O3],
|
|
|
|
EX.p3() => [~B<foo>, ~L"bar"]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
) == Description.new(EX.S, init: {EX.S, EX.p1(), EX.O2})
|
2017-06-02 13:12:53 +00:00
|
|
|
end
|
2020-10-05 10:41:55 +00:00
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
test "with a context", %{description3: description3} do
|
|
|
|
desc =
|
|
|
|
Description.delete(
|
|
|
|
description3,
|
|
|
|
[
|
|
|
|
{:p1, EX.O1},
|
|
|
|
{EX.S, :p2, EX.O3},
|
|
|
|
%{p3: ~B<foo>},
|
|
|
|
EX.p3(EX.S, ~L"bar")
|
|
|
|
],
|
|
|
|
context: [
|
|
|
|
p1: EX.p1(),
|
|
|
|
p2: EX.p2(),
|
|
|
|
p3: EX.p3()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Description.count(desc) == 1
|
|
|
|
assert description_of_subject(desc, iri(EX.S))
|
|
|
|
assert description_includes_predication(desc, {EX.p1(), iri(EX.O2)})
|
|
|
|
end
|
|
|
|
|
2020-10-05 10:41:55 +00:00
|
|
|
test "structs are causing an error" do
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.delete(description(), Date.utc_today())
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.delete(description(), RDF.graph())
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.delete(description(), RDF.dataset())
|
|
|
|
end
|
|
|
|
end
|
2017-05-23 00:03:41 +00:00
|
|
|
end
|
|
|
|
|
2017-06-02 16:06:22 +00:00
|
|
|
describe "delete_predicates" do
|
|
|
|
setup do
|
|
|
|
{:ok,
|
2020-06-29 08:37:42 +00:00
|
|
|
empty_description: Description.new(EX.S),
|
2020-09-25 14:07:37 +00:00
|
|
|
description1: Description.new(EX.S, init: {EX.S, EX.p(), [EX.O1, EX.O2]}),
|
2020-06-29 08:37:42 +00:00
|
|
|
description2:
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S,
|
|
|
|
init: [
|
|
|
|
{EX.P1, [EX.O1, EX.O2]},
|
|
|
|
{EX.p2(), [~B<foo>, ~L"bar"]}
|
|
|
|
]
|
|
|
|
)}
|
2017-06-02 16:06:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a single property",
|
2020-06-29 08:37:42 +00:00
|
|
|
%{
|
|
|
|
empty_description: empty_description,
|
|
|
|
description1: description1,
|
|
|
|
description2: description2
|
|
|
|
} do
|
|
|
|
assert Description.delete_predicates(description1, EX.p()) == empty_description
|
|
|
|
|
2017-06-02 16:06:22 +00:00
|
|
|
assert Description.delete_predicates(description2, EX.P1) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: {EX.S, EX.p2(), [~B<foo>, ~L"bar"]})
|
2017-06-02 16:06:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a list of properties",
|
2020-06-29 08:37:42 +00:00
|
|
|
%{
|
|
|
|
empty_description: empty_description,
|
|
|
|
description1: description1,
|
|
|
|
description2: description2
|
|
|
|
} do
|
|
|
|
assert Description.delete_predicates(description1, [EX.p()]) == empty_description
|
|
|
|
|
|
|
|
assert Description.delete_predicates(description2, [EX.P1, EX.p2(), EX.p3()]) ==
|
|
|
|
empty_description
|
2017-06-02 16:06:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-23 15:31:21 +00:00
|
|
|
describe "update/4" do
|
|
|
|
test "list values returned from the update function become new coerced objects of the predicate" do
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.S, init: {EX.S, EX.P, [EX.O1, EX.O2]})
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Description.update(
|
|
|
|
EX.P,
|
|
|
|
fn [_object | other] -> [EX.O3 | other] end
|
|
|
|
) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: {EX.S, EX.P, [EX.O3, EX.O2]})
|
2019-10-23 15:31:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "single values returned from the update function becomes new object of the predicate" do
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.S, init: {EX.S, EX.P, [EX.O1, EX.O2]})
|
2019-10-23 15:31:21 +00:00
|
|
|
|> Description.update(EX.P, fn _ -> EX.O3 end) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: {EX.S, EX.P, EX.O3})
|
2019-10-23 15:31:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "returning an empty list or nil from the update function causes a removal of the predications" do
|
2020-06-29 08:37:42 +00:00
|
|
|
description =
|
|
|
|
EX.S
|
|
|
|
|> EX.p(EX.O1, EX.O2)
|
|
|
|
|
2019-10-23 15:31:21 +00:00
|
|
|
assert description
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Description.update(EX.p(), fn _ -> [] end) ==
|
2020-07-27 21:09:23 +00:00
|
|
|
Description.new(EX.S)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2019-10-23 15:31:21 +00:00
|
|
|
assert description
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Description.update(EX.p(), fn _ -> nil end) ==
|
2020-07-27 21:09:23 +00:00
|
|
|
Description.new(EX.S)
|
2019-10-23 15:31:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "when the property is not present the initial object value is added for the predicate and the update function not called" do
|
|
|
|
fun = fn _ -> raise "should not be called" end
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.S) |> Description.update(EX.P, EX.O, fun) ==
|
|
|
|
Description.new(EX.S, init: {EX.S, EX.P, EX.O})
|
2019-10-23 15:31:21 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.S) |> Description.update(EX.P, fun) ==
|
2019-10-23 15:31:21 +00:00
|
|
|
Description.new(EX.S)
|
|
|
|
end
|
|
|
|
end
|
2017-06-02 16:06:22 +00:00
|
|
|
|
2017-03-31 14:27:34 +00:00
|
|
|
test "pop" do
|
2016-10-15 16:26:56 +00:00
|
|
|
assert Description.pop(Description.new(EX.S)) == {nil, Description.new(EX.S)}
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
{triple, desc} = Description.new(EX.S, init: {EX.S, EX.p(), EX.O}) |> Description.pop()
|
2020-06-29 08:37:42 +00:00
|
|
|
assert {iri(EX.S), iri(EX.p()), iri(EX.O)} == triple
|
2016-10-15 16:26:56 +00:00
|
|
|
assert Enum.count(desc.predications) == 0
|
|
|
|
|
|
|
|
{{subject, predicate, _}, desc} =
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: [{EX.S, EX.p(), EX.O1}, {EX.S, EX.p(), EX.O2}])
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Description.pop()
|
|
|
|
|
|
|
|
assert {subject, predicate} == {iri(EX.S), iri(EX.p())}
|
2016-10-15 16:26:56 +00:00
|
|
|
assert Enum.count(desc.predications) == 1
|
|
|
|
|
|
|
|
{{subject, _, _}, desc} =
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: [{EX.S, EX.p1(), EX.O1}, {EX.S, EX.p2(), EX.O2}])
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Description.pop()
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
assert subject == iri(EX.S)
|
2016-10-15 16:26:56 +00:00
|
|
|
assert Enum.count(desc.predications) == 1
|
|
|
|
end
|
|
|
|
|
2020-10-09 14:32:24 +00:00
|
|
|
describe "include?/3" do
|
2020-10-05 10:41:55 +00:00
|
|
|
test "valid cases" do
|
|
|
|
desc =
|
|
|
|
Description.new(EX.S,
|
|
|
|
init: [
|
|
|
|
{EX.p1(), [EX.O1, EX.O2]},
|
|
|
|
{EX.p2(), EX.O3},
|
|
|
|
{EX.p3(), [~B<foo>, ~L"bar"]}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Description.include?(desc, {EX.S, EX.p1(), EX.O2})
|
|
|
|
assert Description.include?(desc, {EX.S, EX.p1(), EX.O2, EX.Graph})
|
|
|
|
assert Description.include?(desc, {EX.p1(), EX.O2})
|
|
|
|
assert Description.include?(desc, {EX.p1(), [EX.O1, EX.O2]})
|
|
|
|
assert Description.include?(desc, [{EX.p1(), [EX.O1]}, {EX.p2(), EX.O3}])
|
|
|
|
assert Description.include?(desc, %{EX.p1() => [EX.O1, EX.O2], EX.p2() => EX.O3})
|
|
|
|
|
|
|
|
assert Description.include?(
|
|
|
|
desc,
|
|
|
|
Description.new(EX.S, init: %{EX.p1() => [EX.O1, EX.O2], EX.p2() => EX.O3})
|
|
|
|
)
|
|
|
|
|
|
|
|
refute Description.include?(desc, {EX.p4(), EX.O1})
|
|
|
|
refute Description.include?(desc, {EX.p1(), EX.O3})
|
|
|
|
refute Description.include?(desc, {EX.p1(), [EX.O1, EX.O3]})
|
2020-10-09 14:32:24 +00:00
|
|
|
|
|
|
|
assert Description.include?(
|
|
|
|
desc,
|
|
|
|
%{
|
|
|
|
p1: [EX.O1, EX.O2],
|
|
|
|
p2: EX.O3,
|
|
|
|
p3: [~B<foo>, "bar"]
|
|
|
|
},
|
|
|
|
context: %{p1: EX.p1(), p2: EX.p2(), p3: EX.p3()}
|
|
|
|
)
|
2020-10-05 10:41:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "structs are causing an error" do
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.include?(description(), Date.utc_today())
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.include?(description(), RDF.graph())
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise struct_not_allowed_as_input_error(), fn ->
|
|
|
|
Description.include?(description(), RDF.dataset())
|
|
|
|
end
|
|
|
|
end
|
2020-07-27 21:09:23 +00:00
|
|
|
end
|
|
|
|
|
2018-10-27 14:30:19 +00:00
|
|
|
test "values/1" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Description.new(EX.s()) |> Description.values() == %{}
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.s(), init: {EX.s(), EX.p(), ~L"Foo"}) |> Description.values() ==
|
2020-06-29 08:37:42 +00:00
|
|
|
%{RDF.Term.value(EX.p()) => ["Foo"]}
|
2018-11-04 21:27:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "values/2" do
|
2020-10-10 13:45:25 +00:00
|
|
|
assert Description.new(EX.s(), init: {EX.s(), EX.p(), ~L"Foo"})
|
|
|
|
|> Description.values(PropertyMap.new(p: EX.p())) ==
|
|
|
|
%{p: ["Foo"]}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "map/2" do
|
2018-11-04 21:27:25 +00:00
|
|
|
mapping = fn
|
|
|
|
{:predicate, predicate} ->
|
|
|
|
predicate |> to_string() |> String.split("/") |> List.last() |> String.to_atom()
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2018-11-04 21:27:25 +00:00
|
|
|
{_, term} ->
|
|
|
|
RDF.Term.value(term)
|
|
|
|
end
|
|
|
|
|
2020-10-10 13:45:25 +00:00
|
|
|
assert Description.new(EX.s()) |> Description.map(mapping) == %{}
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-10-10 13:45:25 +00:00
|
|
|
assert Description.new(EX.s(), init: {EX.s(), EX.p(), ~L"Foo"}) |> Description.map(mapping) ==
|
2018-11-04 21:27:25 +00:00
|
|
|
%{p: ["Foo"]}
|
2018-10-27 14:30:19 +00:00
|
|
|
end
|
|
|
|
|
2019-10-15 15:29:46 +00:00
|
|
|
describe "take/2" do
|
|
|
|
test "with a non-empty property list" do
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.S, init: [{EX.S, EX.p1(), EX.O1}, {EX.S, EX.p2(), EX.O2}])
|
2020-06-29 08:37:42 +00:00
|
|
|
|> Description.take([EX.p2(), EX.p3()]) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: {EX.S, EX.p2(), EX.O2})
|
2019-10-15 15:29:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with an empty property list" do
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.S, init: [{EX.S, EX.p1(), EX.O1}, {EX.S, EX.p2(), EX.O2}])
|
2019-10-15 15:29:46 +00:00
|
|
|
|> Description.take([]) == Description.new(EX.S)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with nil" do
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.S, init: [{EX.S, EX.p1(), EX.O1}, {EX.S, EX.p2(), EX.O2}])
|
2019-10-15 15:29:46 +00:00
|
|
|
|> Description.take(nil) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: [{EX.S, EX.p1(), EX.O1}, {EX.S, EX.p2(), EX.O2}])
|
2019-10-15 15:29:46 +00:00
|
|
|
end
|
2019-10-14 07:13:56 +00:00
|
|
|
end
|
|
|
|
|
2019-04-02 22:12:42 +00:00
|
|
|
test "equal/2" do
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.S, init: {EX.S, EX.p(), EX.O})
|
|
|
|
|> Description.equal?(Description.new(EX.S, init: {EX.S, EX.p(), EX.O}))
|
2020-06-29 08:37:42 +00:00
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
refute Description.new(EX.S, init: {EX.S, EX.p(), EX.O})
|
|
|
|
|> Description.equal?(Description.new(EX.S, init: {EX.S, EX.p(), EX.O2}))
|
2019-04-02 22:12:42 +00:00
|
|
|
end
|
|
|
|
|
2016-11-15 00:26:57 +00:00
|
|
|
describe "Enumerable protocol" do
|
2016-10-15 16:26:56 +00:00
|
|
|
test "Enum.count" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Enum.count(Description.new(EX.foo())) == 0
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Enum.count(Description.new(EX.S, init: {EX.S, EX.p(), EX.O})) == 1
|
2020-07-27 21:09:23 +00:00
|
|
|
|
|
|
|
assert Enum.count(
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.S, init: [{EX.S, EX.p(), EX.O1}, {EX.S, EX.p(), EX.O2}])
|
2020-07-27 21:09:23 +00:00
|
|
|
) == 2
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "Enum.member?" do
|
2020-06-29 08:37:42 +00:00
|
|
|
refute Enum.member?(Description.new(EX.S), {iri(EX.S), EX.p(), iri(EX.O)})
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Enum.member?(Description.new(EX.S, init: {EX.S, EX.p(), EX.O}), {EX.S, EX.p(), EX.O})
|
2020-06-29 08:37:42 +00:00
|
|
|
|
|
|
|
desc =
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.Subject,
|
|
|
|
init: [
|
|
|
|
{EX.Subject, EX.predicate1(), EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2(), EX.Object2},
|
|
|
|
{EX.predicate2(), EX.Object3}
|
|
|
|
]
|
|
|
|
)
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Enum.member?(desc, {EX.Subject, EX.predicate1(), EX.Object1})
|
|
|
|
assert Enum.member?(desc, {EX.Subject, EX.predicate2(), EX.Object2})
|
|
|
|
assert Enum.member?(desc, {EX.Subject, EX.predicate2(), EX.Object3})
|
|
|
|
refute Enum.member?(desc, {EX.Subject, EX.predicate1(), EX.Object2})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "Enum.reduce" do
|
2020-06-29 08:37:42 +00:00
|
|
|
desc =
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.Subject,
|
|
|
|
init: [
|
|
|
|
{EX.Subject, EX.predicate1(), EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2(), EX.Object2},
|
|
|
|
{EX.predicate2(), EX.Object3}
|
|
|
|
]
|
|
|
|
)
|
2020-06-29 08:37:42 +00:00
|
|
|
|
|
|
|
assert desc ==
|
|
|
|
Enum.reduce(desc, description(), fn triple, acc ->
|
|
|
|
acc |> Description.add(triple)
|
|
|
|
end)
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-30 10:40:44 +00:00
|
|
|
describe "Collectable protocol" do
|
|
|
|
test "with a map" do
|
|
|
|
map = %{
|
2020-06-29 08:37:42 +00:00
|
|
|
EX.predicate1() => EX.Object1,
|
|
|
|
EX.predicate2() => EX.Object2
|
|
|
|
}
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
assert Enum.into(map, Description.new(EX.Subject)) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.Subject, init: map)
|
2017-10-30 10:40:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of triples" do
|
|
|
|
triples = [
|
2020-06-29 08:37:42 +00:00
|
|
|
{EX.Subject, EX.predicate1(), EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2(), EX.Object2}
|
|
|
|
]
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
assert Enum.into(triples, Description.new(EX.Subject)) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.Subject, init: triples)
|
2017-10-30 10:40:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of predicate-object pairs" do
|
|
|
|
pairs = [
|
2020-06-29 08:37:42 +00:00
|
|
|
{EX.predicate1(), EX.Object1},
|
|
|
|
{EX.predicate2(), EX.Object2}
|
|
|
|
]
|
|
|
|
|
2020-07-27 21:09:23 +00:00
|
|
|
assert Enum.into(pairs, Description.new(EX.Subject)) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.Subject, init: pairs)
|
2017-10-30 10:40:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of lists" do
|
|
|
|
lists = [
|
2020-06-29 08:37:42 +00:00
|
|
|
[EX.Subject, EX.predicate1(), EX.Object1],
|
|
|
|
[EX.Subject, EX.predicate2(), EX.Object2]
|
|
|
|
]
|
|
|
|
|
2017-10-30 10:40:44 +00:00
|
|
|
assert Enum.into(lists, Description.new(EX.Subject)) ==
|
2020-09-25 14:07:37 +00:00
|
|
|
Description.new(EX.Subject, init: Enum.map(lists, &List.to_tuple/1))
|
2017-10-30 10:40:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
describe "Access behaviour" do
|
|
|
|
test "access with the [] operator" do
|
2020-06-29 08:37:42 +00:00
|
|
|
assert Description.new(EX.Subject)[EX.predicate()] == nil
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.Subject, init: {EX.Subject, EX.predicate(), EX.Object})[
|
|
|
|
EX.predicate()
|
|
|
|
] == [
|
2020-06-29 08:37:42 +00:00
|
|
|
iri(EX.Object)
|
|
|
|
]
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.Subject, init: {EX.Subject, EX.Predicate, EX.Object})[
|
|
|
|
EX.Predicate
|
|
|
|
] == [
|
2020-06-29 08:37:42 +00:00
|
|
|
iri(EX.Object)
|
|
|
|
]
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.Subject, init: {EX.Subject, EX.predicate(), EX.Object})[
|
2020-06-29 08:37:42 +00:00
|
|
|
"http://example.com/predicate"
|
|
|
|
] == [iri(EX.Object)]
|
|
|
|
|
2020-09-25 14:07:37 +00:00
|
|
|
assert Description.new(EX.Subject,
|
|
|
|
init: [
|
|
|
|
{EX.Subject, EX.predicate1(), EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate1(), EX.Object2},
|
|
|
|
{EX.Subject, EX.predicate2(), EX.Object3}
|
|
|
|
]
|
|
|
|
)[EX.predicate1()] ==
|
2020-06-29 08:37:42 +00:00
|
|
|
[iri(EX.Object1), iri(EX.Object2)]
|
2016-11-11 15:40:58 +00:00
|
|
|
end
|
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|