core: add RDF.Dataset.delete_graph/2 and RDF.Dataset.delete_default_graph/1

This commit is contained in:
Marcel Otto 2017-06-02 23:05:00 +02:00
parent 7b7c4af243
commit 3f403d6f64
2 changed files with 64 additions and 0 deletions

View file

@ -255,6 +255,29 @@ defmodule RDF.Dataset do
do: put(dataset, {subject, graph_context}, predications, graph_context)
@doc """
Deletes the given graph.
"""
def delete_graph(graph, graph_names)
def delete_graph(%RDF.Dataset{} = dataset, graph_names) when is_list(graph_names) do
Enum.reduce graph_names, dataset, fn (graph_name, dataset) ->
delete_graph(dataset, graph_name)
end
end
def delete_graph(%RDF.Dataset{name: name, graphs: graphs}, graph_name) do
with graph_name = convert_graph_name(graph_name) do
%RDF.Dataset{name: name, graphs: Map.delete(graphs, graph_name)}
end
end
@doc """
Deletes the default graph.
"""
def delete_default_graph(%RDF.Dataset{} = graph),
do: delete_graph(graph, nil)
@doc """
Fetches the `RDF.Graph` with the given name.

View file

@ -388,6 +388,46 @@ defmodule RDF.DatasetTest do
end
end
describe "delete_graph" do
setup do
{:ok,
dataset1: Dataset.new({EX.S1, EX.p1, EX.O1}),
dataset2: Dataset.new([
{EX.S1, EX.p1, EX.O1},
{EX.S2, EX.p2, EX.O2, EX.Graph},
]),
dataset3: Dataset.new([
{EX.S1, EX.p1, EX.O1},
{EX.S2, EX.p2, EX.O2, EX.Graph1},
{EX.S3, EX.p3, EX.O3, EX.Graph2},
]),
}
end
test "the default graph", %{dataset1: dataset1, dataset2: dataset2} do
assert Dataset.delete_graph(dataset1, nil) == Dataset.new
assert Dataset.delete_graph(dataset2, nil) == Dataset.new({EX.S2, EX.p2, EX.O2, EX.Graph})
end
test "delete_default_graph", %{dataset1: dataset1, dataset2: dataset2} do
assert Dataset.delete_default_graph(dataset1) == Dataset.new
assert Dataset.delete_default_graph(dataset2) == Dataset.new({EX.S2, EX.p2, EX.O2, EX.Graph})
end
test "a single graph", %{dataset1: dataset1, dataset2: dataset2} do
assert Dataset.delete_graph(dataset1, EX.Graph) == dataset1
assert Dataset.delete_graph(dataset2, EX.Graph) == dataset1
end
test "a list of graphs", %{dataset1: dataset1, dataset3: dataset3} do
assert Dataset.delete_graph(dataset3, [EX.Graph1, EX.Graph2]) == dataset1
assert Dataset.delete_graph(dataset3, [EX.Graph1, EX.Graph2, EX.Graph3]) == dataset1
assert Dataset.delete_graph(dataset3, [EX.Graph1, EX.Graph2, nil]) == Dataset.new
end
end
test "pop" do
assert Dataset.pop(Dataset.new) == {nil, Dataset.new}
@ -409,6 +449,7 @@ defmodule RDF.DatasetTest do
assert Enum.count(dataset.graphs) == 1
end
describe "Enumerable protocol" do
test "Enum.count" do
assert Enum.count(Dataset.new EX.foo) == 0