port encode_find to rdflib
[htsworkflow.git] / encode_submission / test_encode_find.py
index 38a4c610593f72437698840449e50b10f0335728..6b19ab1f017ecfb43d88e9c98270b6dd6bf90822 100644 (file)
@@ -5,7 +5,7 @@ from datetime import datetime
 import os
 from unittest import TestCase
 
-import RDF
+from rdflib import ConjunctiveGraph, URIRef
 
 import keyring.backend
 
@@ -26,31 +26,31 @@ keyring.set_keyring(MockKeyring())
 from . import encode_find
 from htsworkflow.submission.ucsc import submission_view_url
 from htsworkflow.util.rdfhelp import add_default_schemas, \
-     dump_model, get_model, fromTypedNode
+     dump_model
 from htsworkflow.util.rdfinfer import Infer
 
 SOURCE_PATH = os.path.split(os.path.abspath(__file__))[0]
 
 class TestEncodeFind(TestCase):
     def setUp(self):
-        self.model = get_model()
+        self.model = ConjunctiveGraph()
         add_default_schemas(self.model)
         self.inference = Infer(self.model)
 
     def test_create_status_node_with_uri(self):
         subURL = submission_view_url('5136')
-        submissionUri = RDF.Uri(subURL)
+        submissionUri = URIRef(subURL)
         timestamp = '2011-12-19T12:42:53.048956'
         manualUri = subURL + '/' + timestamp
         nodeUri = encode_find.create_status_node(submissionUri, timestamp)
-        self.assertEqual(str(nodeUri.uri), manualUri)
+        self.assertEqual(str(nodeUri), manualUri)
 
     def test_create_status_node_with_str(self):
         subURL = submission_view_url('5136')
         timestamp = '2011-12-19T12:42:53.048956'
         manualUri = subURL + '/' + timestamp
         nodeUri = encode_find.create_status_node(subURL, timestamp)
-        self.assertEqual(str(nodeUri.uri), manualUri)
+        self.assertEqual(str(nodeUri), manualUri)
 
     def test_parse_submission_page(self):
         timestamp = '2011-12-19T12:42:53.048956'
@@ -64,13 +64,12 @@ class TestEncodeFind(TestCase):
         encode_find.parse_submission_page(self.model, tree, subNode)
         dates = encode_find.get_creation_dates(self.model, subNode)
         self.assertEqual(len(dates), 1)
-        object_date = fromTypedNode(dates[0].object)
+        object_date = dates[0][2].toPython()
         self.assertEqual(object_date, datetime(2011,12,7,15,23,0))
 
     def test_delete_simple_lane(self):
-        model = get_model()
-        parser = RDF.Parser(name='turtle')
-        parser.parse_string_into_model(model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+        model = ConjunctiveGraph()
+        lane_1232_ttl ='''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
 @prefix : <http://www.w3.org/1999/xhtml> .
 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@@ -80,17 +79,17 @@ class TestEncodeFind(TestCase):
     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
     libns:total_unique_locations 5789938 .
 
-''', 'http://jumpgate.caltech.edu/library/')
+'''
+        model.parse(data=lane_1232_ttl, format='turtle', publicID='http://jumpgate.caltech.edu/library/')
         errmsgs = list(self.inference.run_validation())
         self.assertEqual(len(errmsgs), 0)
-        urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/lane/1232'))
+        urn = URIRef('http://jumpgate.caltech.edu/lane/1232')
         encode_find.delete_lane(model, urn)
         self.failUnlessEqual(len(model), 0)
 
     def test_delete_lane_with_mapping(self):
         ontology_size = len(self.model)
-        parser = RDF.Parser(name='turtle')
-        parser.parse_string_into_model(self.model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+        lane_1232_ttl = '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
 @prefix : <http://www.w3.org/1999/xhtml> .
 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@@ -107,18 +106,18 @@ _:bnode110110
     a libns:MappedCount ;
     libns:mapped_to "newcontam_UK.fa"@en ;
     libns:reads 42473 .
-''', 'http://jumpgate.caltech.edu/library/')
+'''
+        self.model.parse(data=lane_1232_ttl, format='turtle', publicID='http://jumpgate.caltech.edu/library/')
         errmsgs = list(self.inference.run_validation())
         self.assertEqual(len(errmsgs), 0)
         self.failUnlessEqual(len(self.model), 8 + ontology_size)
-        urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/lane/1232'))
+        urn = URIRef('http://jumpgate.caltech.edu/lane/1232')
         encode_find.delete_lane(self.model, urn)
         self.failUnlessEqual(len(self.model), 1 + ontology_size)
         # the flowcell triple wasn't deleted.
 
     def test_delete_library(self):
-        parser = RDF.Parser(name='turtle')
-        parser.parse_string_into_model(self.model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+        ttl = '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
 @prefix : <http://www.w3.org/1999/xhtml> .
 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@@ -162,22 +161,21 @@ _:bnode120970
     a libns:MappedCount ;
     libns:mapped_to "newcontam_UK.fa"@en ;
     libns:reads 64 .
-''', 'http://jumpgate.caltech.edu/library')
+'''
+        self.model.parse(data=ttl, format='turtle', publicID='http://jumpgate.caltech.edu/library')
         errmsgs = list(self.inference.run_validation())
         self.assertEqual(len(errmsgs), 0)
-        urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/library/11011/'))
+        urn = URIRef('http://jumpgate.caltech.edu/library/11011/')
         encode_find.delete_library(self.model, urn)
-        q = RDF.Statement(None, encode_find.libraryOntology['reads'], None)
-        stmts = list(self.model.find_statements(q))
+        q = (None, encode_find.libraryOntology['reads'], None)
+        stmts = list(self.model.triples(q))
         self.failUnlessEqual(len(stmts), 1)
-        self.failUnlessEqual(fromTypedNode(stmts[0].object),
-                             64)
+        self.failUnlessEqual(stmts[0][2].toPython(), 64)
 
-        q = RDF.Statement(None, encode_find.libraryOntology['library_id'], None)
-        stmts = list(self.model.find_statements(q))
+        q = (None, encode_find.libraryOntology['library_id'], None)
+        stmts = list(self.model.triples(q))
         self.failUnlessEqual(len(stmts), 1)
-        self.failUnlessEqual(fromTypedNode(stmts[0].object),
-                             '12097')
+        self.failUnlessEqual(stmts[0][2].toPython(), '12097')
 
 def suite():
     from unittest import TestSuite, defaultTestLoader