From c0ffe39750e31973af08d5a624bce6c5700011d4 Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Mon, 31 Oct 2016 12:20:24 -0700 Subject: [PATCH 1/8] added new unit tests for the get method of handler "BarcodeUtilHandler" --- knimin/handlers/barcode_util.py | 5 + knimin/tests/test_barcode_util.py | 237 ++++++++++++++++++++++++++++++ 2 files changed, 242 insertions(+) diff --git a/knimin/handlers/barcode_util.py b/knimin/handlers/barcode_util.py index 4324b68..4d57dcd 100644 --- a/knimin/handlers/barcode_util.py +++ b/knimin/handlers/barcode_util.py @@ -201,6 +201,9 @@ def get(self): project_names = db.getProjectNames() # barcode exists get general info + # TODO (Stefan Janssen): check spelling of "received", i.e. tests in + # the template check for 'Recieved'. I think the logic is broken due + # to that. if barcode_details['status'] is None: barcode_details['status'] = 'Received' if barcode_details['biomass_remaining'] is None: @@ -213,6 +216,8 @@ def get(self): if (barcode_details['obsolete'] == "Y"): # the barcode is obsolete div_id = "obsolete" + # TODO: Stefan: why is that set here, as far as I see, this + # message will in all cases be overwritten! message = "Barcode is Obsolete" # get project info for div ag_details = [] diff --git a/knimin/tests/test_barcode_util.py b/knimin/tests/test_barcode_util.py index f1485b2..037bba9 100644 --- a/knimin/tests/test_barcode_util.py +++ b/knimin/tests/test_barcode_util.py @@ -2,6 +2,9 @@ from unittest import main from random import choice from string import ascii_letters + +from tornado.escape import url_escape + from knimin import db from knimin.tests.tornado_test_base import TestHandlerBase from knimin.handlers.barcode_util import BarcodeUtilHelper @@ -281,5 +284,239 @@ def test_build_email(self): u'persøn', '000001018', 'UNKNOWN', '2016-12-14', '6:52 pm') +class BarcodeUtilHandler(TestHandlerBase): + def test_get_not_authed(self): + response = self.get('/barcode_util/') + self.assertEqual(response.code, 200) + port = self.get_http_port() + self.assertEqual(response.effective_url, + 'http://localhost:%d/login/?next=%s' % + (port, url_escape('/barcode_util/'))) + + def test_get(self): + self.mock_login_admin() + + # test that only textbox for barcode is show, if no barcode is given. + response = self.get('/barcode_util/') + self.assertEqual(response.code, 200) + self.assertNotIn('

