core: clean up RDF.Vocabulary.Namespace tests

This commit is contained in:
Marcel Otto 2017-06-10 03:43:51 +02:00
parent 2a8cf08c8e
commit 7c34231bb5
2 changed files with 163 additions and 164 deletions

View file

@ -1,2 +1,2 @@
<http://example.com/example2/foo> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://example.com/example2/Bar> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Resource> .
<http://example.com/from_ntriples/foo> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://example.com/from_ntriples/Bar> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Resource> .

View file

@ -7,6 +7,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
alias RDF.Description
defmodule TestNS do
use RDF.Vocabulary.Namespace
@ -18,36 +19,35 @@ defmodule RDF.Vocabulary.NamespaceTest do
base_uri: "http://example.com/strict#",
terms: ~w[foo bar]
defvocab Example1,
base_uri: "http://example.com/example1#",
defvocab ExampleFromGraph,
base_uri: "http://example.com/from_graph#",
data: RDF.Graph.new([
{~I<http://example.com/example1#foo>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>},
{~I<http://example.com/example1#Bar>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/2000/01/rdf-schema#Resource>}
{~I<http://example.com/from_graph#foo>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>},
{~I<http://example.com/from_graph#Bar>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/2000/01/rdf-schema#Resource>}
])
defvocab Example23,
base_uri: "http://example.com/example23#",
defvocab ExampleFromDataset,
base_uri: "http://example.com/from_dataset#",
data: RDF.Dataset.new([
{~I<http://example.com/example23#foo>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>},
{~I<http://example.com/example23#Bar>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/2000/01/rdf-schema#Resource>, ~I<http://example.com/example23#Graph>}
{~I<http://example.com/from_dataset#foo>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#Property>},
{~I<http://example.com/from_dataset#Bar>, ~I<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, ~I<http://www.w3.org/2000/01/rdf-schema#Resource>, ~I<http://example.com/from_dataset#Graph>}
])
defvocab Example2,
base_uri: "http://example.com/example2/",
defvocab ExampleFromNTriplesFile,
base_uri: "http://example.com/from_ntriples/",
file: "test/data/vocab_ns_example2.nt"
defvocab Example3,
base_uri: "http://example.com/example3#",
defvocab StrictExampleFromTerms,
base_uri: "http://example.com/strict_from_terms#",
terms: ~w[foo Bar]
defvocab Example4,
base_uri: "http://example.com/example4#",
defvocab NonStrictExampleFromTerms,
base_uri: "http://example.com/non_strict_from_terms#",
terms: ~w[foo Bar],
strict: false
defvocab Example5,
base_uri: "http://example.com/example5#",
defvocab StrictExampleFromAliasedTerms,
base_uri: "http://example.com/strict_from_aliased_terms#",
terms: ~w[term1 Term2 Term-3 term-4],
alias: [
Term1: "term1",
@ -56,8 +56,8 @@ defmodule RDF.Vocabulary.NamespaceTest do
term4: "term-4",
]
defvocab Example6,
base_uri: "http://example.com/example6#",
defvocab NonStrictExampleFromAliasedTerms,
base_uri: "http://example.com/non_strict_from_aliased_terms#",
terms: ~w[],
alias: [
Term1: "term1",
@ -66,13 +66,20 @@ defmodule RDF.Vocabulary.NamespaceTest do
term4: "term-4",
],
strict: false
defvocab ExampleWithSynonymAliases,
base_uri: "http://example.com/ex#",
terms: ~w[bar Bar],
alias: [foo: "bar", baz: "bar",
Foo: "Bar", Baz: "Bar"]
end
describe "defvocab" do
describe "defvocab with bad base uri" do
test "without a base_uri, an error is raised" do
assert_raise KeyError, fn ->
defmodule BadNS1 do
defmodule NSWithoutBaseURI do
use RDF.Vocabulary.Namespace
defvocab Example, terms: []
@ -82,7 +89,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
test "when the base_uri doesn't end with '/' or '#', an error is raised" do
assert_raise RDF.Namespace.InvalidVocabBaseURIError, fn ->
defmodule BadNS2 do
defmodule NSWithInvalidBaseURI1 do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -94,7 +101,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
test "when the base_uri isn't a valid URI, an error is raised" do
assert_raise RDF.Namespace.InvalidVocabBaseURIError, fn ->
defmodule BadNS3 do
defmodule NSWithInvalidBaseURI2 do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -103,7 +110,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
end
end
assert_raise RDF.Namespace.InvalidVocabBaseURIError, fn ->
defmodule BadNS4 do
defmodule NSWithInvalidBaseURI3 do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -112,39 +119,44 @@ defmodule RDF.Vocabulary.NamespaceTest do
end
end
end
test "special terms" do
defmodule NSEdge do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/ex#",
terms: ~w[nil true false]
end
alias NSEdge.Example
assert Example.nil == ~I<http://example.com/ex#nil>
assert Example.true == ~I<http://example.com/ex#true>
assert Example.false == ~I<http://example.com/ex#false>
end
end
describe "defvocab with bad terms" do
test "when the given file not found, an error is raised" do
assert_raise File.Error, fn ->
defmodule BadNS5 do
defmodule NSWithMissingVocabFile do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/ex5#",
base_uri: "http://example.com/ex#",
file: "something.nt"
end
end
end
end
test "defvocab with special terms" do
defmodule NSofEdgeCases do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/ex#",
terms: ~w[nil true false]
end
alias NSofEdgeCases.Example
assert Example.nil == ~I<http://example.com/ex#nil>
assert Example.true == ~I<http://example.com/ex#true>
assert Example.false == ~I<http://example.com/ex#false>
end
describe "defvocab with bad aliases" do
test "when an alias contains invalid characters, an error is raised" do
assert_raise RDF.Namespace.InvalidAliasError, fn ->
defmodule BadNS12 do
defmodule NSWithInvalidTerms do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -157,11 +169,11 @@ defmodule RDF.Vocabulary.NamespaceTest do
test "when trying to map an already existing term, an error is raised" do
assert_raise RDF.Namespace.InvalidAliasError, fn ->
defmodule BadNS6 do
defmodule NSWithInvalidAliases1 do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/ex6#",
base_uri: "http://example.com/ex#",
terms: ~w[foo bar],
alias: [foo: "bar"]
end
@ -170,11 +182,11 @@ defmodule RDF.Vocabulary.NamespaceTest do
test "when strict and trying to map to a term not in the vocabulary, an error is raised" do
assert_raise RDF.Namespace.InvalidAliasError, fn ->
defmodule BadNS7 do
defmodule NSWithInvalidAliases2 do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/ex7#",
base_uri: "http://example.com/ex#",
terms: ~w[],
alias: [foo: "bar"]
end
@ -183,88 +195,24 @@ defmodule RDF.Vocabulary.NamespaceTest do
test "when defining an alias for an alias, an error is raised" do
assert_raise RDF.Namespace.InvalidAliasError, fn ->
defmodule BadNS8 do
defmodule NSWithInvalidAliases3 do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/ex8#",
base_uri: "http://example.com/ex#",
terms: ~w[bar],
alias: [foo: "bar", baz: "foo"]
end
end
end
test "defining multiple aliases for a term" do
defmodule BadNS9 do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/ex8#",
terms: ~w[bar Bar],
alias: [foo: "bar", baz: "bar",
Foo: "Bar", Baz: "Bar"]
end
alias BadNS9.Example
assert Example.foo == Example.baz
assert RDF.uri(Example.foo) == RDF.uri(Example.baz)
end
end
test "__base_uri__ returns the base_uri" do
alias TestNS.Example1, as: HashVocab
alias TestNS.Example2, as: SlashVocab
assert HashVocab.__base_uri__ == "http://example.com/example1#"
assert SlashVocab.__base_uri__ == "http://example.com/example2/"
end
describe "__terms__" do
alias TestNS.{Example1, Example23, Example5}
test "includes all defined terms" do
assert length(Example1.__terms__) == 2
assert :foo in Example1.__terms__
assert :Bar in Example1.__terms__
assert length(Example23.__terms__) == 2
assert :foo in Example23.__terms__
assert :Bar in Example23.__terms__
end
test "includes aliases" do
assert length(Example5.__terms__) == 8
assert :term1 in Example5.__terms__
assert :Term1 in Example5.__terms__
assert :term2 in Example5.__terms__
assert :Term2 in Example5.__terms__
assert :Term3 in Example5.__terms__
assert :term4 in Example5.__terms__
assert :"Term-3" in Example5.__terms__
assert :"term-4" in Example5.__terms__
end
end
test "__uris__ returns all URIs of the vocabulary" do
alias TestNS.{Example1, Example5}
assert length(Example1.__uris__) == 2
assert RDF.uri(Example1.foo) in Example1.__uris__
assert RDF.uri(Example1.Bar) in Example1.__uris__
assert length(Example5.__uris__) == 4
assert RDF.uri(Example5.Term1) in Example5.__uris__
assert RDF.uri(Example5.term2) in Example5.__uris__
assert RDF.uri(Example5.Term3) in Example5.__uris__
assert RDF.uri(Example5.term4) in Example5.__uris__
end
describe "invalid character handling" do
describe "defvocab invalid character handling" do
test "when a term contains unallowed characters and no alias defined, it fails when invalid_characters = :fail" do
assert_raise RDF.Namespace.InvalidTermError, ~r/Foo-bar.*foo-bar/s,
fn ->
defmodule BadNS10 do
defmodule NSWithInvalidTerms1 do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/example#",
@ -274,7 +222,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
end
test "when a term contains unallowed characters it does not fail when invalid_characters = :ignore" do
defmodule BadNS11 do
defmodule NSWithInvalidTerms2 do
use RDF.Vocabulary.Namespace
defvocab Example,
base_uri: "http://example.com/example#",
@ -282,14 +230,12 @@ defmodule RDF.Vocabulary.NamespaceTest do
invalid_characters: :ignore
end
end
end
describe "case violation handling" do
describe "defvocab case violation handling" do
test "aliases can fix case violations" do
defmodule NS23 do
defmodule NSWithBadCasedTerms1 do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -307,7 +253,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
end
test "when case_violations == :ignore is set, case violations are ignored" do
defmodule NS24 do
defmodule NSWithBadCasedTerms2 do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -327,7 +273,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
test "a capitalized property without an alias and :case_violations == :fail, raises an error" do
assert_raise RDF.Namespace.InvalidTermError, ~r<http://example\.com/ex#Foo>s, fn ->
defmodule BadNS13 do
defmodule NSWithBadCasedTerms3 do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -342,7 +288,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
test "a lowercased non-property without an alias and :case_violations == :fail, raises an error" do
assert_raise RDF.Namespace.InvalidTermError, ~r<http://example\.com/ex#bar>s, fn ->
defmodule BadNS14 do
defmodule NSWithBadCasedTerms4 do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -358,7 +304,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
test "a capitalized alias for a property and :case_violations == :fail, raises an error" do
assert_raise RDF.Namespace.InvalidTermError, fn ->
defmodule BadNS15 do
defmodule NSWithBadCasedTerms5 do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -374,7 +320,7 @@ defmodule RDF.Vocabulary.NamespaceTest do
test "a lowercased alias for a non-property and :case_violations == :fail, raises an error" do
assert_raise RDF.Namespace.InvalidTermError, fn ->
defmodule BadNS16 do
defmodule NSWithBadCasedTerms6 do
use RDF.Vocabulary.Namespace
defvocab Example,
@ -387,7 +333,54 @@ defmodule RDF.Vocabulary.NamespaceTest do
end
end
end
end
test "__base_uri__ returns the base_uri" do
alias TestNS.ExampleFromGraph, as: HashVocab
alias TestNS.ExampleFromNTriplesFile, as: SlashVocab
assert HashVocab.__base_uri__ == "http://example.com/from_graph#"
assert SlashVocab.__base_uri__ == "http://example.com/from_ntriples/"
end
test "__uris__ returns all URIs of the vocabulary" do
alias TestNS.ExampleFromGraph, as: Example1
assert length(Example1.__uris__) == 2
assert RDF.uri(Example1.foo) in Example1.__uris__
assert RDF.uri(Example1.Bar) in Example1.__uris__
alias TestNS.StrictExampleFromAliasedTerms, as: Example2
assert length(Example2.__uris__) == 4
assert RDF.uri(Example2.Term1) in Example2.__uris__
assert RDF.uri(Example2.term2) in Example2.__uris__
assert RDF.uri(Example2.Term3) in Example2.__uris__
assert RDF.uri(Example2.term4) in Example2.__uris__
end
describe "__terms__" do
alias TestNS.{ExampleFromGraph, ExampleFromDataset, StrictExampleFromAliasedTerms}
test "includes all defined terms" do
assert length(ExampleFromGraph.__terms__) == 2
for term <- ~w[foo Bar]a do
assert term in ExampleFromGraph.__terms__
end
assert length(ExampleFromDataset.__terms__) == 2
for term <- ~w[foo Bar]a do
assert term in ExampleFromDataset.__terms__
end
end
test "includes aliases" do
assert length(StrictExampleFromAliasedTerms.__terms__) == 8
for term <- ~w[term1 Term1 term2 Term2 Term3 term4 Term-3 term-4]a do
assert term in StrictExampleFromAliasedTerms.__terms__
end
end
end
@ -396,104 +389,111 @@ defmodule RDF.Vocabulary.NamespaceTest do
assert_raise RDF.Namespace.UndefinedTermError, fn -> RDF.uri(:foo) end
end
describe "term resolution in a strict vocab namespace" do
alias TestNS.{Example1, Example2, Example3}
alias TestNS.{ExampleFromGraph, ExampleFromNTriplesFile, StrictExampleFromTerms}
test "undefined terms" do
assert_raise UndefinedFunctionError, fn ->
Example1.undefined
ExampleFromGraph.undefined
end
assert_raise UndefinedFunctionError, fn ->
Example2.undefined
ExampleFromNTriplesFile.undefined
end
assert_raise UndefinedFunctionError, fn ->
Example3.undefined
StrictExampleFromTerms.undefined
end
assert_raise RDF.Namespace.UndefinedTermError, fn ->
RDF.Namespace.resolve_term(TestNS.Example1.Undefined)
RDF.Namespace.resolve_term(TestNS.ExampleFromGraph.Undefined)
end
assert_raise RDF.Namespace.UndefinedTermError, fn ->
RDF.Namespace.resolve_term(Example2.Undefined)
RDF.Namespace.resolve_term(ExampleFromNTriplesFile.Undefined)
end
assert_raise RDF.Namespace.UndefinedTermError, fn ->
RDF.Namespace.resolve_term(Example3.Undefined)
RDF.Namespace.resolve_term(StrictExampleFromTerms.Undefined)
end
end
test "lowercased terms" do
assert Example1.foo == URI.parse("http://example.com/example1#foo")
assert RDF.uri(Example1.foo) == URI.parse("http://example.com/example1#foo")
assert ExampleFromGraph.foo == URI.parse("http://example.com/from_graph#foo")
assert RDF.uri(ExampleFromGraph.foo) == URI.parse("http://example.com/from_graph#foo")
assert Example2.foo == URI.parse("http://example.com/example2/foo")
assert RDF.uri(Example2.foo) == URI.parse("http://example.com/example2/foo")
assert ExampleFromNTriplesFile.foo == URI.parse("http://example.com/from_ntriples/foo")
assert RDF.uri(ExampleFromNTriplesFile.foo) == URI.parse("http://example.com/from_ntriples/foo")
assert Example3.foo == URI.parse("http://example.com/example3#foo")
assert RDF.uri(Example3.foo) == URI.parse("http://example.com/example3#foo")
assert StrictExampleFromTerms.foo == URI.parse("http://example.com/strict_from_terms#foo")
assert RDF.uri(StrictExampleFromTerms.foo) == URI.parse("http://example.com/strict_from_terms#foo")
end
test "captitalized terms" do
assert RDF.uri(Example1.Bar) == URI.parse("http://example.com/example1#Bar")
assert RDF.uri(Example2.Bar) == URI.parse("http://example.com/example2/Bar")
assert RDF.uri(Example3.Bar) == URI.parse("http://example.com/example3#Bar")
assert RDF.uri(ExampleFromGraph.Bar) == URI.parse("http://example.com/from_graph#Bar")
assert RDF.uri(ExampleFromNTriplesFile.Bar) == URI.parse("http://example.com/from_ntriples/Bar")
assert RDF.uri(StrictExampleFromTerms.Bar) == URI.parse("http://example.com/strict_from_terms#Bar")
end
end
describe "term resolution in a non-strict vocab namespace" do
alias TestNS.Example4
alias TestNS.NonStrictExampleFromTerms
test "undefined lowercased terms" do
assert Example4.random == URI.parse("http://example.com/example4#random")
assert NonStrictExampleFromTerms.random == URI.parse("http://example.com/non_strict_from_terms#random")
end
test "undefined capitalized terms" do
assert RDF.uri(Example4.Random) == URI.parse("http://example.com/example4#Random")
assert RDF.uri(NonStrictExampleFromTerms.Random) == URI.parse("http://example.com/non_strict_from_terms#Random")
end
test "defined lowercase terms" do
assert Example4.foo == URI.parse("http://example.com/example4#foo")
assert NonStrictExampleFromTerms.foo == URI.parse("http://example.com/non_strict_from_terms#foo")
end
test "defined capitalized terms" do
assert RDF.uri(Example4.Bar) == URI.parse("http://example.com/example4#Bar")
assert RDF.uri(NonStrictExampleFromTerms.Bar) == URI.parse("http://example.com/non_strict_from_terms#Bar")
end
end
describe "term resolution of aliases on a strict vocabulary" do
alias TestNS.Example5
alias TestNS.StrictExampleFromAliasedTerms, as: Example
test "the alias resolves to the correct URI" do
assert RDF.uri(Example5.Term1) == URI.parse("http://example.com/example5#term1")
assert RDF.uri(Example5.term2) == URI.parse("http://example.com/example5#Term2")
assert RDF.uri(Example5.Term3) == URI.parse("http://example.com/example5#Term-3")
assert RDF.uri(Example5.term4) == URI.parse("http://example.com/example5#term-4")
assert RDF.uri(Example.Term1) == URI.parse("http://example.com/strict_from_aliased_terms#term1")
assert RDF.uri(Example.term2) == URI.parse("http://example.com/strict_from_aliased_terms#Term2")
assert RDF.uri(Example.Term3) == URI.parse("http://example.com/strict_from_aliased_terms#Term-3")
assert RDF.uri(Example.term4) == URI.parse("http://example.com/strict_from_aliased_terms#term-4")
end
test "the old term remains resolvable" do
assert RDF.uri(Example5.term1) == URI.parse("http://example.com/example5#term1")
assert RDF.uri(Example5.Term2) == URI.parse("http://example.com/example5#Term2")
assert RDF.uri(Example.term1) == URI.parse("http://example.com/strict_from_aliased_terms#term1")
assert RDF.uri(Example.Term2) == URI.parse("http://example.com/strict_from_aliased_terms#Term2")
end
test "defining multiple aliases for a term" do
alias TestNS.ExampleWithSynonymAliases, as: Example
assert Example.foo == Example.baz
assert RDF.uri(Example.foo) == RDF.uri(Example.baz)
end
end
describe "term resolution of aliases on a non-strict vocabulary" do
alias TestNS.Example6
alias TestNS.NonStrictExampleFromAliasedTerms, as: Example
test "the alias resolves to the correct URI" do
assert RDF.uri(Example6.Term1) == URI.parse("http://example.com/example6#term1")
assert RDF.uri(Example6.term2) == URI.parse("http://example.com/example6#Term2")
assert RDF.uri(Example6.Term3) == URI.parse("http://example.com/example6#Term-3")
assert RDF.uri(Example6.term4) == URI.parse("http://example.com/example6#term-4")
assert RDF.uri(Example.Term1) == URI.parse("http://example.com/non_strict_from_aliased_terms#term1")
assert RDF.uri(Example.term2) == URI.parse("http://example.com/non_strict_from_aliased_terms#Term2")
assert RDF.uri(Example.Term3) == URI.parse("http://example.com/non_strict_from_aliased_terms#Term-3")
assert RDF.uri(Example.term4) == URI.parse("http://example.com/non_strict_from_aliased_terms#term-4")
end
test "the old term remains resolvable" do
assert RDF.uri(Example6.term1) == URI.parse("http://example.com/example6#term1")
assert RDF.uri(Example6.Term2) == URI.parse("http://example.com/example6#Term2")
assert RDF.uri(Example.term1) == URI.parse("http://example.com/non_strict_from_aliased_terms#term1")
assert RDF.uri(Example.Term2) == URI.parse("http://example.com/non_strict_from_aliased_terms#Term2")
end
end
describe "Description DSL" do
describe "description DSL" do
alias TestNS.{EX, EXS}
test "one statement with a strict property term" do
@ -535,7 +535,6 @@ defmodule RDF.Vocabulary.NamespaceTest do
])
end
test "one statement with a non-strict property term" do
assert EX.p(EX.S, EX.O) == Description.new(EX.S, EX.p, EX.O)
end