from datetime import datetime
from htsworkflow.util.rdfhelp import \
+ add_default_schemas, \
blankOrUri, \
+ dcNS, \
dump_model, \
fromTypedNode, \
get_model, \
+ guess_parser, \
+ guess_parser_by_extension, \
load_string_into_model, \
+ owlNS, \
+ rdfNS, \
rdfsNS, \
+ remove_schemas, \
toTypedNode, \
- simplifyUri, \
+ stripNamespace, \
+ simplify_uri, \
sanitize_literal, \
xsdNS
self.assertEqual(fromTypedNode(toTypedNode(long_datetime)),
long_datetime)
- def test_simplify_uri(self):
+ def test_strip_namespace_uri(self):
nsOrg = RDF.NS('example.org/example#')
nsCom = RDF.NS('example.com/example#')
term = 'foo'
node = nsOrg[term]
- self.failUnlessEqual(simplifyUri(nsOrg, node), term)
- self.failUnlessEqual(simplifyUri(nsCom, node), None)
- self.failUnlessEqual(simplifyUri(nsOrg, node.uri), term)
+ self.failUnlessEqual(stripNamespace(nsOrg, node), term)
+ self.failUnlessEqual(stripNamespace(nsCom, node), None)
+ self.failUnlessEqual(stripNamespace(nsOrg, node.uri), term)
- def test_simplify_uri_exceptions(self):
+ def test_strip_namespace_exceptions(self):
nsOrg = RDF.NS('example.org/example#')
nsCom = RDF.NS('example.com/example#')
node = toTypedNode('bad')
- self.failUnlessRaises(ValueError, simplifyUri, nsOrg, node)
- self.failUnlessRaises(ValueError, simplifyUri, nsOrg, nsOrg)
+ self.failUnlessRaises(ValueError, stripNamespace, nsOrg, node)
+ self.failUnlessRaises(ValueError, stripNamespace, nsOrg, nsOrg)
+
+ def test_simplify_uri(self):
+ DATA = [('http://asdf.org/foo/bar', 'bar'),
+ ('http://asdf.org/foo/bar#bleem', 'bleem'),
+ ('http://asdf.org/foo/bar/', 'bar'),
+ ('http://asdf.org/foo/bar?was=foo', 'was=foo')]
+
+ for uri, expected in DATA:
+ self.assertEqual(simplify_uri(uri), expected)
+
+ for uri, expected in DATA:
+ n = RDF.Uri(uri)
+ self.assertEqual(simplify_uri(n), expected)
+
+ for uri, expected in DATA:
+ n = RDF.Node(RDF.Uri(uri))
+ self.assertEqual(simplify_uri(n), expected)
+
+ # decoding literals is questionable
+ n = toTypedNode('http://foo/bar')
+ self.assertRaises(ValueError, simplify_uri, n)
def test_owl_import(self):
path, name = os.path.split(__file__)
- loc = 'file://'+os.path.abspath(path)+'/'
+ #loc = 'file://'+os.path.abspath(path)+'/'
+ loc = os.path.abspath(path)+'/'
model = get_model()
fragment = '''
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
self.failUnlessEqual(hello_clean.literal_value['string'],
hello_text)
+ def test_sanitize_literal_empty_string(self):
+ value = ""
+ value_node = RDF.Node(value)
+ self.assertEqual(str(sanitize_literal(value_node)), value)
+
def test_sanitize_literal_html(self):
hello = "hello <a onload='javascript:alert(\"foo\");' href='http://google.com'>google.com</a>, whats up?"
hello_clean = 'hello <a href="http://google.com">google.com</a>, whats up?'
hostile_result = """hi <b>there</b>"""
self.failUnlessEqual(str(hostile_sanitized), hostile_result)
+ def test_guess_parser_from_file(self):
+ DATA = [
+ ('/a/b/c.rdf', 'rdfxml'),
+ ('/a/b/c.xml', 'rdfxml'),
+ ('/a/b/c.html', 'rdfa'),
+ ('/a/b/c.turtle', 'turtle')]
+ for path, parser in DATA:
+ self.assertEqual(guess_parser_by_extension(path), parser)
+ self.assertEqual(guess_parser(None, path), parser)
+
+ DATA = [
+ ('application/rdf+xml', 'http://a.org/b/c', 'rdfxml'),
+ ('application/x-turtle', 'http://a.org/b/c', 'turtle'),
+ ('text/html', 'http://a.org/b/c', 'rdfa')
+ ]
+ for contenttype, url, parser in DATA:
+ self.assertEqual(guess_parser(contenttype, url), parser)
+
+ class TestRDFSchemas(unittest.TestCase):
+ def test_rdf_schema(self):
+ """Does it basically work?
+ """
+ model = get_model()
+ self.assertEqual(model.size(), 0)
+ add_default_schemas(model)
+ self.assertGreater(model.size(), 0)
+ remove_schemas(model)
+ self.assertEqual(model.size(), 0)
+
+ def test_included_schemas(self):
+ model = get_model()
+ add_default_schemas(model)
+
+ # rdf test
+ s = RDF.Statement(rdfNS[''], dcNS['title'], None)
+ title = model.get_target(rdfNS[''], dcNS['title'])
+ self.assertTrue(title is not None)
+
+ s = RDF.Statement(rdfNS['Property'], rdfNS['type'], rdfsNS['Class'])
+ self.assertTrue(model.contains_statement(s))
+
+ # rdfs test
+ s = RDF.Statement(rdfsNS['Class'], rdfNS['type'], rdfsNS['Class'])
+ self.assertTrue(model.contains_statement(s))
+
+ s = RDF.Statement(owlNS['inverseOf'], rdfNS['type'],
+ rdfNS['Property'])
+ self.assertTrue(model.contains_statement(s))
+
def suite():
return unittest.makeSuite(TestRDFHelp, 'test')