Use unittest2's module hooks for setting up the django environment.
authorDiane Trout <diane@caltech.edu>
Thu, 29 Nov 2012 00:45:33 +0000 (16:45 -0800)
committerDiane Trout <diane@caltech.edu>
Thu, 29 Nov 2012 01:20:43 +0000 (17:20 -0800)
It'll set up and destroy the test environment as well as
configure the email handler per module test.

I only added the code to the modules that were using
django.test.TestCase

htsworkflow/frontend/experiments/test_experiments.py
htsworkflow/frontend/inventory/test_inventory.py
htsworkflow/frontend/samples/test_samples.py
htsworkflow/submission/test/test_condorfastq.py

index 8eb998343b7409350554a169c5a9cbc1748b96f6..d4c556a9f8c0ef1d0d864a66e4629c8c3dfa17bd 100644 (file)
@@ -15,6 +15,8 @@ from django.core import mail
 from django.core.exceptions import ObjectDoesNotExist
 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.experiments import models
 from htsworkflow.frontend.experiments import experiments
 from htsworkflow.frontend.auth import apidata
@@ -26,6 +28,18 @@ LANE_SET = range(1,9)
 
 NSMAP = {'libns':'http://jumpgate.caltech.edu/wiki/LibraryOntology#'}
 
+from django.db import connection
+OLD_DB_NAME = settings.DATABASE_NAME
+VERBOSITY = 0
+def setUpModule():
+    setup_test_environment()
+    settings.DEBUG = False
+    connection.creation.create_test_db(VERBOSITY)
+
+def tearDownModule():
+    connection.creation.destroy_test_db(OLD_DB_NAME, VERBOSITY)
+    teardown_test_environment()
+
 class ClusterStationTestCases(TestCase):
     fixtures = ['test_flowcells.json']
 
@@ -483,7 +497,6 @@ class TestFileType(TestCase):
         self.assertEqual(u"<FileType: QSEQ tarfile>",
                              unicode(file_type_object))
 
-class TestFileType(TestCase):
     def test_find_file_type(self):
         file_type_objects = models.FileType.objects
         cases = [('woldlab_090921_HWUSI-EAS627_0009_42FC3AAXX_l7_r1.tar.bz2',
@@ -537,17 +550,6 @@ class TestFileType(TestCase):
 class TestEmailNotify(TestCase):
     fixtures = ['test_flowcells.json']
 
-    @classmethod
-    def setUpClass(self):
-        # isolate django mail when running under unittest2
-        setup_test_environment()
-
-    @classmethod
-    def tearDownClass(self):
-        # isolate django mail when running under unittest2
-        teardown_test_environment()
-
-
     def test_started_email_not_logged_in(self):
         response = self.client.get('/experiments/started/153/')
         self.assertEqual(response.status_code, 302)
@@ -684,3 +686,29 @@ class TestSequencer(TestCase):
 
         errmsgs = list(inference.run_validation())
         self.assertEqual(len(errmsgs), 0)
+
+
+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 unittest2 import TestSuite, defaultTestLoader
+    suite = TestSuite()
+    for testcase in [ClusterStationTestCases,
+                     SequencerTestCases,
+                     ExerimentsTestCases,
+                     TestFileType,
+                     TestEmailNotify,
+                     TestSequencer]:
+        suite.addTests(defaultTestLoader.loadTestsFromTestCase(testcase))
+    return suite
+
+if __name__ == "__main__":
+    from unittest2 import main
+    main(defaultTest="suite")
index 118c654aaf8463ee638d748414c5c620df9c0d67..86d37b7cbe7e740468f04df9380ae8fa82c98b35 100644 (file)
@@ -1,6 +1,11 @@
 import RDF
 
 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 django.contrib.auth.models import User
 from django.core import urlresolvers
 
@@ -108,6 +113,15 @@ class InventoryTestCase(TestCase):
         flowcells = [ str(x.uri) for x in targets]
         return flowcells
 
+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 unittest2 import TestSuite, defaultTestLoader
     suite = TestSuite()
index 2fb394511989b32e6cb97d7a507cf28fce71fc2d..f0844e55cce7fbf3b33fbdfbf03b9d9264b3e13a 100644 (file)
@@ -6,6 +6,10 @@ except ImportError, 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, \
@@ -327,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 unittest2 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 unittest2 import main
+    main(defaultTest="suite")
index ffb9f88620356ba0e61800db60fa4715463515e2..09d68083c1ec50052c450a71e9d9e24356573cf7 100644 (file)
@@ -7,6 +7,10 @@ import shutil
 import tempfile
 
 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.submission.condorfastq import CondorFastqExtract
 from htsworkflow.submission.results import ResultMap
@@ -676,6 +680,16 @@ class TestCondorFastq(TestCase):
             self.assertTrue('12345_C02F9ACXX_c202_l3_r2.fastq' in arguments[3])
 
 
+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 unittest2 import TestSuite, defaultTestLoader
     suite = TestSuite()