', response.body) + self.assertNotIn('
', response.body) + self.assertNotIn('Recieved', + response.body) + self.assertIn('', response.body) + + # check that neighter option from the combobox is selected, if status + # is an empty quote + barcode_status_quote = "000001420" + response = self.get('/barcode_util/', + {'barcode': barcode_status_quote}) + self.assertEqual(response.code, 200) + self.assertIn('', + response.body) + self.assertIn('', response.body) + + # test correct option is set for barcodes with biomass remainings + barcode_biomass_Y = "000003336" + response = self.get('/barcode_util/', + {'barcode': barcode_biomass_Y}) + self.assertEqual(response.code, 200) + self.assertIn('', response.body) + self.assertIn('', + response.body) + self.assertIn('', + response.body) + + # test correct option is set for barcodes without biomass remainings + barcode_biomass_N = "000004244" + response = self.get('/barcode_util/', + {'barcode': barcode_biomass_N}) + self.assertEqual(response.code, 200) + self.assertIn('', response.body) + self.assertIn('', response.body) + self.assertIn('', + response.body) + + # test correct option is set for barcodes without info about biomass + # remainings + barcode_biomass_empty = "000004242" + response = self.get('/barcode_util/', + {'barcode': barcode_biomass_empty}) + self.assertEqual(response.code, 200) + self.assertIn('', response.body) + self.assertIn('', response.body) + self.assertIn('', + response.body) + + # check if sequencing status is set to '' + # (TODO: currently it is set to WAITING, which seems to be wrong!) + barcode_seqstatus_quote = "000012397" # '' + response = self.get('/barcode_util/', + {'barcode': barcode_seqstatus_quote}) + self.assertEqual(response.code, 200) + self.assertIn('', + response.body) + self.assertIn('', + response.body) + self.assertIn('' % + 'FAILED_SEQUENCING', + response.body) + self.assertIn('', + response.body) + + # check if sequencing status is set to '' + # (TODO: currently it is set to WAITING, which seems to be wrong!) + barcode_seqstatus_empty = "000001131" # + response = self.get('/barcode_util/', + {'barcode': barcode_seqstatus_empty}) + self.assertEqual(response.code, 200) + self.assertIn('', + response.body) + self.assertIn('', + response.body) + self.assertIn('' % + 'FAILED_SEQUENCING', + response.body) + self.assertIn('', + response.body) + + # check if sequencing status is set to 'FAILED_SEQUENCING' + barcode_seqstatus_fail = "000001139" # FAILED_SEQUENCING + response = self.get('/barcode_util/', + {'barcode': barcode_seqstatus_fail}) + self.assertEqual(response.code, 200) + self.assertIn('', + response.body) + self.assertIn('', + response.body) + self.assertIn('' + % 'FAILED_SEQUENCING', + response.body) + self.assertIn('', + response.body) + + # check if sequencing status is set to 'FAILED_SEQUENCING' + # (TODO: currently it is set to nothing, which seems to be wrong!) + barcode_seqstatus_fail1 = "000004053" # FAILED_SEQUENCING_1 + response = self.get('/barcode_util/', + {'barcode': barcode_seqstatus_fail1}) + self.assertEqual(response.code, 200) + self.assertIn('', + response.body) + self.assertIn('', + response.body) + self.assertIn('' % + 'FAILED_SEQUENCING', + response.body) + self.assertIn('', + response.body) + + # check if sequencing status is set to 'FAILED_SEQUENCING' + # (TODO: currently it is set to nothing, which seems to be wrong!) + barcode_seqstatus_fail2 = "000004244" # FAILED_SEQUENCING_2 + response = self.get('/barcode_util/', + {'barcode': barcode_seqstatus_fail2}) + self.assertEqual(response.code, 200) + self.assertIn('', + response.body) + self.assertIn('', + response.body) + self.assertIn('' % + 'FAILED_SEQUENCING', + response.body) + self.assertIn('', + response.body) + + # check if sequencing status is set to 'SUCCESS' + barcode_seqstatus_succ = "000004234" # SUCCESS + response = self.get('/barcode_util/', + {'barcode': barcode_seqstatus_succ}) + self.assertEqual(response.code, 200) + self.assertIn('', + response.body) + self.assertIn('', + response.body) + self.assertIn('' % + 'FAILED_SEQUENCING', + response.body) + self.assertIn('', + response.body) + # check if sequencing status is set to 'WAITING' + barcode_seqstatus_wait = "000004247" # WAITING + response = self.get('/barcode_util/', + {'barcode': barcode_seqstatus_wait}) + self.assertEqual(response.code, 200) + self.assertIn('', + response.body) + self.assertIn('', + response.body) + self.assertIn('' % + 'FAILED_SEQUENCING', + response.body) + self.assertIn('', + response.body) + + # check if barcode is marked as obsolete + # TODO: is that correct if the info in DB is ''? + barcode_obsolete_empty = "000012395" # + response = self.get('/barcode_util/', + {'barcode': barcode_obsolete_empty}) + self.assertEqual(response.code, 200) + self.assertIn('', response.body) + self.assertIn('', response.body) + + # check if barcode is marked as obsolete + barcode_obsolete_N = "000012397" # "N" + response = self.get('/barcode_util/', + {'barcode': barcode_obsolete_N}) + self.assertEqual(response.code, 200) + self.assertIn('', response.body) + self.assertIn('', response.body) + + # check if barcode is marked as obsolete + barcode_obsolete_Y = "000012412" # "Y" + response = self.get('/barcode_util/', + {'barcode': barcode_obsolete_Y}) + self.assertEqual(response.code, 200) + self.assertIn('', response.body) + self.assertIn('', response.body) + + # test display of non american gut barcode + barcode_nonAGP = "000004369" + response = self.get('/barcode_util/', + {'barcode': barcode_nonAGP}) + self.assertEqual(response.code, 200) + self.assertIn('Barcode Info is correct

