From 4b086aaab751149c20ad355cc4645d717bdd5458 Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Wed, 28 Nov 2012 16:45:33 -0800 Subject: [PATCH] Use unittest2's module hooks for setting up the django environment. 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 --- .../frontend/experiments/test_experiments.py | 52 ++++++++++++++----- .../frontend/inventory/test_inventory.py | 14 +++++ htsworkflow/frontend/samples/test_samples.py | 25 +++++++++ .../submission/test/test_condorfastq.py | 14 +++++ 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/htsworkflow/frontend/experiments/test_experiments.py b/htsworkflow/frontend/experiments/test_experiments.py index 8eb9983..d4c556a 100644 --- a/htsworkflow/frontend/experiments/test_experiments.py +++ b/htsworkflow/frontend/experiments/test_experiments.py @@ -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"", 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") diff --git a/htsworkflow/frontend/inventory/test_inventory.py b/htsworkflow/frontend/inventory/test_inventory.py index 118c654..86d37b7 100644 --- a/htsworkflow/frontend/inventory/test_inventory.py +++ b/htsworkflow/frontend/inventory/test_inventory.py @@ -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() diff --git a/htsworkflow/frontend/samples/test_samples.py b/htsworkflow/frontend/samples/test_samples.py index 2fb3945..f0844e5 100644 --- a/htsworkflow/frontend/samples/test_samples.py +++ b/htsworkflow/frontend/samples/test_samples.py @@ -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") diff --git a/htsworkflow/submission/test/test_condorfastq.py b/htsworkflow/submission/test/test_condorfastq.py index ffb9f88..09d6808 100644 --- a/htsworkflow/submission/test/test_condorfastq.py +++ b/htsworkflow/submission/test/test_condorfastq.py @@ -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() -- 2.30.2