diff options
author | Nicholas Car <nick@kurrawong.net> | 2020-03-16 12:24:31 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-16 12:24:31 +1000 |
commit | 94b4cd12615a3b29e834cb665b79e6256be221ef (patch) | |
tree | 5a40867042bac82cd82cdda95716540443b1ef44 /test | |
parent | 3baafa8f5e8b855fe99f033e95b9d27b39296e37 (diff) | |
parent | 1096bb880704c2c9327da2da6d35e63f5d06fb77 (diff) | |
download | rdflib-94b4cd12615a3b29e834cb665b79e6256be221ef.tar.gz |
Merge pull request #649 from tgbugs/master
namespace.py fix compute_qname missing namespaces
Diffstat (limited to 'test')
-rw-r--r-- | test/n3/n3-writer-test-30.n3 | 22 | ||||
-rw-r--r-- | test/n3/n3-writer-test-31.n3 | 15 | ||||
-rw-r--r-- | test/test_n3_suite.py | 12 | ||||
-rw-r--r-- | test/test_namespace.py | 27 | ||||
-rw-r--r-- | test/test_roundtrip.py | 31 | ||||
-rw-r--r-- | test/test_turtle_serialize.py | 18 |
6 files changed, 121 insertions, 4 deletions
diff --git a/test/n3/n3-writer-test-30.n3 b/test/n3/n3-writer-test-30.n3 new file mode 100644 index 00000000..88cf2210 --- /dev/null +++ b/test/n3/n3-writer-test-30.n3 @@ -0,0 +1,22 @@ +# Test full length qnames + +@prefix : <http://example.org/here#> . +@prefix full: <http://example.org/full> . +@prefix pref: <http://example.org/prefix/> . +@prefix more: <http://example.org/prefix/more> . + +# Test namespace generation + +full: :x :y . +:x full: :y . +:x :y full: . + +full: full: full: . + +# Test existing namespace + +more: :x :y . +:x more: :y . +:x :y more: . + +more: more: more: . diff --git a/test/n3/n3-writer-test-31.n3 b/test/n3/n3-writer-test-31.n3 new file mode 100644 index 00000000..1c4494f1 --- /dev/null +++ b/test/n3/n3-writer-test-31.n3 @@ -0,0 +1,15 @@ +# Test unshortenable strict qnames no predicates for xml sanity check + +@prefix : <http://example.org/here#> . +@prefix evil1: <http://example.org/1> . +@prefix evil2: <http://example.org/prefix/1#> . + +# Test namespace generation + +evil1: :x :y . +:x :y evil1: . + +# Test existing namespace + +evil2:1 :x :y . +:x :y evil2:1 . diff --git a/test/test_n3_suite.py b/test/test_n3_suite.py index f6efe34a..21e6bcba 100644 --- a/test/test_n3_suite.py +++ b/test/test_n3_suite.py @@ -1,5 +1,8 @@ import os import sys +import logging + +log = logging.getLogger(__name__) try: from .testutils import check_serialize_parse @@ -18,6 +21,15 @@ def _get_test_files_formats(): elif f.endswith('.n3'): yield fpath, 'n3' +def all_n3_files(): + skiptests = [ + 'test/n3/example-lots_of_graphs.n3', # only n3 can serialize QuotedGraph, no point in testing roundtrip + ] + for fpath, fmt in _get_test_files_formats(): + if fpath in skiptests: + log.debug("Skipping %s, known issue" % fpath) + else: + yield fpath, fmt def test_n3_writing(): for fpath, fmt in _get_test_files_formats(): diff --git a/test/test_namespace.py b/test/test_namespace.py index 9de7ff29..4041433e 100644 --- a/test/test_namespace.py +++ b/test/test_namespace.py @@ -22,6 +22,33 @@ class NamespacePrefixTest(unittest.TestCase): self.assertEqual(g.compute_qname(URIRef("http://blip/blop")), ("ns4", URIRef("http://blip/"), "blop")) + # should return empty qnames correctly + self.assertEqual(g.compute_qname(URIRef("http://foo/bar/")), + ("ns1", URIRef("http://foo/bar/"), "")) + + def test_reset(self): + data = ('@prefix a: <http://example.org/a> .\n' + 'a: <http://example.org/b> <http://example.org/c> .') + graph = Graph().parse(data=data, format='turtle') + for p, n in tuple(graph.namespaces()): + graph.store._IOMemory__namespace.pop(p) + graph.store._IOMemory__prefix.pop(n) + graph.namespace_manager.reset() + self.assertFalse(tuple(graph.namespaces())) + u = URIRef('http://example.org/a') + prefix, namespace, name = graph.namespace_manager.compute_qname(u, generate=True) + self.assertNotEqual(namespace, u) + + def test_reset_preserve_prefixes(self): + data = ('@prefix a: <http://example.org/a> .\n' + 'a: <http://example.org/b> <http://example.org/c> .') + graph = Graph().parse(data=data, format='turtle') + graph.namespace_manager.reset() + self.assertTrue(tuple(graph.namespaces())) + u = URIRef('http://example.org/a') + prefix, namespace, name = graph.namespace_manager.compute_qname(u, generate=True) + self.assertEqual(namespace, u) + def test_n3(self): g = Graph() g.add((URIRef("http://example.com/foo"), diff --git a/test/test_roundtrip.py b/test/test_roundtrip.py index 819c944a..9dfed952 100644 --- a/test/test_roundtrip.py +++ b/test/test_roundtrip.py @@ -5,8 +5,11 @@ import rdflib.compare try: from .test_nt_suite import all_nt_files assert all_nt_files + from .test_n3_suite import all_n3_files + assert all_n3_files except: from test.test_nt_suite import all_nt_files + from test.test_n3_suite import all_n3_files """ Test round-tripping by all serializers/parser that are registerd. @@ -25,9 +28,10 @@ tests roundtripping through rdf/xml with only the literals-02 file SKIP = [ + ('xml', 'test/n3/n3-writer-test-29.n3'), # has predicates that cannot be shortened to strict qnames ('xml', 'test/nt/qname-02.nt'), # uses a property that cannot be qname'd - # uses a property that cannot be qname'd - ('application/rdf+xml', 'test/nt/qname-02.nt'), + ('trix', 'test/n3/strquot.n3'), # contains charachters forbidden by the xml spec + ('xml', 'test/n3/strquot.n3'), # contains charachters forbidden by the xml spec ] @@ -43,6 +47,7 @@ def roundtrip(e, verbose=False): if verbose: print("S:") print(s) + print(s.decode()) g2 = rdflib.ConjunctiveGraph() g2.parse(data=s, format=testfmt) @@ -52,12 +57,12 @@ def roundtrip(e, verbose=False): print("Diff:") print("%d triples in both" % len(both)) print("G1 Only:") - for t in first: + for t in sorted(first): print(t) print("--------------------") print("G2 Only") - for t in second: + for t in sorted(second): print(t) assert rdflib.compare.isomorphic(g1, g2) @@ -88,6 +93,24 @@ def test_cases(): yield roundtrip, (infmt, testfmt, f) +def test_n3(): + global formats + if not formats: + serializers = set( + x.name for x in rdflib.plugin.plugins( + None, rdflib.plugin.Serializer)) + parsers = set( + x.name for x in rdflib.plugin.plugins( + None, rdflib.plugin.Parser)) + formats = parsers.intersection(serializers) + + for testfmt in formats: + if "/" in testfmt: continue # skip double testing + for f, infmt in all_n3_files(): + if (testfmt, f) not in SKIP: + yield roundtrip, (infmt, testfmt, f) + + if __name__ == "__main__": import nose if len(sys.argv) == 1: diff --git a/test/test_turtle_serialize.py b/test/test_turtle_serialize.py index 0b602b34..81f57847 100644 --- a/test/test_turtle_serialize.py +++ b/test/test_turtle_serialize.py @@ -70,6 +70,24 @@ def test_turtle_valid_list(): assert turtle_serializer.isValidList(o) +def test_turtle_namespace(): + graph = Graph() + graph.bind('OBO', 'http://purl.obolibrary.org/obo/') + graph.bind('GENO', 'http://purl.obolibrary.org/obo/GENO_') + graph.bind('RO', 'http://purl.obolibrary.org/obo/RO_') + graph.bind('RO_has_phenotype', + 'http://purl.obolibrary.org/obo/RO_0002200') + graph.add((URIRef('http://example.org'), + URIRef('http://purl.obolibrary.org/obo/RO_0002200'), + URIRef('http://purl.obolibrary.org/obo/GENO_0000385'))) + output = [val for val in + graph.serialize(format='turtle').decode().splitlines() + if not val.startswith('@prefix')] + output = ' '.join(output) + assert 'RO_has_phenotype:' in output + assert 'GENO:0000385' in output + + if __name__ == "__main__": import nose import sys |