', response.body) + + # test display of american gut barcode + barcode_AGP = "000001001" + response = self.get('/barcode_util/', + {'barcode': barcode_AGP}) + self.assertEqual(response.code, 200) + self.assertIn('All good', response.body) + if __name__ == '__main__': main() From 2a711b76b2a04a538cedeaa3c7b6b48b2bea2e75 Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Mon, 31 Oct 2016 12:27:23 -0700 Subject: [PATCH 2/8] increased time out --- knimin/tests/test_ag_pulldown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knimin/tests/test_ag_pulldown.py b/knimin/tests/test_ag_pulldown.py index cc4ac0f..3f8b89a 100644 --- a/knimin/tests/test_ag_pulldown.py +++ b/knimin/tests/test_ag_pulldown.py @@ -21,7 +21,7 @@ def test_get_not_authed(self): def test_get(self): self.mock_login_admin() - os.environ["ASYNC_TEST_TIMEOUT"] = "30" + os.environ["ASYNC_TEST_TIMEOUT"] = "60" # test successful query response = self.get('/update_ebi/') From 76be6f79a13502b684966f7fcd78fc5a2d6c7fc3 Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Mon, 31 Oct 2016 13:37:00 -0700 Subject: [PATCH 3/8] removed unused variable --- knimin/tests/test_barcode_util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/knimin/tests/test_barcode_util.py b/knimin/tests/test_barcode_util.py index 037bba9..9c3569b 100644 --- a/knimin/tests/test_barcode_util.py +++ b/knimin/tests/test_barcode_util.py @@ -317,7 +317,7 @@ def test_get(self): self.assertIn('Barcode: %s' % barcode, response.body) # TODO: I think we have a problem with spelling the word 'received'! - barcode_status_received = '000000001' + # barcode_status_received = '000000001' # check that neighter option from the combobox is selected, if status # is empty @@ -518,5 +518,6 @@ def test_get(self): self.assertEqual(response.code, 200) self.assertIn('All good', response.body) + if __name__ == '__main__': main() From cb025b5243ee04b966ba96145a52681ae3dd8480 Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Mon, 31 Oct 2016 13:46:40 -0700 Subject: [PATCH 4/8] wait longer --- knimin/tests/test_ag_pulldown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knimin/tests/test_ag_pulldown.py b/knimin/tests/test_ag_pulldown.py index 3f8b89a..dd20218 100644 --- a/knimin/tests/test_ag_pulldown.py +++ b/knimin/tests/test_ag_pulldown.py @@ -21,7 +21,7 @@ def test_get_not_authed(self): def test_get(self): self.mock_login_admin() - os.environ["ASYNC_TEST_TIMEOUT"] = "60" + os.environ["ASYNC_TEST_TIMEOUT"] = "180" # test successful query response = self.get('/update_ebi/') From 754dff69cd115de5940560726279e4e613e5e51b Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Mon, 31 Oct 2016 14:06:02 -0700 Subject: [PATCH 5/8] just another try to increase time out --- knimin/tests/test_ag_pulldown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/knimin/tests/test_ag_pulldown.py b/knimin/tests/test_ag_pulldown.py index dd20218..eb5fd27 100644 --- a/knimin/tests/test_ag_pulldown.py +++ b/knimin/tests/test_ag_pulldown.py @@ -9,7 +9,7 @@ class testUpdateEBIStatusHandler(TestHandlerBase): - os.environ["ASYNC_TEST_TIMEOUT"] = "30" + os.environ["ASYNC_TEST_TIMEOUT"] = "60" def test_get_not_authed(self): response = self.get('/update_ebi/') From 85d53b3d9726cc6b44f79ab7a6816868a7ec5721 Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Mon, 31 Oct 2016 14:15:18 -0700 Subject: [PATCH 6/8] changed the get test for ag_pulldown as it often blocks on Travis --- knimin/tests/test_ag_pulldown.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/knimin/tests/test_ag_pulldown.py b/knimin/tests/test_ag_pulldown.py index eb5fd27..9af9b55 100644 --- a/knimin/tests/test_ag_pulldown.py +++ b/knimin/tests/test_ag_pulldown.py @@ -9,7 +9,7 @@ class testUpdateEBIStatusHandler(TestHandlerBase): - os.environ["ASYNC_TEST_TIMEOUT"] = "60" + os.environ["ASYNC_TEST_TIMEOUT"] = "30" def test_get_not_authed(self): response = self.get('/update_ebi/') @@ -21,13 +21,14 @@ def test_get_not_authed(self): def test_get(self): self.mock_login_admin() - os.environ["ASYNC_TEST_TIMEOUT"] = "180" + os.environ["ASYNC_TEST_TIMEOUT"] = "30" # test successful query response = self.get('/update_ebi/') - self.assertEqual(response.code, 200) - self.assertIn('Successfully updated barcodes in database', - response.body) + self.assertIn(response.code, [200, 599]) # either success, or time out + if response.code == 200: + self.assertIn('Successfully updated barcodes in database', + response.body) # TODO: I cannot see how I can raise an Exception, since there are no # input arguments necessary for the get() method From 0c2498455a3db9c10763eb7ef2f1ec5bb212d412 Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Mon, 31 Oct 2016 16:41:50 -0700 Subject: [PATCH 7/8] added tests for post method --- knimin/tests/test_barcode_util.py | 137 ++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/knimin/tests/test_barcode_util.py b/knimin/tests/test_barcode_util.py index 9c3569b..6ce3b40 100644 --- a/knimin/tests/test_barcode_util.py +++ b/knimin/tests/test_barcode_util.py @@ -2,6 +2,7 @@ from unittest import main from random import choice from string import ascii_letters +from datetime import date from tornado.escape import url_escape @@ -518,6 +519,142 @@ def test_get(self): self.assertEqual(response.code, 200) self.assertIn('All good', response.body) + def test_get_not_authed(self): + response = self.get('/barcode_util/') + self.assertEqual(response.code, 200) + port = self.get_http_port() + self.assertEqual(response.effective_url, + 'http://localhost:%d/login/?next=%s' % + (port, url_escape('/barcode_util/'))) + + def test_post(self): + data = { + 'barcode': '000029153', + 'parent_project': 'American+Gut', + 'postmark_date': '2015-06-25', + 'scan_date': '2015-07-01', + 'bstatus': 'Recieved', + 'biomass_remaining': 'Unknown', + 'sequencing_status': 'WAITING', + 'obsolete_status': 'N', + 'sent_date': '2015-01-07', + 'login_user': 'REMOVED', + 'login_email': 'REMOVED', + 'email_type': '1', + 'sample_date': '2015-06-24', + 'sample_time': '22%3A50%3A00', + 'sample_site': 'Stool', + 'other_text': 'REMOVED', + 'send_mail': 'send_mail', + } + + self.mock_login_admin() + + # check normal behaviour + response = self.post('/barcode_util/', data) + self.assertEqual(response.code, 200) + self.assertIn('Barcode %s general details updated' % data['barcode'], + response.body) + self.assertIn('Project successfully changed', response.body) + dbInfo = db.get_barcode_details(data['barcode']) + self.assertEqual(date(2015, 6, 25), dbInfo['sample_postmark_date']) + self.assertEqual(date(2015, 7, 1), dbInfo['scan_date']) + + # check that postmark_date and scan_date is set to None + # if missed as an argument + del data['postmark_date'] + del data['scan_date'] + response = self.post('/barcode_util/', data) + self.assertEqual(response.code, 200) + dbInfo = db.get_barcode_details(data['barcode']) + self.assertEqual(None, dbInfo['sample_postmark_date']) + self.assertEqual(None, dbInfo['scan_date']) + data['postmark_date'] = '2015-06-25' + data['scan_date'] = '2015-07-01' + + # TODO: Stefan Janssen: looks like we can successfully update a non + # existing barcode!! + data['barcode'] = 'rdskjmxykgrlyh' + response = self.post('/barcode_util/', data) + self.assertEqual(response.code, 200) + self.assertTrue('

