Use flowcell model information to estimate how long it will take to run the flowcell.
[htsworkflow.git] / htsworkflow / frontend / experiments / test_experiments.py
index 631fb1b6ab8ca1fde67246f97f6a3c2836346970..4a665e1222e5ab1e625cbee44b3e2f5f88985d97 100644 (file)
@@ -4,6 +4,7 @@ try:
     import json
 except ImportError, e:
     import simplejson as json
+from datetime import timedelta
 import os
 import shutil
 import sys
@@ -14,6 +15,9 @@ from django.conf import settings
 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
@@ -25,8 +29,11 @@ LANE_SET = range(1,9)
 
 NSMAP = {'libns':'http://jumpgate.caltech.edu/wiki/LibraryOntology#'}
 
+from django.db import connection
+
 class ClusterStationTestCases(TestCase):
-    fixtures = ['test_flowcells.json']
+    fixtures = ['initial_data.json',
+                'test_flowcells.json']
 
     def test_default(self):
         c = models.ClusterStation.default()
@@ -74,7 +81,8 @@ class ClusterStationTestCases(TestCase):
 
 
 class SequencerTestCases(TestCase):
-    fixtures = ['test_flowcells.json']
+    fixtures = ['initial_data.json',
+                'test_flowcells.json']
 
     def test_default(self):
         # starting with no default
@@ -126,7 +134,8 @@ class SequencerTestCases(TestCase):
 
 
 class ExperimentsTestCases(TestCase):
-    fixtures = ['test_flowcells.json',
+    fixtures = ['initial_data.json',
+                'test_flowcells.json',
                 ]
 
     def setUp(self):
@@ -283,8 +292,8 @@ class ExperimentsTestCases(TestCase):
         tree = fromstring(response.content)
         flowcell_spans = tree.xpath('//span[@property="libns:flowcell_id"]',
                                     namespaces=NSMAP)
-        self.assertEqual(flowcell_spans[0].text, '30012AAXX (failed)')
-        failed_fc_span = flowcell_spans[0]
+        self.assertEqual(flowcell_spans[1].text, '30012AAXX (failed)')
+        failed_fc_span = flowcell_spans[1]
         failed_fc_a = failed_fc_span.getparent()
         # make sure some of our RDF made it.
         self.assertEqual(failed_fc_a.get('typeof'), 'libns:IlluminaFlowcell')
@@ -473,16 +482,38 @@ class ExperimentsTestCases(TestCase):
             self.assertTrue(library_id in expected[lane_id])
         self.assertEqual(count, 10)
 
+    def test_flowcell_estimates(self):
+        classic_flowcell = models.FlowCell.objects.get(pk=153)
+        classic_mid = experiments.estimateFlowcellDuration(classic_flowcell)
+
+        self.assertEqual(classic_mid, timedelta(4, 44000))
+
+        rapid_flowcell = models.FlowCell.objects.get(pk=300)
+        rapid_mid = experiments.estimateFlowcellDuration(rapid_flowcell)
+        self.assertEqual(rapid_mid, timedelta(seconds=60800))
+
+    def test_round_to_days(self):
+        data = [
+            [timedelta(2, 12345), (timedelta(days=2), timedelta(days=3))],
+            [timedelta(0, 345), (timedelta(days=0), timedelta(days=1))],
+        ]
+
+        for estimate, expected in data:
+            rounded = experiments.roundToDays(estimate)
+            self.assertEqual(rounded, expected)
 
 class TestFileType(TestCase):
+    fixtures = ['initial_data.json',
+                'test_flowcells.json',
+                ]
+
     def test_file_type_unicode(self):
         file_type_objects = models.FileType.objects
         name = 'QSEQ tarfile'
         file_type_object = file_type_objects.get(name=name)
-        self.assertEqual(u"<FileType: QSEQ tarfile>",
+        self.assertEqual(u"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',
@@ -534,7 +565,8 @@ class TestFileType(TestCase):
             self.assertEqual(result.get('end', None), end)
 
 class TestEmailNotify(TestCase):
-    fixtures = ['test_flowcells.json']
+    fixtures = ['initial_data.json',
+                'test_flowcells.json']
 
     def test_started_email_not_logged_in(self):
         response = self.client.get('/experiments/started/153/')
@@ -584,7 +616,8 @@ def multi_lane_to_dict(lane):
     return dict( ((x['library_id'],x) for x in lane) )
 
 class TestSequencer(TestCase):
-    fixtures = ['test_flowcells.json',
+    fixtures = ['initial_data.json',
+                'test_flowcells.json',
                 ]
 
     def test_name_generation(self):
@@ -672,3 +705,19 @@ class TestSequencer(TestCase):
 
         errmsgs = list(inference.run_validation())
         self.assertEqual(len(errmsgs), 0)
+
+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")