core: RDF.Test.Case with shared test helper functions

This commit is contained in:
Marcel Otto 2017-02-13 01:21:32 +01:00
parent 4c007a0e02
commit f35086cd7b
3 changed files with 74 additions and 49 deletions

66
test/support/rdf_case.ex Normal file
View file

@ -0,0 +1,66 @@
defmodule RDF.Test.Case do
use ExUnit.CaseTemplate
alias RDF.{Dataset, Graph, Description}
import RDF, only: [uri: 1]
defmodule EX, do:
use RDF.Vocabulary, base_uri: "http://example.com/"
using do
quote do
alias RDF.{Dataset, Graph, Description}
alias EX
import RDF, only: [uri: 1, literal: 1, bnode: 1]
import RDF.Test.Case
end
end
###############################
# RDF.Description
def description, do: Description.new(EX.Subject)
def description(content), do: Description.add(description(), content)
def description_of_subject(%Description{subject: subject}, subject),
do: true
def description_of_subject(_, _),
do: false
def empty_description(%Description{predications: predications}),
do: predications == %{}
def description_includes_predication(desc, {predicate, object}) do
desc.predications
|> Map.get(predicate, %{})
|> Enum.member?({object, nil})
end
###############################
# RDF.Graph
def graph, do: unnamed_graph()
def unnamed_graph, do: Graph.new
def named_graph(name \\ EX.GraphName), do: Graph.new(name)
def unnamed_graph?(%Graph{name: nil}), do: true
def unnamed_graph?(_), do: false
def named_graph?(%Graph{name: %URI{}}), do: true
def named_graph?(_), do: false
def named_graph?(%Graph{name: name}, name), do: true
def named_graph?(_, _), do: false
def empty_graph?(%Graph{descriptions: descriptions}),
do: descriptions == %{}
def graph_includes_statement?(graph, statement = {subject, _, _}) do
graph.descriptions
|> Map.get(uri(subject), %{})
|> Enum.member?(statement)
end
end

View file

@ -1,27 +1,8 @@
defmodule RDF.DescriptionTest do defmodule RDF.DescriptionTest do
use ExUnit.Case use RDF.Test.Case
defmodule EX, do:
use RDF.Vocabulary, base_uri: "http://example.com/description/"
doctest RDF.Description doctest RDF.Description
alias RDF.Description
import RDF, only: [uri: 1, literal: 1, bnode: 1]
def description, do: Description.new(EX.Subject)
def description(content), do: Description.add(description(), content)
def description_of_subject(%Description{subject: subject}, subject), do: true
def description_of_subject(_, _), do: false
def empty_description(%Description{predications: predications}) do
predications == %{}
end
def description_includes_predication(desc, {predicate, object}) do
desc.predications
|> Map.get(predicate, %{})
|> Enum.member?({object, nil})
end
describe "creating an empty description" do describe "creating an empty description" do
test "with a subject URI" do test "with a subject URI" do
@ -91,17 +72,17 @@ defmodule RDF.DescriptionTest do
test "a predicate-object-pair of convertible RDF terms" do test "a predicate-object-pair of convertible RDF terms" do
assert Description.add(description(), assert Description.add(description(),
"http://example.com/description/predicate", uri(EX.Object)) "http://example.com/predicate", uri(EX.Object))
|> description_includes_predication({EX.predicate, uri(EX.Object)}) |> description_includes_predication({EX.predicate, uri(EX.Object)})
assert Description.add(description(), assert Description.add(description(),
{"http://example.com/description/predicate", 42}) {"http://example.com/predicate", 42})
|> description_includes_predication({EX.predicate, literal(42)}) |> description_includes_predication({EX.predicate, literal(42)})
# TODO: Test a url-string as object ... # TODO: Test a url-string as object ...
assert Description.add(description(), assert Description.add(description(),
{"http://example.com/description/predicate", bnode(:foo)}) {"http://example.com/predicate", bnode(:foo)})
|> description_includes_predication({EX.predicate, bnode(:foo)}) |> description_includes_predication({EX.predicate, bnode(:foo)})
end end
@ -275,7 +256,7 @@ defmodule RDF.DescriptionTest do
assert Description.new(EX.Subject)[EX.predicate] == nil assert Description.new(EX.Subject)[EX.predicate] == nil
assert Description.new(EX.Subject, EX.predicate, EX.Object)[EX.predicate] == [uri(EX.Object)] assert Description.new(EX.Subject, EX.predicate, EX.Object)[EX.predicate] == [uri(EX.Object)]
assert Description.new(EX.Subject, EX.Predicate, EX.Object)[EX.Predicate] == [uri(EX.Object)] assert Description.new(EX.Subject, EX.Predicate, EX.Object)[EX.Predicate] == [uri(EX.Object)]
assert Description.new(EX.Subject, EX.predicate, EX.Object)["http://example.com/description/predicate"] == [uri(EX.Object)] assert Description.new(EX.Subject, EX.predicate, EX.Object)["http://example.com/predicate"] == [uri(EX.Object)]
assert Description.new([{EX.Subject, EX.predicate1, EX.Object1}, assert Description.new([{EX.Subject, EX.predicate1, EX.Object1},
{EX.Subject, EX.predicate1, EX.Object2}, {EX.Subject, EX.predicate1, EX.Object2},
{EX.Subject, EX.predicate2, EX.Object3}])[EX.predicate1] == {EX.Subject, EX.predicate2, EX.Object3}])[EX.predicate1] ==

View file

@ -1,30 +1,8 @@
defmodule RDF.GraphTest do defmodule RDF.GraphTest do
use ExUnit.Case use RDF.Test.Case
defmodule EX, do:
use RDF.Vocabulary, base_uri: "http://example.com/graph/"
doctest RDF.Graph doctest RDF.Graph
alias RDF.{Graph, Description}
import RDF, only: [uri: 1, bnode: 1]
def graph, do: unnamed_graph()
def unnamed_graph, do: Graph.new
def named_graph(name \\ EX.GraphName), do: Graph.new(name)
def unnamed_graph?(%Graph{name: nil}), do: true
def unnamed_graph?(_), do: false
def named_graph?(%Graph{name: %URI{}}), do: true
def named_graph?(_), do: false
def named_graph?(%Graph{name: name}, name), do: true
def named_graph?(_, _), do: false
def empty_graph?(%Graph{descriptions: descriptions}), do: descriptions == %{}
def graph_includes_statement?(graph, statement = {subject, _, _}) do
graph.descriptions
|> Map.get(uri(subject), %{})
|> Enum.member?(statement)
end
describe "construction" do describe "construction" do
test "creating an empty unnamed graph" do test "creating an empty unnamed graph" do
@ -111,10 +89,10 @@ defmodule RDF.GraphTest do
test "a convertible triple" do test "a convertible triple" do
assert Graph.add(graph(), assert Graph.add(graph(),
"http://example.com/graph/Subject", EX.predicate, EX.Object) "http://example.com/Subject", EX.predicate, EX.Object)
|> graph_includes_statement?({EX.Subject, EX.predicate, EX.Object}) |> graph_includes_statement?({EX.Subject, EX.predicate, EX.Object})
assert Graph.add(graph(), assert Graph.add(graph(),
{"http://example.com/graph/Subject", EX.predicate, EX.Object}) {"http://example.com/Subject", EX.predicate, EX.Object})
|> graph_includes_statement?({EX.Subject, EX.predicate, EX.Object}) |> graph_includes_statement?({EX.Subject, EX.predicate, EX.Object})
end end