Skip to content

Commit

Permalink
better tests for base support in n3/turtle. Fixes #248.
Browse files Browse the repository at this point in the history
  • Loading branch information
gromgull committed Mar 11, 2013
1 parent df61ace commit 1649edf
Showing 1 changed file with 70 additions and 29 deletions.
99 changes: 70 additions & 29 deletions test/test_n3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from rdflib.namespace import Namespace



test_data = """
# Definitions of terms describing the n3 model
#
Expand Down Expand Up @@ -58,7 +57,6 @@
"""



import unittest
from urllib2 import URLError

Expand Down Expand Up @@ -89,53 +87,95 @@ def testFileName(self):
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")

Expand All @@ -146,10 +186,11 @@ def testIssue156(self):
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()
Expand All @@ -163,15 +204,15 @@ def testModel(self):

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__':
Expand Down

0 comments on commit 1649edf

Please sign in to comment.