Fix RDF.Graph.put/2 to ignore empty RDF.Descriptions

This commit is contained in:
Marcel Otto 2020-09-30 10:47:02 +02:00
parent f83ac494fc
commit 96bb678ffc
3 changed files with 25 additions and 4 deletions

View file

@ -23,10 +23,16 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Changed
- `RDF.Description.new` now requires the `subject` to be passed always as first argument;
if you want to add some initial data this must be done with the `:init` option
- for consistency reasons the internal `:id` struct field of `RDF.BlankNode` was renamed
to `:value`
- `RDF.Description.new` now requires the `subject` to be passed always as first argument;
if you want to add some initial data this must be done with the `:init` option
### Fixed
- `RDF.Graph.put/2` ignores empty descriptions; this should be the final piece to ensure
that `RDF.Graph`s never contain empty descriptions, which would distort results of
functions like `RDF.Graph.subjects/1`, `RDF.Graph.subject_count/1`, `RDF.Graph.descriptions/1`
[Compare v0.8.2...HEAD](https://github.com/rdf-elixir/rdf-ex/compare/v0.8.2...HEAD)

View file

@ -250,8 +250,13 @@ defmodule RDF.Graph do
def put(graph, {subject, predicate, object, _}),
do: put(graph, {subject, predicate, object})
def put(%__MODULE__{} = graph, %Description{subject: subject} = description),
do: do_put(graph, subject, description)
def put(%__MODULE__{} = graph, %Description{subject: subject} = description) do
if Description.count(description) > 0 do
do_put(graph, subject, description)
else
graph
end
end
def put(graph, %__MODULE__{descriptions: descriptions, prefixes: prefixes}) do
graph =

View file

@ -244,6 +244,11 @@ defmodule RDF.GraphTest do
assert graph_includes_statement?(g, {EX.Subject1, EX.predicate3(), EX.Object3})
end
test "an empty description is ignored" do
g = Graph.new() |> Graph.add(Description.new(EX.Subject))
assert empty_graph?(g)
end
test "a list of descriptions" do
g =
Graph.add(graph(), [
@ -402,6 +407,11 @@ defmodule RDF.GraphTest do
assert graph_includes_statement?(g, {EX.S2, EX.P2, EX.O2})
end
test "an empty description is ignored" do
g = Graph.new() |> Graph.put(Description.new(EX.Subject))
assert empty_graph?(g)
end
test "a list of descriptions" do
g =
Graph.new([{EX.S1, EX.p1(), EX.O1}, {EX.S2, EX.p2(), EX.O2}, {EX.S1, EX.p3(), EX.O3}])