rdf-ex/test/unit/description_test.exs

1012 lines
36 KiB
Elixir
Raw Normal View History

2016-10-15 16:26:56 +00:00
defmodule RDF.DescriptionTest do
use RDF.Test.Case
2016-10-15 16:26:56 +00:00
doctest RDF.Description
describe "new" do
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
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
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
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)})
end
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)})
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)})
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
end
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
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
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
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
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
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
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
test "with a list of predicate-object tuples" do
2020-06-29 08:37:42 +00:00
desc =
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)})
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
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)})
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}
])
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
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})
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)]
})
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)})
end
test "a map of predications with non-coercible RDF terms" do
assert_raise RDF.IRI.InvalidError, fn ->
Description.add(description(), %{"not a IRI" => iri(EX.Object)})
end
assert_raise RDF.Literal.InvalidError, fn ->
2020-06-29 08:37:42 +00:00
Description.add(description(), %{EX.prop() => self()})
end
end
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}])
|> Description.add(
Description.new(EX.Other, init: {EX.Other, EX.predicate3(), EX.Object3})
)
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)})
desc =
Description.add(
desc,
Description.new(EX.Other, init: {EX.Other, EX.predicate1(), EX.Object4})
)
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
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)})
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)})
end
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
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
test "non-coercible Triple elements are causing an error" do
assert_raise RDF.IRI.InvalidError, fn ->
Description.add(description(), {"not a IRI", iri(EX.Object)})
2016-10-15 16:26:56 +00:00
end
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
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
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)})
end
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
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
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)})
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([
{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
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([
{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
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,
Description.new(EX.Other, init: {EX.Other, EX.predicate(), iri(EX.Object)})
) == desc
end
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
test "triples with another subject are ignored" do
assert empty_description(
Description.put(description(), {EX.Other, EX.predicate(), iri(EX.Object)})
)
end
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
end
describe "delete" do
setup do
{:ok,
2020-06-29 08:37:42 +00:00
empty_description: Description.new(EX.S),
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:
Description.new(EX.S,
init: [
{EX.p1(), [EX.O1, EX.O2]},
{EX.p2(), EX.O3},
{EX.p3(), [~B<foo>, ~L"bar"]}
]
)}
end
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}) ==
Description.new(EX.S, init: {EX.S, EX.p(), EX.O1})
end
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
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}) ==
Description.new(EX.S, init: {EX.S, EX.p(), EX.O1})
end
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
assert Description.delete(description2, {EX.Other, EX.p(), EX.O2, EX.Graph}) == description2
end
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
assert Description.delete(description2, {EX.p(), [EX.O1, EX.O2]}) == empty_description
end
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
assert Description.delete(description3, [
2020-06-29 08:37:42 +00:00
{EX.p1(), EX.O1},
{EX.p2(), [EX.O2, EX.O3]},
{EX.S, EX.p3(), [~B<foo>]},
{EX.S, EX.p3(), ~L"bar", EX.Graph}
]) == Description.new(EX.S, init: {EX.S, EX.p1(), EX.O2})
end
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
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"]
}) == Description.new(EX.S, init: {EX.S, EX.p1(), EX.O2})
end
test "another description",
2020-06-29 08:37:42 +00:00
%{
empty_description: empty_description,
description1: description1,
description3: description3
} do
assert Description.delete(empty_description, description1) == empty_description
2020-06-29 08:37:42 +00:00
assert Description.delete(
description3,
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})
end
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
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
end
describe "delete_predicates" do
setup do
{:ok,
2020-06-29 08:37:42 +00:00
empty_description: Description.new(EX.S),
description1: Description.new(EX.S, init: {EX.S, EX.p(), [EX.O1, EX.O2]}),
2020-06-29 08:37:42 +00:00
description2:
Description.new(EX.S,
init: [
{EX.P1, [EX.O1, EX.O2]},
{EX.p2(), [~B<foo>, ~L"bar"]}
]
)}
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
assert Description.delete_predicates(description2, EX.P1) ==
Description.new(EX.S, init: {EX.S, EX.p2(), [~B<foo>, ~L"bar"]})
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
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
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
) ==
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
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) ==
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) ==
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) ==
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
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
assert Description.new(EX.S) |> Description.update(EX.P, fun) ==
2019-10-23 15:31:21 +00:00
Description.new(EX.S)
end
end
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, 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} =
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} =
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()
assert subject == iri(EX.S)
2016-10-15 16:26:56 +00:00
assert Enum.count(desc.predications) == 1
end
describe "include?/3" do
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]})
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()}
)
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
end
test "values/1" do
2020-06-29 08:37:42 +00:00
assert Description.new(EX.s()) |> Description.values() == %{}
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"]}
end
test "values/2" do
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
mapping = fn
{:predicate, predicate} ->
predicate |> to_string() |> String.split("/") |> List.last() |> String.to_atom()
2020-06-29 08:37:42 +00:00
{_, term} ->
RDF.Term.value(term)
end
assert Description.new(EX.s()) |> Description.map(mapping) == %{}
2020-06-29 08:37:42 +00:00
assert Description.new(EX.s(), init: {EX.s(), EX.p(), ~L"Foo"}) |> Description.map(mapping) ==
%{p: ["Foo"]}
end
2019-10-15 15:29:46 +00:00
describe "take/2" do
test "with a non-empty property list" do
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()]) ==
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
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
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) ==
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
test "equal/2" do
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
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}))
end
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
assert Enum.count(Description.new(EX.S, init: {EX.S, EX.p(), EX.O})) == 1
assert Enum.count(
Description.new(EX.S, init: [{EX.S, EX.p(), EX.O1}, {EX.S, EX.p(), EX.O2}])
) == 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)})
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 =
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 =
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
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
}
assert Enum.into(map, Description.new(EX.Subject)) ==
Description.new(EX.Subject, init: map)
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}
]
assert Enum.into(triples, Description.new(EX.Subject)) ==
Description.new(EX.Subject, init: triples)
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}
]
assert Enum.into(pairs, Description.new(EX.Subject)) ==
Description.new(EX.Subject, init: pairs)
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]
]
assert Enum.into(lists, Description.new(EX.Subject)) ==
Description.new(EX.Subject, init: Enum.map(lists, &List.to_tuple/1))
end
end
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
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)
]
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)
]
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)]
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)]
end
end
2016-10-15 16:26:56 +00:00
end