Fix RDF.Dataset.equal?/2 to ignore prefixes

This commit is contained in:
Marcel Otto 2019-04-04 21:19:33 +02:00
parent 731b168ef4
commit 95d5d01217
4 changed files with 29 additions and 33 deletions

View file

@ -274,8 +274,8 @@ defimpl RDF.Data, for: RDF.Graph do
do: RDF.Data.equal?(description, graph)
def equal?(graph, %RDF.Graph{} = other_graph),
do: RDF.Graph.equal?(RDF.Data.Utils.normalize_graph(graph),
RDF.Data.Utils.normalize_graph(other_graph))
do: RDF.Graph.equal?(%RDF.Graph{graph | name: nil},
%RDF.Graph{other_graph | name: nil})
def equal?(graph, %RDF.Dataset{} = dataset),
do: RDF.Data.equal?(dataset, graph)
@ -354,25 +354,9 @@ defimpl RDF.Data, for: RDF.Dataset do
end
def equal?(dataset, %RDF.Dataset{} = other_dataset) do
RDF.Dataset.equal?(RDF.Data.Utils.normalize_dataset(dataset),
RDF.Data.Utils.normalize_dataset(other_dataset))
RDF.Dataset.equal?(%RDF.Dataset{dataset | name: nil},
%RDF.Dataset{other_dataset | name: nil})
end
def equal?(_, _), do: false
end
defmodule RDF.Data.Utils do
@moduledoc false
def normalize_graph(graph) do
%RDF.Graph{graph | name: nil}
end
def normalize_dataset(%RDF.Dataset{graphs: graphs}) do
%RDF.Dataset{name: nil, graphs:
Map.new(graphs, fn {name, graph} ->
{name, RDF.Graph.clear_prefixes(graph)}
end)
}
end
end

View file

@ -788,11 +788,20 @@ defmodule RDF.Dataset do
def equal?(dataset1, dataset2)
def equal?(%RDF.Dataset{} = dataset1, %RDF.Dataset{} = dataset2) do
dataset1 == dataset2
clear_prefixes(dataset1) == clear_prefixes(dataset2)
end
def equal?(_, _), do: false
defp clear_prefixes(%RDF.Dataset{graphs: graphs} = dataset) do
%RDF.Dataset{dataset |
graphs:
Map.new(graphs, fn {name, graph} ->
{name, RDF.Graph.clear_prefixes(graph)}
end)
}
end
defimpl Enumerable do
def member?(dataset, statement), do: {:ok, RDF.Dataset.include?(dataset, statement)}

View file

@ -740,13 +740,21 @@ defmodule RDF.DatasetTest do
test "equal/2" do
assert Dataset.new({EX.S, EX.p, EX.O}) |> Dataset.equal?(Dataset.new({EX.S, EX.p, EX.O}))
assert Dataset.new({EX.S, EX.p, EX.O}, name: EX.Dataset1)
|> Dataset.equal?(Dataset.new({EX.S, EX.p, EX.O}, name: EX.Dataset1))
refute Dataset.new({EX.S, EX.p, EX.O}) |> Dataset.equal?(Dataset.new({EX.S, EX.p, EX.O2}))
refute Dataset.new({EX.S, EX.p, EX.O}, name: EX.Dataset1)
|> Dataset.equal?(Dataset.new({EX.S, EX.p, EX.O}, name: EX.Dataset2))
triple = {EX.S, EX.p, EX.O}
assert Dataset.equal?(Dataset.new(triple), Dataset.new(triple))
assert Dataset.equal?(Dataset.new(triple, name: EX.Dataset1),
Dataset.new(triple, name: EX.Dataset1))
assert Dataset.equal?(
Dataset.new(Graph.new(triple, name: EX.Graph1, prefixes: %{ex: EX})),
Dataset.new(Graph.new(triple, name: EX.Graph1, prefixes: %{ex: RDF}))
)
refute Dataset.equal?(Dataset.new(triple), Dataset.new({EX.S, EX.p, EX.O2}))
refute Dataset.equal?(Dataset.new(triple, name: EX.Dataset1),
Dataset.new(triple, name: EX.Dataset2))
refute Dataset.equal?(
Dataset.new(Graph.new(triple, name: EX.Graph1)),
Dataset.new(Graph.new(triple, name: EX.Graph2))
)
end

View file

@ -85,11 +85,6 @@ defmodule RDF.PrefixMapTest do
end
end
@tag skip: "TODO: "
test "when a default namespace is given" do
assert PrefixMap.add(@example1, :_, @ex_ns2) == {:ok, @example5}
end
test "when a mapping of the given prefix to the same namespace already exists" do
assert PrefixMap.add(@example2, :ex2, "http://example.com/bar#") == {:ok, @example2}
end