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
|
|
|
|
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
|
2016-10-15 16:26:56 +00:00
|
|
|
assert description_of_subject(Description.new("http://example.com/description/subject"),
|
2017-08-20 20:35:14 +00:00
|
|
|
~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
|
|
|
|
|
2017-03-31 14:27:34 +00:00
|
|
|
test "with a single initial triple" do
|
|
|
|
desc = Description.new({EX.Subject, EX.predicate, EX.Object})
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.predicate, iri(EX.Object)})
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-03-31 14:27:34 +00:00
|
|
|
desc = Description.new(EX.Subject, EX.predicate, 42)
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
2017-03-31 14:27:34 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate, literal(42)})
|
|
|
|
end
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-03-31 14:27:34 +00:00
|
|
|
test "with a list of initial triples" do
|
|
|
|
desc = Description.new([{EX.Subject, EX.predicate1, EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2, EX.Object2}])
|
2017-08-20 20:35:14 +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)})
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-03-31 14:27:34 +00:00
|
|
|
desc = Description.new(EX.Subject, EX.predicate, [EX.Object, bnode(:foo), "bar"])
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {EX.predicate, iri(EX.Object)})
|
2017-03-31 14:27:34 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate, bnode(:foo)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate, literal("bar")})
|
|
|
|
end
|
2016-11-15 00:26:57 +00:00
|
|
|
|
2017-03-31 14:27:34 +00:00
|
|
|
test "from another description" do
|
|
|
|
desc1 = Description.new({EX.Other, EX.predicate, EX.Object})
|
|
|
|
desc2 = Description.new(EX.Subject, desc1)
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_of_subject(desc2, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc2, {EX.predicate, iri(EX.Object)})
|
2017-03-31 14:27:34 +00:00
|
|
|
end
|
|
|
|
|
2017-08-11 20:22:27 +00:00
|
|
|
test "from a map with coercible RDF term" do
|
2017-03-31 14:27:34 +00:00
|
|
|
desc = Description.new(EX.Subject, %{EX.Predicate => EX.Object})
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_of_subject(desc, iri(EX.Subject))
|
|
|
|
assert description_includes_predication(desc, {iri(EX.Predicate), iri(EX.Object)})
|
2017-03-31 14:27:34 +00:00
|
|
|
end
|
2017-05-25 13:29:39 +00:00
|
|
|
|
|
|
|
test "with another description as subject, it performs and add " do
|
|
|
|
desc = Description.new({EX.S, EX.p, EX.O})
|
|
|
|
|
|
|
|
assert Description.new(desc, EX.p2, EX.O2) ==
|
|
|
|
Description.add(desc, EX.p2, EX.O2)
|
|
|
|
assert Description.new(desc, EX.p, [EX.O1, EX.O2]) ==
|
|
|
|
Description.add(desc, EX.p, [EX.O1, EX.O2])
|
|
|
|
end
|
2016-11-24 07:44:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
describe "add" do
|
2016-10-15 16:26:56 +00:00
|
|
|
test "a predicate-object-pair of proper RDF terms" do
|
2017-08-20 20:35:14 +00:00
|
|
|
assert Description.add(description(), EX.predicate, iri(EX.Object))
|
|
|
|
|> description_includes_predication({EX.predicate, iri(EX.Object)})
|
|
|
|
assert Description.add(description(), {EX.predicate, iri(EX.Object)})
|
|
|
|
|> description_includes_predication({EX.predicate, iri(EX.Object)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
2017-08-11 20:22:27 +00:00
|
|
|
test "a predicate-object-pair of coercible RDF terms" do
|
2017-02-12 14:42:27 +00:00
|
|
|
assert Description.add(description(),
|
2017-08-20 20:35:14 +00:00
|
|
|
"http://example.com/predicate", iri(EX.Object))
|
|
|
|
|> description_includes_predication({EX.predicate, iri(EX.Object)})
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-02-12 14:42:27 +00:00
|
|
|
assert Description.add(description(),
|
2017-02-13 00:21:32 +00:00
|
|
|
{"http://example.com/predicate", 42})
|
2016-10-15 16:26:56 +00:00
|
|
|
|> description_includes_predication({EX.predicate, literal(42)})
|
|
|
|
|
2017-07-02 17:48:38 +00:00
|
|
|
assert Description.add(description(),
|
|
|
|
{"http://example.com/predicate", true})
|
|
|
|
|> description_includes_predication({EX.predicate, literal(true)})
|
|
|
|
|
2017-02-12 14:42:27 +00:00
|
|
|
assert Description.add(description(),
|
2017-02-13 00:21:32 +00:00
|
|
|
{"http://example.com/predicate", bnode(:foo)})
|
2016-10-15 16:26:56 +00:00
|
|
|
|> description_includes_predication({EX.predicate, bnode(:foo)})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a proper triple" do
|
2017-02-12 14:42:27 +00:00
|
|
|
assert Description.add(description(),
|
2017-08-20 20:35:14 +00:00
|
|
|
{iri(EX.Subject), EX.predicate, iri(EX.Object)})
|
|
|
|
|> description_includes_predication({EX.predicate, iri(EX.Object)})
|
2016-10-15 16:26:56 +00:00
|
|
|
|
2017-02-12 14:42:27 +00:00
|
|
|
assert Description.add(description(),
|
2017-08-20 20:35:14 +00:00
|
|
|
{iri(EX.Subject), EX.predicate, literal(42)})
|
2016-10-15 16:26:56 +00:00
|
|
|
|> description_includes_predication({EX.predicate, literal(42)})
|
|
|
|
|
2017-02-12 14:42:27 +00:00
|
|
|
assert Description.add(description(),
|
2017-08-20 20:35:14 +00:00
|
|
|
{iri(EX.Subject), EX.predicate, bnode(:foo)})
|
2016-10-15 16:26:56 +00:00
|
|
|
|> description_includes_predication({EX.predicate, bnode(:foo)})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "add ignores triples not about the subject of the Description struct" do
|
|
|
|
assert empty_description(
|
2017-08-20 20:35:14 +00:00
|
|
|
Description.add(description(), {EX.Other, EX.predicate, iri(EX.Object)}))
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a list of predicate-object-pairs" do
|
2017-02-12 14:42:27 +00:00
|
|
|
desc = Description.add(description(),
|
2016-10-15 16:26:56 +00:00
|
|
|
[{EX.predicate, EX.Object1}, {EX.predicate, EX.Object2}])
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate, iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate, iri(EX.Object2)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a list of triples" do
|
2017-02-12 14:42:27 +00:00
|
|
|
desc = Description.add(description(), [
|
2016-10-15 16:26:56 +00:00
|
|
|
{EX.Subject, EX.predicate1, EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2, EX.Object2}
|
|
|
|
])
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate1, iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2, iri(EX.Object2)})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "a list of mixed triples and predicate-object-pairs" do
|
2017-02-12 14:42:27 +00:00
|
|
|
desc = Description.add(description(), [
|
2016-10-15 16:26:56 +00:00
|
|
|
{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))
|
|
|
|
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
|
|
|
|
|
2016-11-15 00:26:57 +00:00
|
|
|
|
|
|
|
test "another description" do
|
2016-11-24 07:44:07 +00:00
|
|
|
desc = description([{EX.predicate1, EX.Object1}, {EX.predicate2, EX.Object2}])
|
2016-11-15 00:26:57 +00:00
|
|
|
|> Description.add(Description.new({EX.Other, EX.predicate3, EX.Object3}))
|
|
|
|
|
2017-08-20 20:35:14 +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)})
|
2016-11-15 00:26:57 +00:00
|
|
|
|
|
|
|
desc = Description.add(desc, Description.new({EX.Other, EX.predicate1, EX.Object4}))
|
2017-08-20 20:35:14 +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)})
|
2016-11-15 00:26:57 +00:00
|
|
|
end
|
|
|
|
|
2017-08-11 20:22:27 +00:00
|
|
|
test "a map of predications with coercible RDF terms" do
|
2016-11-24 07:44:07 +00:00
|
|
|
desc = description([{EX.predicate1, EX.Object1}, {EX.predicate2, EX.Object2}])
|
|
|
|
|> Description.add(%{EX.predicate3 => EX.Object3})
|
|
|
|
|
2017-08-20 20:35:14 +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)})
|
2016-11-24 07:44:07 +00:00
|
|
|
|
|
|
|
desc = Description.add(desc, %{EX.predicate1 => EX.Object1,
|
|
|
|
EX.predicate2 => [EX.Object2, 42],
|
|
|
|
EX.predicate3 => [bnode(:foo)]})
|
|
|
|
assert Description.count(desc) == 5
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate1, iri(EX.Object1)})
|
|
|
|
assert description_includes_predication(desc, {EX.predicate2, iri(EX.Object2)})
|
2016-11-24 07:44:07 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate2, literal(42)})
|
2017-08-20 20:35:14 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate3, iri(EX.Object3)})
|
2016-11-24 07:44:07 +00:00
|
|
|
assert description_includes_predication(desc, {EX.predicate3, bnode(:foo)})
|
|
|
|
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 ->
|
2017-02-12 14:42:27 +00:00
|
|
|
Description.add(description(), %{EX.prop => self()})
|
2016-11-24 07:44:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-15 16:26:56 +00:00
|
|
|
test "duplicates are ignored" do
|
2017-02-12 14:42:27 +00:00
|
|
|
desc = Description.add(description(), {EX.predicate, EX.Object})
|
2016-10-15 16:26:56 +00:00
|
|
|
assert Description.add(desc, {EX.predicate, EX.Object}) == desc
|
|
|
|
assert Description.add(desc, {EX.Subject, EX.predicate, EX.Object}) == desc
|
|
|
|
|
2017-02-12 14:42:27 +00:00
|
|
|
desc = Description.add(description(), {EX.predicate, 42})
|
2016-10-15 16:26:56 +00:00
|
|
|
assert Description.add(desc, {EX.predicate, literal(42)}) == desc
|
|
|
|
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 ->
|
2017-02-12 14:42:27 +00:00
|
|
|
Description.add(description(), {EX.prop, self()})
|
2016-10-15 16:26:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-23 00:03:41 +00:00
|
|
|
describe "delete" do
|
|
|
|
setup do
|
|
|
|
{:ok,
|
|
|
|
empty_description: Description.new(EX.S),
|
|
|
|
description1: Description.new(EX.S, EX.p, EX.O),
|
|
|
|
description2: Description.new(EX.S, EX.p, [EX.O1, EX.O2]),
|
|
|
|
description3: Description.new(EX.S, [
|
|
|
|
{EX.p1, [EX.O1, EX.O2]},
|
|
|
|
{EX.p2, EX.O3},
|
|
|
|
{EX.p3, [~B<foo>, ~L"bar"]},
|
|
|
|
])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a single statement as a predicate object",
|
|
|
|
%{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.O1) == Description.new(EX.S, EX.p, EX.O2)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a single statement as a predicate-object tuple",
|
|
|
|
%{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}) == Description.new(EX.S, EX.p, EX.O1)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a single statement as a subject-predicate-object tuple and the proper description subject",
|
|
|
|
%{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
|
|
|
|
assert Description.delete(description2, {EX.S, EX.p, EX.O2}) == Description.new(EX.S, EX.p, EX.O1)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a single statement as a subject-predicate-object tuple and another description subject",
|
|
|
|
%{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
|
|
|
|
end
|
|
|
|
|
|
|
|
test "multiple statements via predicate-objects tuple",
|
|
|
|
%{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
|
|
|
|
end
|
|
|
|
|
|
|
|
test "multiple statements with a list",
|
|
|
|
%{empty_description: empty_description, description3: description3} do
|
|
|
|
assert Description.delete(empty_description, [{EX.p, [EX.O1, EX.O2]}]) == empty_description
|
|
|
|
assert Description.delete(description3, [
|
|
|
|
{EX.p1, EX.O1},
|
|
|
|
{EX.p2, [EX.O2, EX.O3]},
|
|
|
|
{EX.S, EX.p3, [~B<foo>, ~L"bar"]},
|
|
|
|
]) == Description.new(EX.S, EX.p1, EX.O2)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "multiple statements with a map of predications",
|
|
|
|
%{empty_description: empty_description, description3: description3} do
|
2017-06-02 13:12:53 +00:00
|
|
|
assert Description.delete(empty_description, %{EX.p => EX.O1}) == empty_description
|
2017-05-23 00:03:41 +00:00
|
|
|
assert Description.delete(description3, %{
|
|
|
|
EX.p1 => EX.O1,
|
|
|
|
EX.p2 => [EX.O2, EX.O3],
|
|
|
|
EX.p3 => [~B<foo>, ~L"bar"],
|
|
|
|
}) == Description.new(EX.S, EX.p1, EX.O2)
|
|
|
|
end
|
2017-06-02 13:12:53 +00:00
|
|
|
|
|
|
|
test "multiple statements with another description",
|
|
|
|
%{empty_description: empty_description, description1: description1, description3: description3} do
|
|
|
|
assert Description.delete(empty_description, description1) == empty_description
|
|
|
|
assert Description.delete(description3, Description.new(EX.S, %{
|
|
|
|
EX.p1 => EX.O1,
|
|
|
|
EX.p2 => [EX.O2, EX.O3],
|
|
|
|
EX.p3 => [~B<foo>, ~L"bar"],
|
|
|
|
})) == Description.new(EX.S, EX.p1, EX.O2)
|
|
|
|
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,
|
|
|
|
empty_description: Description.new(EX.S),
|
|
|
|
description1: Description.new(EX.S, EX.p, [EX.O1, EX.O2]),
|
|
|
|
description2: Description.new(EX.S, [
|
|
|
|
{EX.P1, [EX.O1, EX.O2]},
|
|
|
|
{EX.p2, [~B<foo>, ~L"bar"]},
|
|
|
|
])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a single property",
|
|
|
|
%{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) ==
|
|
|
|
Description.new(EX.S, EX.p2, [~B<foo>, ~L"bar"])
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a list of properties",
|
|
|
|
%{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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
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)}
|
|
|
|
|
|
|
|
{triple, desc} = Description.new({EX.S, EX.p, EX.O}) |> Description.pop
|
2017-08-20 20:35:14 +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} =
|
|
|
|
Description.new([{EX.S, EX.p, EX.O1}, {EX.S, EX.p, EX.O2}])
|
|
|
|
|> Description.pop
|
2017-08-20 20:35:14 +00:00
|
|
|
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} =
|
|
|
|
Description.new([{EX.S, EX.p1, EX.O1}, {EX.S, EX.p2, EX.O2}])
|
|
|
|
|> 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
|
|
|
|
|
2016-11-15 00:26:57 +00:00
|
|
|
describe "Enumerable protocol" do
|
2016-10-15 16:26:56 +00:00
|
|
|
test "Enum.count" do
|
|
|
|
assert Enum.count(Description.new EX.foo) == 0
|
|
|
|
assert Enum.count(Description.new {EX.S, EX.p, EX.O}) == 1
|
|
|
|
assert Enum.count(Description.new [{EX.S, EX.p, EX.O1}, {EX.S, EX.p, EX.O2}]) == 2
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Enum.member?" do
|
2017-08-20 20:35:14 +00:00
|
|
|
refute Enum.member?(Description.new(EX.S), {iri(EX.S), EX.p, iri(EX.O)})
|
2016-10-15 16:26:56 +00:00
|
|
|
assert Enum.member?(Description.new({EX.S, EX.p, EX.O}), {EX.S, EX.p, EX.O})
|
|
|
|
|
|
|
|
desc = Description.new([
|
|
|
|
{EX.Subject, EX.predicate1, EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2, EX.Object2},
|
|
|
|
{EX.predicate2, EX.Object3}])
|
|
|
|
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})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "Enum.reduce" do
|
|
|
|
desc = Description.new([
|
|
|
|
{EX.Subject, EX.predicate1, EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2, EX.Object2},
|
|
|
|
{EX.predicate2, EX.Object3}])
|
2017-02-12 14:42:27 +00:00
|
|
|
assert desc == Enum.reduce(desc, description(),
|
2016-10-15 16:26:56 +00:00
|
|
|
fn(triple, acc) -> acc |> Description.add(triple) end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-30 10:40:44 +00:00
|
|
|
describe "Collectable protocol" do
|
|
|
|
test "with a map" do
|
|
|
|
map = %{
|
|
|
|
EX.predicate1 => EX.Object1,
|
|
|
|
EX.predicate2 => EX.Object2
|
|
|
|
}
|
|
|
|
assert Enum.into(map, Description.new(EX.Subject)) == Description.new(EX.Subject, map)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of triples" do
|
|
|
|
triples = [
|
|
|
|
{EX.Subject, EX.predicate1, EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate2, EX.Object2}
|
|
|
|
]
|
|
|
|
assert Enum.into(triples, Description.new(EX.Subject)) == Description.new(triples)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of predicate-object pairs" do
|
|
|
|
pairs = [
|
|
|
|
{EX.predicate1, EX.Object1},
|
|
|
|
{EX.predicate2, EX.Object2}
|
|
|
|
]
|
|
|
|
assert Enum.into(pairs, Description.new(EX.Subject)) == Description.new(EX.Subject, pairs)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "with a list of lists" do
|
|
|
|
lists = [
|
|
|
|
[EX.Subject, EX.predicate1, EX.Object1],
|
|
|
|
[EX.Subject, EX.predicate2, EX.Object2]
|
|
|
|
]
|
|
|
|
assert Enum.into(lists, Description.new(EX.Subject)) ==
|
|
|
|
Description.new(Enum.map(lists, &List.to_tuple/1))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-11 15:40:58 +00:00
|
|
|
describe "Access behaviour" do
|
|
|
|
test "access with the [] operator" do
|
|
|
|
assert Description.new(EX.Subject)[EX.predicate] == nil
|
2017-08-20 20:35:14 +00:00
|
|
|
assert Description.new(EX.Subject, EX.predicate, EX.Object)[EX.predicate] == [iri(EX.Object)]
|
|
|
|
assert Description.new(EX.Subject, EX.Predicate, EX.Object)[EX.Predicate] == [iri(EX.Object)]
|
|
|
|
assert Description.new(EX.Subject, EX.predicate, EX.Object)["http://example.com/predicate"] == [iri(EX.Object)]
|
2016-11-11 15:40:58 +00:00
|
|
|
assert Description.new([{EX.Subject, EX.predicate1, EX.Object1},
|
|
|
|
{EX.Subject, EX.predicate1, EX.Object2},
|
|
|
|
{EX.Subject, EX.predicate2, EX.Object3}])[EX.predicate1] ==
|
2017-08-20 20:35:14 +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
|