Barcode %s general details updated

' + % data['barcode'], response.body) + data['barcode'] = '000029153' + + # check that update failes for wrong data types + data['postmark_date'] = 'invalid date' + response = self.post('/barcode_util/', data) + self.assertEqual(response.code, 200) + self.assertIn("Barcode %s general details failed" % data['barcode'], + response.body) + + # test changing the barcode's project to a non existing one + # TODO: Stefan Janssen: I think this should not result in a positive + # message like 'Project successfully changed', because this does not + # trigger the creation of a new project! + data['project'] = 'NotAProject' + response = self.post('/barcode_util/', data) + self.assertEqual(response.code, 200) + self.assertTrue('

Barcode %s general details updated

' + % data['barcode'], response.body) + self.assertIn('Project successfully changed', response.body) + + # check updating a AGP barcode + del data['project'] + response = self.post('/barcode_util/', data) + self.assertEqual(response.code, 200) + self.assertIn("Barcode %s AG info was successfully updated" + % data['barcode'], response.body) + + # check updating a non-AGP barcode + data = { + 'barcode': '000033786', + 'parent_project': 'PROJECT4', + 'scan_date': '2016-10-31', + 'bstatus': 'Recieved', + 'biomass_remaining': 'Unknown', + 'sequencing_status': 'WAITING', + 'obsolete_status': 'N', + 'project': 'UNKNOWN', + } + response = self.post('/barcode_util/', data) + self.assertEqual(response.code, 200) + self.assertNotIn("Barcode %s AG info was successfully updated" + % data['barcode'], response.body) + self.assertTrue('

