diff --git a/lib/rdf/dataset.ex b/lib/rdf/dataset.ex index 46229de..39ff032 100644 --- a/lib/rdf/dataset.ex +++ b/lib/rdf/dataset.ex @@ -37,6 +37,13 @@ defmodule RDF.Dataset do def new(%RDF.Description{} = description), do: new() |> add(description) + @doc """ + Creates an unnamed `RDF.Dataset` with a `RDF.Graph`. + """ + def new(%RDF.Graph{} = graph), + do: new() |> add(graph, graph.name) + + @doc """ Creates an empty named `RDF.Dataset`. """ @@ -61,6 +68,12 @@ defmodule RDF.Dataset do def new(name, %RDF.Description{} = description), do: new(name) |> add(description) + @doc """ + Creates a named `RDF.Dataset` with a `RDF.Graph`. + """ + def new(name, %RDF.Graph{} = graph), + do: new(name) |> add(graph, graph.name) + @doc """ diff --git a/test/unit/dataset_test.exs b/test/unit/dataset_test.exs index 9408ef1..8109739 100644 --- a/test/unit/dataset_test.exs +++ b/test/unit/dataset_test.exs @@ -70,8 +70,8 @@ defmodule RDF.DatasetTest do end test "creating a named dataset with an initial description" do - ds = Dataset.new(EX.GraphName, Description.new({EX.Subject, EX.predicate, EX.Object})) - assert named_dataset?(ds, uri(EX.GraphName)) + ds = Dataset.new(EX.DatasetName, Description.new({EX.Subject, EX.predicate, EX.Object})) + assert named_dataset?(ds, uri(EX.DatasetName)) assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate, EX.Object}) end @@ -80,6 +80,32 @@ defmodule RDF.DatasetTest do assert unnamed_dataset?(ds) assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate, EX.Object}) end + + test "creating a named dataset with an inital graph" do + ds = Dataset.new(EX.DatasetName, Graph.new({EX.Subject, EX.predicate, EX.Object})) + assert named_dataset?(ds, uri(EX.DatasetName)) + assert unnamed_graph?(Dataset.default_graph(ds)) + assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate, EX.Object}) + + ds = Dataset.new(EX.DatasetName, Graph.new(EX.GraphName, {EX.Subject, EX.predicate, EX.Object})) + assert named_dataset?(ds, uri(EX.DatasetName)) + assert unnamed_graph?(Dataset.default_graph(ds)) + assert named_graph?(Dataset.graph(ds, EX.GraphName), uri(EX.GraphName)) + assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate, EX.Object, EX.GraphName}) + end + + test "creating an unnamed dataset with an inital graph" do + ds = Dataset.new(Graph.new({EX.Subject, EX.predicate, EX.Object})) + assert unnamed_dataset?(ds) + assert unnamed_graph?(Dataset.default_graph(ds)) + assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate, EX.Object}) + + ds = Dataset.new(Graph.new(EX.GraphName, {EX.Subject, EX.predicate, EX.Object})) + assert unnamed_dataset?(ds) + assert unnamed_graph?(Dataset.default_graph(ds)) + assert named_graph?(Dataset.graph(ds, EX.GraphName), uri(EX.GraphName)) + assert dataset_includes_statement?(ds, {EX.Subject, EX.predicate, EX.Object, EX.GraphName}) + end end describe "add" do