defmodule RDF.NQuads.EncoderTest do use ExUnit.Case, async: false alias RDF.NQuads doctest NQuads.Encoder alias RDF.{Dataset, Graph} alias RDF.NS.XSD import RDF.Sigils import RDF.Test.Case, only: [stream_to_string: 1] use RDF.Vocabulary.Namespace defvocab EX, base_iri: "http://example.org/#", terms: [], strict: false test "stream_support?/0" do assert NQuads.Encoder.stream_support?() end describe "serializing a graph" do test "an empty graph is serialized to an empty string" do assert NQuads.Encoder.encode!(Graph.new()) == "" end test "statements with IRIs only" do assert NQuads.Encoder.encode!( Graph.new([ {EX.S1, EX.p1(), EX.O1}, {EX.S1, EX.p1(), EX.O2}, {EX.S1, EX.p2(), EX.O3}, {EX.S2, EX.p3(), EX.O4} ]) ) == """ . . . . """ end test "statements with literals" do assert NQuads.Encoder.encode!( Graph.new([ {EX.S1, EX.p1(), ~L"foo"}, {EX.S1, EX.p1(), ~L"foo"en}, {EX.S1, EX.p2(), 42}, {EX.S2, EX.p3(), RDF.literal("strange things", datatype: EX.custom())} ]) ) == """ "foo"@en . "foo" . "42"^^<#{XSD.integer()}> . "strange things"^^<#{EX.custom()}> . """ end test "statements with blank nodes" do assert NQuads.Encoder.encode!( Graph.new([ {EX.S1, EX.p1(), RDF.bnode(1)}, {EX.S1, EX.p1(), RDF.bnode("foo")}, {EX.S1, EX.p1(), RDF.bnode(:bar)} ]) ) == """ _:b1 . _:bar . _:foo . """ end end describe "serializing a dataset" do test "an empty dataset is serialized to an empty string" do assert NQuads.Encoder.encode!(Dataset.new()) == "" end test "statements with IRIs only" do assert NQuads.Encoder.encode!( Dataset.new([ {EX.S1, EX.p1(), EX.O1, EX.G}, {EX.S1, EX.p1(), EX.O2, EX.G}, {EX.S1, EX.p2(), EX.O3, EX.G}, {EX.S2, EX.p3(), EX.O4} ]) ) == """ . . . . """ end test "statements with literals" do assert NQuads.Encoder.encode!( Dataset.new([ {EX.S1, EX.p1(), ~L"foo", EX.G1}, {EX.S1, EX.p1(), ~L"foo"en, EX.G2}, {EX.S1, EX.p2(), 42, EX.G3}, {EX.S2, EX.p3(), RDF.literal("strange things", datatype: EX.custom()), EX.G3} ]) ) == """ "foo" . "foo"@en . "42"^^<#{XSD.integer()}> . "strange things"^^<#{EX.custom()}> . """ end test "statements with blank nodes" do assert NQuads.Encoder.encode!( Dataset.new([ {EX.S1, EX.p1(), RDF.bnode(1)}, {EX.S1, EX.p1(), RDF.bnode("foo"), EX.G}, {EX.S1, EX.p1(), RDF.bnode(:bar)} ]) ) == """ _:b1 . _:bar . _:foo . """ end end describe "stream/2" do dataset = Dataset.new([ {EX.S1, EX.p1(), EX.O1}, {EX.S2, EX.p2(), RDF.bnode("foo"), EX.G}, {EX.S3, EX.p3(), ~L"foo"}, {EX.S3, EX.p3(), ~L"foo"en, EX.G} ]) expected_result = """ . "foo" . _:foo . "foo"@en . """ assert NQuads.Encoder.stream(dataset, mode: :string) |> stream_to_string() == expected_result assert NQuads.Encoder.stream(dataset, mode: :iodata) |> stream_to_string() == expected_result end end