diff options
author | gromgull <gromgull@gmail.com> | 2013-03-11 15:13:24 +0100 |
---|---|---|
committer | gromgull <gromgull@gmail.com> | 2013-03-11 15:13:24 +0100 |
commit | 1649edfde35a4505154f108c076d57c097fc07b6 (patch) | |
tree | 1596b376d1ea44909f68e171f2600a49646849b4 /test/test_n3.py | |
parent | df61acead90b8dea7a4aab58a6a9691ff5a24ac0 (diff) | |
download | rdflib-1649edfde35a4505154f108c076d57c097fc07b6.tar.gz |
better tests for base support in n3/turtle. Fixes #248.
Diffstat (limited to 'test/test_n3.py')
-rw-r--r-- | test/test_n3.py | 99 |
1 files changed, 70 insertions, 29 deletions
diff --git a/test/test_n3.py b/test/test_n3.py index b0776f2d..02f48d8d 100644 --- a/test/test_n3.py +++ b/test/test_n3.py @@ -2,7 +2,6 @@ from rdflib.term import Literal, URIRef from rdflib.namespace import Namespace - test_data = """ # Definitions of terms describing the n3 model # @@ -58,7 +57,6 @@ n3:context a rdf:Property; rdfs:domain n3:statement; """ - import unittest from urllib2 import URLError @@ -89,53 +87,95 @@ class TestN3Case(unittest.TestCase): self.assertRaises(Exception, g.parse, data=test_data, format="n3") # or.. (challenging comment below): # This isn't the expected result based on my reading of n3 bits - #g.parse(data=test_data, format="n3") - #s = g.value(predicate=URIRef("http://www.example.com/p"), object=URIRef("http://www.example.com/q")) - #self.assertEquals(s, URIRef("http://www.example.org/foo.txt")) + # g.parse(data=test_data, format="n3") + # s = g.value(predicate=URIRef("http://www.example.com/p"), object=URIRef("http://www.example.com/q")) + # self.assertEquals(s, URIRef("http://www.example.org/foo.txt")) - def testBase(self): + def testBaseCumulative(self): """ Test that the n3 parser supports base declarations This is issue #22 """ input = """ -@prefix : <http://example.com> . +@prefix : <http://example.com/> . # default base <foo> :name "Foo" . -# change it +# change it @base <http://example.com/doc/> . <bar> :name "Bar" . -# and change it more - they are cummalative +# and change it more - they are cumulative @base <doc2/> . <bing> :name "Bing" . +# unless abosulute +@base <http://test.com/> . +<bong> :name "Bong" . + """ g = Graph() g.parse(data=input, format="n3") + print list(g) + self.assertTrue((None, None, Literal('Foo')) in g) + self.assertTrue( + (URIRef('http://example.com/doc/bar'), None, None) in g) + self.assertTrue( + (URIRef('http://example.com/doc/doc2/bing'), None, None) in g) + self.assertTrue((URIRef('http://test.com/bong'), None, None) in g) + + def testBaseExplicit(self): + """ + Test that the n3 parser supports resolving relative URIs + and that base will override + """ + + input = """ +@prefix : <http://example.com/> . +# default base +<foo> :name "Foo" . +# change it +@base <http://example.com/doc/> . +<bar> :name "Bar" . +""" + g = Graph() + g.parse(data=input, publicID='http://blah.com/', format="n3") + print list(g) + self.assertTrue( + (URIRef('http://blah.com/foo'), None, Literal('Foo')) in g) + self.assertTrue( + (URIRef('http://example.com/doc/bar'), None, None) in g) + + def testBaseSerialize(self): + g = Graph() + g.add((URIRef('http://example.com/people/Bob'), URIRef( + 'urn:knows'), URIRef('http://example.com/people/Linda'))) + s = g.serialize(base='http://example.com/', format='n3') + self.assertTrue('<people/Bob>' in s) + g2 = ConjunctiveGraph() + g2.parse(data=s, publicID='http://example.com/', format='n3') + self.assertEqual(list(g), list(g2)) + + def testIssue23(self): + input = """<http://example.com/article1> <http://example.com/title> "this word is in \u201Cquotes\u201D".""" - def testIssue23(self): - input="""<http://example.com/article1> <http://example.com/title> "this word is in \u201Cquotes\u201D".""" - g = Graph() g.parse(data=input, format="n3") # Note difference in case of hex code, cwm allows lower-case - input="""<http://example.com/article1> <http://example.com/title> "this word is in \u201cquotes\u201d".""" - + input = """<http://example.com/article1> <http://example.com/title> "this word is in \u201cquotes\u201d".""" + g.parse(data=input, format="n3") - def testIssue29(self): - input="""@prefix foo-bar: <http://example.org/> . + def testIssue29(self): + input = """@prefix foo-bar: <http://example.org/> . foo-bar:Ex foo-bar:name "Test" . """ - + g = Graph() g.parse(data=input, format="n3") - - def testIssue68(self): - input="""@prefix : <http://some.url/pome#>.\n\n:Brecon a :Place;\n\t:hasLord\n\t\t:Bernard_of_Neufmarch\xc3\xa9 .\n """ - + def testIssue68(self): + input = """@prefix : <http://some.url/pome#>.\n\n:Brecon a :Place;\n\t:hasLord\n\t\t:Bernard_of_Neufmarch\xc3\xa9 .\n """ + g = Graph() g.parse(data=input, format="n3") @@ -146,10 +186,11 @@ foo-bar:Ex foo-bar:name "Test" . """ g = Graph() g.parse("test/n3/issue156.n3", format="n3") - def testDotInPrefix(self): - g=Graph() - g.parse(data="@prefix a.1: <http://example.org/> .\n a.1:cake <urn:x> <urn:y> . \n", format='n3') - + def testDotInPrefix(self): + g = Graph() + g.parse( + data="@prefix a.1: <http://example.org/> .\n a.1:cake <urn:x> <urn:y> . \n", + format='n3') def testModel(self): g = ConjunctiveGraph() @@ -163,15 +204,15 @@ foo-bar:Ex foo-bar:name "Test" . """ g.close() - def testParse(self): g = ConjunctiveGraph() try: - g.parse("http://groups.csail.mit.edu/dig/2005/09/rein/examples/troop42-policy.n3", format="n3") + g.parse( + "http://groups.csail.mit.edu/dig/2005/09/rein/examples/troop42-policy.n3", format="n3") except URLError: from nose import SkipTest - raise SkipTest('No network to retrieve the information, skipping test') - + raise SkipTest( + 'No network to retrieve the information, skipping test') if __name__ == '__main__': |