2017-04-11 00:09:35 +00:00
|
|
|
defmodule RDF.NQuads.DecoderTest do
|
|
|
|
use ExUnit.Case, async: false
|
|
|
|
|
|
|
|
doctest RDF.NQuads.Decoder
|
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
alias RDF.NQuads.Decoder
|
2017-07-06 19:47:04 +00:00
|
|
|
alias RDF.Dataset
|
2017-04-11 00:09:35 +00:00
|
|
|
|
|
|
|
import RDF.Sigils
|
|
|
|
|
|
|
|
use RDF.Vocabulary.Namespace
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defvocab EX, base_iri: "http://example.org/#", terms: [], strict: false
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
defvocab P, base_iri: "http://www.perceive.net/schemas/relationship/", terms: [], strict: false
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
import RDF.Sigils
|
|
|
|
import RDF.Test.Case, only: [string_to_stream: 1]
|
|
|
|
|
|
|
|
test "stream_support?/0" do
|
|
|
|
assert Decoder.stream_support?()
|
|
|
|
end
|
|
|
|
|
2017-04-11 00:09:35 +00:00
|
|
|
test "an empty string is deserialized to an empty graph" do
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("") == Dataset.new()
|
|
|
|
assert Decoder.decode!(" \n\r\r\n ") == Dataset.new()
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "decoding comments" do
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("# just a comment") == Dataset.new()
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S> <http://example.org/#p> _:1 <http://example.org/#G>. # a comment
|
|
|
|
""") == Dataset.new({EX.S, EX.p(), RDF.bnode("1"), EX.G})
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
# a comment
|
|
|
|
<http://example.org/#S> <http://example.org/#p> <http://example.org/#O> <http://example.org/#G>.
|
|
|
|
""") == Dataset.new({EX.S, EX.p(), EX.O, EX.G})
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S> <http://example.org/#p> <http://example.org/#O> <http://example.org/#G>.
|
|
|
|
# a comment
|
|
|
|
""") == Dataset.new({EX.S, EX.p(), EX.O, EX.G})
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
# Header line 1
|
|
|
|
# Header line 2
|
|
|
|
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> <http://example.org/#G> .
|
|
|
|
# 1st comment
|
|
|
|
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> . # 2nd comment
|
|
|
|
# last comment
|
|
|
|
""") ==
|
|
|
|
Dataset.new([
|
|
|
|
{EX.S1, EX.p1(), EX.O1, EX.G},
|
|
|
|
{EX.S1, EX.p2(), EX.O2}
|
|
|
|
])
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "empty lines" do
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green_goblin> <http://example.org/graphs/spiderman> .
|
|
|
|
""") ==
|
|
|
|
Dataset.new(
|
|
|
|
{EX.spiderman(), P.enemyOf(), EX.green_goblin(),
|
|
|
|
~I<http://example.org/graphs/spiderman>}
|
|
|
|
)
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green_goblin> <http://example.org/graphs/spiderman> .
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
""") ==
|
|
|
|
Dataset.new(
|
|
|
|
{EX.spiderman(), P.enemyOf(), EX.green_goblin(),
|
|
|
|
~I<http://example.org/graphs/spiderman>}
|
|
|
|
)
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> .
|
2017-04-11 00:09:35 +00:00
|
|
|
|
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> <http://example.org/#G> .
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
""") ==
|
|
|
|
Dataset.new([
|
|
|
|
{EX.S1, EX.p1(), EX.O1},
|
|
|
|
{EX.S1, EX.p2(), EX.O2, EX.G}
|
|
|
|
])
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
test "decoding a single statement with iris" do
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green_goblin> .
|
|
|
|
""") == Dataset.new({EX.spiderman(), P.enemyOf(), EX.green_goblin()})
|
2017-04-11 00:09:35 +00:00
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/enemyOf> <http://example.org/#green_goblin> <http://example.org/graphs/spiderman>.
|
|
|
|
""") ==
|
|
|
|
Dataset.new(
|
|
|
|
{EX.spiderman(), P.enemyOf(), EX.green_goblin(),
|
|
|
|
~I<http://example.org/graphs/spiderman>}
|
|
|
|
)
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "decoding a single statement with a blank node" do
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
_:foo <http://example.org/#p> <http://example.org/#O> <http://example.org/#G> .
|
|
|
|
""") == Dataset.new({RDF.bnode("foo"), EX.p(), EX.O, EX.G})
|
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S> <http://example.org/#p> _:1 <http://example.org/#G> .
|
|
|
|
""") == Dataset.new({EX.S, EX.p(), RDF.bnode("1"), EX.G})
|
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
_:foo <http://example.org/#p> _:bar <http://example.org/#G> .
|
|
|
|
""") == Dataset.new({RDF.bnode("foo"), EX.p(), RDF.bnode("bar"), EX.G})
|
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S> <http://example.org/#p> _:1 _:G .
|
|
|
|
""") == Dataset.new({EX.S, EX.p(), RDF.bnode("1"), RDF.bnode("G")})
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "decoding a single statement with an untyped string literal" do
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/realname> "Peter Parker" <http://example.org/#G> .
|
|
|
|
""") == Dataset.new({EX.spiderman(), P.realname(), RDF.literal("Peter Parker"), EX.G})
|
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#spiderman> <http://www.perceive.net/schemas/relationship/realname> "Peter Parker" .
|
|
|
|
""") == Dataset.new({EX.spiderman(), P.realname(), RDF.literal("Peter Parker")})
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "decoding a single statement with a typed literal" do
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#spiderman> <http://example.org/#p> "42"^^<http://www.w3.org/2001/XMLSchema#integer> <http://example.org/#G> .
|
|
|
|
""") == Dataset.new({EX.spiderman(), EX.p(), RDF.literal(42), EX.G})
|
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#spiderman> <http://example.org/#p> "42"^^<http://www.w3.org/2001/XMLSchema#integer> .
|
|
|
|
""") == Dataset.new({EX.spiderman(), EX.p(), RDF.literal(42)})
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "decoding a single statement with a language tagged literal" do
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S> <http://example.org/#p> "foo"@en <http://example.org/#G> .
|
|
|
|
""") == Dataset.new({EX.S, EX.p(), RDF.literal("foo", language: "en"), EX.G})
|
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S> <http://example.org/#p> "foo"@en .
|
|
|
|
""") == Dataset.new({EX.S, EX.p(), RDF.literal("foo", language: "en")})
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "decoding multiple statements" do
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> <http://example.org/#G> .
|
|
|
|
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> <http://example.org/#G> .
|
|
|
|
""") ==
|
|
|
|
Dataset.new([
|
|
|
|
{EX.S1, EX.p1(), EX.O1, EX.G},
|
|
|
|
{EX.S1, EX.p2(), EX.O2, EX.G}
|
|
|
|
])
|
|
|
|
|
2020-11-04 15:44:53 +00:00
|
|
|
assert Decoder.decode!("""
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> <http://example.org/#G> .
|
|
|
|
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> <http://example.org/#G> .
|
|
|
|
<http://example.org/#S2> <http://example.org/#p3> <http://example.org/#O3> <http://example.org/#G> .
|
2020-11-04 15:44:53 +00:00
|
|
|
|
2020-06-29 08:37:42 +00:00
|
|
|
<http://example.org/#S2> <http://example.org/#p3> <http://example.org/#O3> .
|
|
|
|
""") ==
|
|
|
|
Dataset.new([
|
|
|
|
{EX.S1, EX.p1(), EX.O1, EX.G},
|
|
|
|
{EX.S1, EX.p2(), EX.O2, EX.G},
|
|
|
|
{EX.S2, EX.p3(), EX.O3, EX.G},
|
|
|
|
{EX.S2, EX.p3(), EX.O3}
|
|
|
|
])
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|
2020-11-04 15:44:53 +00:00
|
|
|
|
|
|
|
test "decode_from_stream/2" do
|
|
|
|
assert """
|
|
|
|
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> <http://example.org/#G> .
|
|
|
|
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> <http://example.org/#G> .
|
|
|
|
<http://example.org/#S2> <http://example.org/#p3> _:foo <http://example.org/#G> .
|
|
|
|
|
|
|
|
|
|
|
|
<http://example.org/#S2> <http://example.org/#p3> "foo"@en .
|
|
|
|
"""
|
|
|
|
|> string_to_stream()
|
|
|
|
|> Decoder.decode_from_stream() ==
|
2020-11-06 12:03:01 +00:00
|
|
|
{:ok,
|
|
|
|
Dataset.new([
|
|
|
|
{EX.S1, EX.p1(), EX.O1, EX.G},
|
|
|
|
{EX.S1, EX.p2(), EX.O2, EX.G},
|
|
|
|
{EX.S2, EX.p3(), ~B"foo", EX.G},
|
|
|
|
{EX.S2, EX.p3(), ~L"foo"en}
|
|
|
|
])}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "decode_from_stream!/2" do
|
|
|
|
assert """
|
|
|
|
<http://example.org/#S1> <http://example.org/#p1> <http://example.org/#O1> <http://example.org/#G> .
|
|
|
|
<http://example.org/#S1> <http://example.org/#p2> <http://example.org/#O2> <http://example.org/#G> .
|
|
|
|
<http://example.org/#S2> <http://example.org/#p3> _:foo <http://example.org/#G> .
|
|
|
|
|
|
|
|
|
|
|
|
<http://example.org/#S2> <http://example.org/#p3> "foo"@en .
|
|
|
|
"""
|
|
|
|
|> string_to_stream()
|
|
|
|
|> Decoder.decode_from_stream!() ==
|
2020-11-04 15:44:53 +00:00
|
|
|
Dataset.new([
|
|
|
|
{EX.S1, EX.p1(), EX.O1, EX.G},
|
|
|
|
{EX.S1, EX.p2(), EX.O2, EX.G},
|
|
|
|
{EX.S2, EX.p3(), ~B"foo", EX.G},
|
|
|
|
{EX.S2, EX.p3(), ~L"foo"en}
|
|
|
|
])
|
|
|
|
end
|
2017-04-11 00:09:35 +00:00
|
|
|
end
|