def test_send_postevaluation_email_exception(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() weekyear = os.path.join(temp_dir, '2017', 'dataset.week.12') os.makedirs(weekyear, mode=0o755) task = PostEvaluationTask(weekyear, params) task.create_dir() csvfile = os.path.join(task.get_dir(), 'foo.csv') open(csvfile, 'a').close() smtpemailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock(side_effect=IOError('ha')) mockserver.quit = Mock() smtpemailer.set_alternate_smtp_server(mockserver) pee = PostEvaluationEmailer(['*****@*****.**'], smtpemailer) task.set_evaluation_emailer(pee) f = open(task.get_summary_txt(), 'w') f.write('hello') f.flush() f.close() pee.send_postevaluation_email(task) mockserver.quit.assert_any_call() self.assertEqual( pee.get_message_log(), '\nCaught exception trying to email :' ' [email protected] : Caught exception ha\n') self.assertEqual(mockserver.sendmail.call_count, 1) finally: shutil.rmtree(temp_dir)
def test_generate_post_evaluation_email_body(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() params.version = '1.2.3' weekyear = os.path.join(temp_dir, '2017', 'dataset.week.12') os.makedirs(weekyear, mode=0o755) task = PostEvaluationTask(weekyear, params) task.create_dir() f = open(task.get_summary_txt(), 'w') f.write('hello') f.flush() f.close() pee = PostEvaluationEmailer(None, None) subject, msg = pee._generate_post_evaluation_email_body(task) self.assertEqual( subject, D3RTask.SUBJECT_LINE_PREFIX + '2017 week 12 post evaluation summary report') self.assertEqual( msg, 'Dear CELPP Admin,\n\nHere is the post ' 'evaluation summary reports for CELPP ' 'week 12\n\n\n\nhello\n\n\nSincerely,\n\n' 'CELPP Automation 1.2.3') finally: shutil.rmtree(temp_dir)
def test_send_postevaluation_email_success(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() weekyear = os.path.join(temp_dir, '2017', 'dataset.week.12') os.makedirs(weekyear, mode=0o755) task = PostEvaluationTask(weekyear, params) task.create_dir() smtpemailer = SmtpEmailer() mockserver = D3RParameters() mockserver.sendmail = Mock() mockserver.quit = Mock() smtpemailer.set_alternate_smtp_server(mockserver) pee = PostEvaluationEmailer(['*****@*****.**'], smtpemailer) task.set_evaluation_emailer(pee) f = open(task.get_summary_txt(), 'w') f.write('hello') f.flush() f.close() pee.send_postevaluation_email(task) mockserver.quit.assert_any_call() self.assertEqual(pee.get_message_log(), '\nSent post evaluation email to: [email protected]\n') self.assertEqual(mockserver.sendmail.call_count, 1) finally: shutil.rmtree(temp_dir)
def test_get_postevaluation_summary(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() task = PostEvaluationTask(temp_dir, params) task.create_dir() # no file res = task.get_postevaluation_summary() self.assertEqual( res, '\n\nNo ' + task.get_summary_txt() + ' file found\n') # empty file s_file = os.path.join(task.get_dir(), PostEvaluationTask.SUMMARY_TXT) open(s_file, 'a').close() res = task.get_postevaluation_summary() self.assertEqual(res, '\n\n\n') os.chmod(task.get_summary_txt(), 0) res = task.get_postevaluation_summary() self.assertTrue('\n\nUnable to generate postevaluation' in res) os.chmod(task.get_summary_txt(), stat.S_IRWXU) # some data in the file f = open(s_file, 'w') f.write('hello\nhi\n') f.flush() f.close() res = task.get_postevaluation_summary() self.assertEqual(res, '\n\nhello\nhi\n\n') finally: shutil.rmtree(temp_dir)
def test_get_all_evaluation_tasks(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() # no evaluation tasks task = PostEvaluationTask(temp_dir, params) res = task.get_all_evaluation_tasks() self.assertEqual(len(res), 0) # directory in path that does NOT match suffix task.create_dir() os.makedirs(os.path.join(temp_dir, 'stage.x.1'), mode=0o755) res = task.get_all_evaluation_tasks() self.assertEqual(len(res), 0) # one task, but is file (weird case) weirdtaskfile = os.path.join( temp_dir, 'stage.7.hi.' + EvaluationTaskFactory.SCORING_SUFFIX) open(weirdtaskfile, 'a').close() res = task.get_all_evaluation_tasks() self.assertEqual(len(res), 0) self.assertEqual( task.get_email_log(), 'Just a note, found a task with valid name, but ' 'it is not a directory ' + weirdtaskfile) # one task etask = EvaluationTask(temp_dir, 'foo.evaluation', None, params) etask.create_dir() res = task.get_all_evaluation_tasks() self.assertEqual(len(res), 1) self.assertTrue(res[0].get_dir_name(), etask.get_dir_name()) # three tasks two with non complete status etask2 = EvaluationTask(temp_dir, 'bla_dock.extsubmission.evaluation', None, params) etask2.create_dir() open(os.path.join(etask2.get_dir(), D3RTask.ERROR_FILE), 'a').close() etask3 = EvaluationTask(temp_dir, '12_x.extsubmission.evaluation', None, params) etask3.create_dir() open(os.path.join(etask3.get_dir(), D3RTask.COMPLETE_FILE), 'a').close() res = task.get_all_evaluation_tasks() self.assertEqual(len(res), 3) d_name_list = [] for entry in res: d_name_list.append(entry.get_dir_name()) self.assertTrue(etask.get_dir_name() in d_name_list) self.assertTrue(etask2.get_dir_name() in d_name_list) self.assertTrue(etask3.get_dir_name() in d_name_list) finally: shutil.rmtree(temp_dir)
def test_get_uploadable_files(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() task = PostEvaluationTask(temp_dir, params) # try with no dir self.assertEqual(task.get_uploadable_files(), []) # try with empty dir task.create_dir() self.assertEqual(task.get_uploadable_files(), []) # try with final log final_log = os.path.join(task.get_dir(), EvaluationTask.FINAL_LOG) open(final_log, 'a').close() flist = task.get_uploadable_files() self.assertEqual(len(flist), 1) self.assertTrue(final_log in flist) # try with csv file csvfile = os.path.join( task.get_dir(), 'Overall_RMSD_foo' + PostEvaluationTask.CSV_SUFFIX) open(csvfile, 'a').close() flist = task.get_uploadable_files() self.assertEqual(len(flist), 2) self.assertTrue(csvfile in flist) self.assertTrue(final_log in flist) # try with summary.txt file open(task.get_summary_txt(), 'a').close() flist = task.get_uploadable_files() self.assertEqual(len(flist), 3) self.assertTrue(task.get_summary_txt() in flist) self.assertTrue(final_log in flist) # try with 2 csv file csvfile2 = os.path.join( task.get_dir(), 'Overall_RMSD_foo2' + PostEvaluationTask.CSV_SUFFIX) open(csvfile2, 'a').close() flist = task.get_uploadable_files() self.assertEqual(len(flist), 4) self.assertTrue(csvfile in flist) self.assertTrue(csvfile2 in flist) self.assertTrue(task.get_summary_txt() in flist) self.assertTrue(final_log in flist) finally: shutil.rmtree(temp_dir)
def test_run_fails_cause_can_run_is_false(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() task = PostEvaluationTask(temp_dir, params) task.create_dir() task.run() self.assertEqual( task.get_error(), task.get_dir_name() + ' already exists and status is ' + D3RTask.UNKNOWN_STATUS) finally: shutil.rmtree(temp_dir)
def test_get_all_csv_files_in_dir(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() task = PostEvaluationTask(temp_dir, params) task.create_dir() # test empty dir res = task.get_all_csv_files_in_dir() self.assertEqual(len(res), 0) # add directory with csv ending csvdir = os.path.join(task.get_dir(), 'hi ' + PostEvaluationTask.CSV_SUFFIX) os.makedirs(csvdir, mode=0o755) res = task.get_all_csv_files_in_dir() self.assertEqual(len(res), 0) # 1 csv onecsv = os.path.join( task.get_dir(), 'Overall_RMSD_hi' + PostEvaluationTask.CSV_SUFFIX) open(onecsv, 'a').close() res = task.get_all_csv_files_in_dir() self.assertEqual(len(res), 1) self.assertTrue(onecsv in res) # 3 csv twocsv = os.path.join( task.get_dir(), 'Overall_RMSD_bye' + PostEvaluationTask.CSV_SUFFIX) open(twocsv, 'a').close() threecsv = os.path.join(task.get_dir(), 'some' + PostEvaluationTask.CSV_SUFFIX) open(threecsv, 'a').close() res = task.get_all_csv_files_in_dir() self.assertEqual(len(res), 3) self.assertTrue(onecsv in res) self.assertTrue(twocsv in res) self.assertTrue(threecsv in res) finally: shutil.rmtree(temp_dir)
def test_can_run(self): temp_dir = tempfile.mkdtemp() try: params = D3RParameters() # post evaluation task exists already task = PostEvaluationTask(temp_dir, params) task.create_dir() self.assertEqual(task.can_run(), False) self.assertEqual( task.get_error(), task.get_dir_name() + ' already exists and status is unknown') # post evaluation task already complete task = PostEvaluationTask(temp_dir, params) open(os.path.join(task.get_dir(), D3RTask.COMPLETE_FILE), 'a').close() self.assertEqual(task.can_run(), False) self.assertEqual(task.get_error(), None) finally: shutil.rmtree(temp_dir)