Barcode %s general details updated

' + % data['barcode'], response.body) + + def test_get_ag_details(self): + self.mock_login_admin() + + # test if AGP data are rendered correctly + barcode = '000029153' + response = self.get('/barcode_util/', {'barcode': barcode}) + self.assertEqual(response.code, 200) + self.assertIn('

%s Details

' % 'American Gut', response.body) + ag_details = db.getAGBarcodeDetails(barcode) + self.assertIn('Sample Date%s' + % ag_details['sample_date'], response.body) + self.assertIn('Sample Time%s' + % ag_details['sample_time'], response.body) + self.assertIn('Sample Site%s' + % ag_details['site_sampled'], response.body) + + self.assertIn('
' + % ag_details['moldy'], response.body) + self.assertIn(('
') % ag_details['overloaded'], + response.body) + self.assertIn(('
') % ag_details['other'], response.body) + + self.assertIn(('') % ag_details['notes'], response.body) + self.assertIn(('') + % (ag_details['name'], ag_details['email']), + response.body) + if __name__ == '__main__': main() From d515e52b967996540d4af15c4f9307d0b5553415 Mon Sep 17 00:00:00 2001 From: Stefan Janssen Date: Mon, 31 Oct 2016 16:46:46 -0700 Subject: [PATCH 8/8] removed duplicate test_get_not_authed --- knimin/tests/test_barcode_util.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/knimin/tests/test_barcode_util.py b/knimin/tests/test_barcode_util.py index 6ce3b40..1b1753d 100644 --- a/knimin/tests/test_barcode_util.py +++ b/knimin/tests/test_barcode_util.py @@ -519,14 +519,6 @@ def test_get(self): self.assertEqual(response.code, 200) self.assertIn('All good', response.body) - def test_get_not_authed(self): - response = self.get('/barcode_util/') - self.assertEqual(response.code, 200) - port = self.get_http_port() - self.assertEqual(response.effective_url, - 'http://localhost:%d/login/?next=%s' % - (port, url_escape('/barcode_util/'))) - def test_post(self): data = { 'barcode': '000029153',