rdf-ex/test/support/rdf_case.ex

132 lines
3.5 KiB
Elixir
Raw Normal View History

defmodule RDF.Test.Case do
use ExUnit.CaseTemplate
use RDF.Vocabulary.Namespace
2020-06-29 08:37:42 +00:00
defvocab EX, base_iri: "http://example.com/", terms: [], strict: false
2020-06-29 08:37:42 +00:00
defvocab FOAF, base_iri: "http://xmlns.com/foaf/0.1/", terms: [], strict: false
alias RDF.{Dataset, Graph, Description, IRI}
import RDF, only: [iri: 1]
using do
quote do
2020-10-28 10:51:00 +00:00
alias RDF.{Dataset, Graph, Description, IRI, XSD, PrefixMap, PropertyMap}
alias RDF.NS.{RDFS, OWL}
alias unquote(__MODULE__).{EX, FOAF}
import RDF, only: [iri: 1, literal: 1, bnode: 1]
import unquote(__MODULE__)
2017-06-04 02:20:08 +00:00
import RDF.Sigils
@compile {:no_warn_undefined, RDF.Test.Case.EX}
@compile {:no_warn_undefined, RDF.Test.Case.FOAF}
end
end
# TODO: Remove this when we dropped support for Elixir versions < 1.10
def struct_not_allowed_as_input_error do
if Version.match?(System.version(), "~> 1.10") do
FunctionClauseError
else
ArgumentError
end
end
def string_to_stream(string) do
{:ok, pid} = StringIO.open(string)
IO.binstream(pid, :line)
end
def stream_to_string(stream) do
stream
|> Enum.to_list()
|> IO.iodata_to_binary()
end
###############################
# RDF.Description
2020-06-29 08:37:42 +00:00
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
2020-06-29 08:37:42 +00:00
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()
2020-06-29 08:37:42 +00:00
def unnamed_graph, do: Graph.new()
def named_graph(name \\ EX.GraphName), do: Graph.new(name: name)
def unnamed_graph?(%Graph{name: nil}), do: true
2020-06-29 08:37:42 +00:00
def unnamed_graph?(_), do: false
2020-06-29 08:37:42 +00:00
def named_graph?(%Graph{name: %IRI{}}), do: true
def named_graph?(_), do: false
def named_graph?(%Graph{name: name}, name), do: true
2020-06-29 08:37:42 +00:00
def named_graph?(_, _), do: false
def empty_graph?(%Graph{descriptions: descriptions}),
do: descriptions == %{}
2017-02-18 20:35:27 +00:00
def graph_includes_statement?(graph, {subject, _, _} = statement) do
graph.descriptions
|> Map.get(iri(subject), %{})
|> Enum.member?(statement)
end
2017-02-18 20:35:27 +00:00
###############################
# RDF.Dataset
2017-02-18 20:35:27 +00:00
def dataset, do: unnamed_dataset()
2020-06-29 08:37:42 +00:00
def unnamed_dataset, do: Dataset.new()
2017-02-18 20:35:27 +00:00
def named_dataset(name \\ EX.DatasetName), do: Dataset.new(name: name)
2017-02-18 20:35:27 +00:00
def unnamed_dataset?(%Dataset{name: nil}), do: true
2020-06-29 08:37:42 +00:00
def unnamed_dataset?(_), do: false
2017-02-18 20:35:27 +00:00
2020-06-29 08:37:42 +00:00
def named_dataset?(%Dataset{name: %IRI{}}), do: true
def named_dataset?(_), do: false
2017-02-18 20:35:27 +00:00
def named_dataset?(%Dataset{name: name}, name), do: true
2020-06-29 08:37:42 +00:00
def named_dataset?(_, _), do: false
2017-02-18 20:35:27 +00:00
def empty_dataset?(%Dataset{graphs: graphs}), do: graphs == %{}
def dataset_includes_statement?(dataset, {_, _, _} = statement) do
dataset
2020-06-29 08:37:42 +00:00
|> Dataset.default_graph()
2017-02-18 20:35:27 +00:00
|> graph_includes_statement?(statement)
end
def dataset_includes_statement?(dataset, {subject, predicate, objects, nil}),
do: dataset_includes_statement?(dataset, {subject, predicate, objects})
2020-06-29 08:37:42 +00:00
def dataset_includes_statement?(
dataset,
{subject, predicate, objects, graph_context}
) do
2017-02-18 20:35:27 +00:00
dataset.graphs
|> Map.get(iri(graph_context), named_graph(graph_context))
2017-02-18 20:35:27 +00:00
|> graph_includes_statement?({subject, predicate, objects})
end
end