Initial port to python3
[htsworkflow.git] / htsworkflow / frontend / samples / test_samples.py
index 4e0c2a3cb87736033c117c650167a6254d5558be..12a4b5ba9e692366e585932ba7f7d93f8c118db7 100644 (file)
@@ -1,12 +1,15 @@
 import datetime
-import unittest
 
 try:
     import json
-except ImportError, e:
+except ImportError as e:
     import simplejson as json
 
 from django.test import TestCase
+from django.test.utils import setup_test_environment, \
+     teardown_test_environment
+from django.db import connection
+from django.conf import settings
 
 from htsworkflow.frontend.samples.models import \
         Affiliation, \
@@ -90,28 +93,28 @@ class SampleWebTestCase(TestCase):
                     self.failUnlessEqual(d['stopping_point'], lib.stopping_point)
                     self.failUnlessEqual(d['successful_pM'], lib.successful_pM)
                     self.failUnlessEqual(d['undiluted_concentration'],
-                                         unicode(lib.undiluted_concentration))
+                                         str(lib.undiluted_concentration))
                 # 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'lane_id': 1193,
-                                u'flowcell': u'303TUAAXX',
-                                u'status_code': None}
+                    lane_set = {'status': 'Unknown',
+                                'paired_end': True,
+                                'read_length': 75,
+                                'lane_number': 1,
+                                'lane_id': 1193,
+                                'flowcell': '303TUAAXX',
+                                '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'lane_id': 1197,
-                                u'flowcell': u'303TUAAXX',
-                                u'status_code': 2}
+                    lane_set = {'status': 'Good',
+                                'paired_end': True,
+                                'read_length': 75,
+                                'lane_number': 5,
+                                'lane_id': 1197,
+                                'flowcell': '303TUAAXX',
+                                'status_code': 2}
                     self.failUnlessEqual(len(d['lane_set']), 1)
                     self.failUnlessEqual(d['lane_set'][0], lane_set)
 
@@ -166,11 +169,11 @@ class SampleWebTestCase(TestCase):
         }"""
         query = RDF.SPARQLQuery(body)
         for r in query.execute(model):
-            self.assertEqual(fromTypedNode(r['library_id']), u'10981')
+            self.assertEqual(fromTypedNode(r['library_id']), '10981')
             self.assertEqual(fromTypedNode(r['name']),
-                             u'Paired End Multiplexed Sp-BAC')
+                             'Paired End Multiplexed Sp-BAC')
             self.assertEqual(fromTypedNode(r['gel_cut']), 400)
-            self.assertEqual(fromTypedNode(r['made_by']), u'Igor')
+            self.assertEqual(fromTypedNode(r['made_by']), 'Igor')
 
         state = validate_xhtml(content)
         if state is not None:
@@ -181,9 +184,7 @@ class SampleWebTestCase(TestCase):
         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 ]
+        errmsgs = list(inference.run_validation())
         self.assertEqual(len(errmsgs), 0)
 
     def test_library_index_rdfa(self):
@@ -200,27 +201,26 @@ class SampleWebTestCase(TestCase):
         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 ]
+        errmsgs = list(inference.run_validation())
         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_name
+        select ?library ?library_id ?name ?species ?species_name
         where {
            ?library a libns:Library .
            OPTIONAL { ?library libns:library_id ?library_id . }
-           OPTIONAL { ?library libns:species_name ?species_name . }
+           OPTIONAL { ?library libns:species ?species .
+                      ?species libns:species_name ?species_name . }
            OPTIONAL { ?library libns:name ?name . }
         }"""
-        bindings = set(['library', 'library_id', 'name', 'species_name'])
+        bindings = set(['library', 'library_id', 'name', 'species', 'species_name'])
         query = RDF.SPARQLQuery(body)
         count = 0
         for r in query.execute(model):
             count += 1
-            for name, value in r.items():
+            for name, value in list(r.items()):
                 self.assertTrue(name in bindings)
                 self.assertTrue(value is not None)
 
@@ -229,7 +229,7 @@ class SampleWebTestCase(TestCase):
         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.
@@ -274,7 +274,7 @@ try:
     rdfNS = RDF.NS("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
     xsdNS = RDF.NS("http://www.w3.org/2001/XMLSchema#")
     libNS = RDF.NS("http://jumpgate.caltech.edu/wiki/LibraryOntology#")
-except ImportError,e:
+except ImportError as e:
     HAVE_RDF = False
 
 
@@ -300,12 +300,12 @@ class TestRDFaLibrary(TestCase):
         self.check_literal_object(model, ['Drosophila melanogaster'], p=libNS['species_name'])
 
         self.check_uri_object(model,
-                              [u'http://localhost/lane/1193'],
+                              ['http://localhost/lane/1193'],
                               p=libNS['has_lane'])
 
         fc_uri = RDF.Uri('http://localhost/flowcell/303TUAAXX/')
         self.check_literal_object(model,
-                                  [u"303TUAAXX"],
+                                  ["303TUAAXX"],
                                   s=fc_uri, p=libNS['flowcell_id'])
 
     def check_literal_object(self, model, values, s=None, p=None, o=None):
@@ -323,7 +323,7 @@ class TestRDFaLibrary(TestCase):
         self.failUnlessEqual(len(statements), len(values),
                         "Couln't find %s %s %s" % (s,p,o))
         for s in statements:
-            self.failUnless(unicode(s.object.uri) in values)
+            self.failUnless(str(s.object.uri) in values)
 
 
 
@@ -331,3 +331,24 @@ def get_rdf_memory_model():
     storage = RDF.MemoryStorage()
     model = RDF.Model(storage)
     return model
+
+OLD_DB = settings.DATABASES['default']['NAME']
+def setUpModule():
+    setup_test_environment()
+    connection.creation.create_test_db()
+
+def tearDownModule():
+    connection.creation.destroy_test_db(OLD_DB)
+    teardown_test_environment()
+
+def suite():
+    from unittest import TestSuite, defaultTestLoader
+    suite = TestSuite()
+    suite.addTests(defaultTestLoader.loadTestsFromTestCase(LibraryTestCase))
+    suite.addTests(defaultTestLoader.loadTestsFromTestCase(SampleWebTestCase))
+    suite.addTests(defaultTestLoader.loadTestsFromTestCase(TestRDFaLibrary))
+    return suite
+
+if __name__ == "__main__":
+    from unittest import main
+    main(defaultTest="suite")