Minor tweaks to deal with the older version of librdf on ubuntu 10.04
[htsworkflow.git] / htsworkflow / util / test / test_rdfhelp.py
index 34c3200a909de310c8fb9dd48bcb04605aa8a3c9..dfa4b971c70c1bcd28723d8b3b416c331d6d84c5 100644 (file)
@@ -6,14 +6,22 @@ import types
 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
 
@@ -102,27 +110,49 @@ try:
             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#> .
@@ -172,6 +202,59 @@ _:a owl:imports "{loc}extra.turtle" .
             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'),
+                ('http://foo.bar/bleem.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'),
+                ('text/html', 'http://a.org/b/c.html', 'rdfa'),
+                ('text/plain', 'http://a.org/b/c.turtle', 'turtle'),
+                ('text/plain', 'http://a.org/b/c', 'guess')
+            ]
+            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.assertTrue(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')