Run the library detail page through RDF validation.
[htsworkflow.git] / htsworkflow / frontend / samples / tests.py
index 476ede36ebd9c697172650404dd496f34f66e5cb..111e29e8f43f5a9edb80a880e9d354b380d30bdd 100644 (file)
@@ -20,7 +20,7 @@ from htsworkflow.frontend.samples.views import \
 
 from htsworkflow.frontend.auth import apidata
 from htsworkflow.util.conversion import unicode_or_none
-
+from htsworkflow.util.ethelp import validate_xhtml
 
 class LibraryTestCase(TestCase):
     fixtures = ['test_samples.json']
@@ -52,7 +52,6 @@ class SampleWebTestCase(TestCase):
     fixtures = ['test_samples.json']
 
     def test_library_info(self):
-
         for lib in Library.objects.all():
             lib_dict = library_dict(lib.id)
             url = '/samples/library/%s/json' % (lib.id,)
@@ -95,12 +94,24 @@ class SampleWebTestCase(TestCase):
                 # some specific tests
                 if lib.id == '10981':
                     # test a case where there is no known status
-                    lane_set = {u'status': u'Unknown', u'paired_end': True, u'read_length': 75, u'lane_number': 1, u'flowcell': u'303TUAAXX', u'status_code': None}
+                    lane_set = {u'status': u'Unknown',
+                                u'paired_end': True,
+                                u'read_length': 75,
+                                u'lane_number': 1,
+                                u'lane_id': 1193,
+                                u'flowcell': u'303TUAAXX',
+                                u'status_code': None}
                     self.failUnlessEqual(len(d['lane_set']), 1)
                     self.failUnlessEqual(d['lane_set'][0], lane_set)
                 elif lib.id == '11016':
                     # test a case where there is a status
-                    lane_set = {u'status': 'Good', u'paired_end': True, u'read_length': 75, u'lane_number': 5, u'flowcell': u'303TUAAXX', u'status_code': 2}
+                    lane_set = {u'status': 'Good',
+                                u'paired_end': True,
+                                u'read_length': 75,
+                                u'lane_number': 5,
+                                u'lane_id': 1197,
+                                u'flowcell': u'303TUAAXX',
+                                u'status_code': 2}
                     self.failUnlessEqual(len(d['lane_set']), 1)
                     self.failUnlessEqual(d['lane_set'][0], lane_set)
 
@@ -127,7 +138,98 @@ class SampleWebTestCase(TestCase):
         response = self.client.get('/samples/library/10981/json', apidata)
         self.failUnlessEqual(response.status_code, 200)
 
-
+    def test_library_rdf(self):
+        import RDF
+        from htsworkflow.util.rdfhelp import get_model, \
+             dump_model, \
+             fromTypedNode, \
+             load_string_into_model, \
+             rdfNS, \
+             libraryOntology
+        model = get_model()
+
+        response = self.client.get('/library/10981/')
+        self.assertEqual(response.status_code, 200)
+        content = response.content
+        load_string_into_model(model, 'rdfa', content)
+
+        body = """prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+        prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#>
+
+        select ?library ?name ?library_id ?gel_cut ?made_by
+        where {
+           ?library a libns:library ;
+                    libns:name ?name ;
+                    libns:library_id ?library_id ;
+                    libns:gel_cut ?gel_cut ;
+                    libns:made_by ?made_by
+        }"""
+        query = RDF.SPARQLQuery(body)
+        for r in query.execute(model):
+            self.assertEqual(fromTypedNode(r['library_id']), u'10981')
+            self.assertEqual(fromTypedNode(r['name']),
+                             u'Paired End Multiplexed Sp-BAC')
+            self.assertEqual(fromTypedNode(r['gel_cut']), 400)
+            self.assertEqual(fromTypedNode(r['made_by']), u'Igor')
+
+        state = validate_xhtml(content)
+        if state is not None:
+            self.assertTrue(state)
+
+        # validate a library page.
+        from htsworkflow.util.rdfhelp import add_default_schemas
+        from htsworkflow.util.rdfinfer import Infer
+        add_default_schemas(model)
+        inference = Infer(model)
+        ignored = {'Missing type for: http://localhost/'}
+        errmsgs = [msg for msg in inference.run_validation()
+                   if msg not in ignored ]
+        self.assertEqual(len(errmsgs), 0)
+
+    def test_library_index_rdfa(self):
+        from htsworkflow.util.rdfhelp import \
+             add_default_schemas, get_model, load_string_into_model, \
+             dump_model
+        from htsworkflow.util.rdfinfer import Infer
+
+        model = get_model()
+        add_default_schemas(model)
+        inference = Infer(model)
+
+        response = self.client.get('/library/')
+        self.assertEqual(response.status_code, 200)
+        load_string_into_model(model, 'rdfa', response.content)
+
+        ignored = {'Missing type for: http://localhost/'}
+        errmsgs = [msg for msg in inference.run_validation()
+                   if msg not in ignored ]
+        self.assertEqual(len(errmsgs), 0)
+
+        body =  """prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+        prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#>
+
+        select ?library ?library_id ?name ?species
+        where {
+           ?library a libns:Library .
+           OPTIONAL { ?library libns:library_id ?library_id . }
+           OPTIONAL { ?library libns:species ?species . }
+           OPTIONAL { ?library libns:name ?name . }
+        }"""
+        bindings = set(['library', 'library_id', 'name', 'species'])
+        query = RDF.SPARQLQuery(body)
+        count = 0
+        for r in query.execute(model):
+            count += 1
+            for name, value in r.items():
+                self.assertTrue(name in bindings)
+                self.assertTrue(value is not None)
+
+        self.assertEqual(count, len(Library.objects.filter(hidden=False)))
+
+        state = validate_xhtml(response.content)
+        if state is not None: self.assertTrue(state)
+
+            
 # The django test runner flushes the database between test suites not cases,
 # so to be more compatible with running via nose we flush the database tables
 # of interest before creating our sample data.
@@ -201,9 +303,10 @@ class TestRDFaLibrary(TestCase):
                               [u'http://localhost/lane/1193'],
                               p=libNS['has_lane'])
 
+        fc_uri = RDF.Uri('http://localhost/flowcell/303TUAAXX/')
         self.check_literal_object(model,
                                   [u"303TUAAXX"],
-                                  s=RDF.Uri('http://localhost/flowcell/303TUAAXX/'))
+                                  s=fc_uri, p=libNS['flowcell_id'])
 
     def check_literal_object(self, model, values, s=None, p=None, o=None):
         statements = list(model.find_statements(
@@ -228,4 +331,3 @@ def get_rdf_memory_model():
     storage = RDF.MemoryStorage()
     model = RDF.Model(storage)
     return model
-