From 0b3187889d75ec1b777366ea4906ba64b4c6de1e Mon Sep 17 00:00:00 2001 From: mike cullerton Date: Thu, 24 Feb 2022 10:25:34 -0500 Subject: [PATCH] We now raise an error if the spreadsheet does not exist. Added test for case where the spreadsheet does not exist --- crc/scripts/modify_spreadsheet.py | 29 +++++++------ .../modify_spreadsheet.bpmn | 26 +++--------- tests/scripts/test_modify_spreadsheet.py | 42 ++++++++++++++----- 3 files changed, 54 insertions(+), 43 deletions(-) diff --git a/crc/scripts/modify_spreadsheet.py b/crc/scripts/modify_spreadsheet.py index fd58b734..d7458c0d 100644 --- a/crc/scripts/modify_spreadsheet.py +++ b/crc/scripts/modify_spreadsheet.py @@ -35,10 +35,10 @@ class ModifySpreadsheet(Script): def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs): parameters = self.get_parameters(args, kwargs) if len(parameters) == 3: - return parameters + return True else: raise ApiError(code='missing_parameters', - message='The modify_spreadsheet script requires 4 parameters: upload_workflow_id, irb_doc_code, cell, and text') + message='The modify_spreadsheet script requires 3 parameters: irb_doc_code, cell, and text') def do_task(self, task, study_id, workflow_id, *args, **kwargs): parameters = self.get_parameters(args, kwargs) @@ -48,16 +48,19 @@ class ModifySpreadsheet(Script): filter(FileModel.workflow_id == workflow_id). \ filter(FileModel.irb_doc_code == parameters['irb_doc_code']).\ first() - spreadsheet_data = session.query(FileDataModel).\ - filter(FileDataModel.file_model_id==spreadsheet.id).\ - first() - workbook = load_workbook(BytesIO(spreadsheet_data.data)) - sheet = workbook.active - sheet[parameters['cell']] = parameters['text'] - data_string = save_virtual_workbook(workbook) - spreadsheet_data.data = data_string - session.commit() - return parameters + if spreadsheet: + spreadsheet_data = session.query(FileDataModel).\ + filter(FileDataModel.file_model_id==spreadsheet.id).\ + first() + workbook = load_workbook(BytesIO(spreadsheet_data.data)) + sheet = workbook.active + sheet[parameters['cell']] = parameters['text'] + data_string = save_virtual_workbook(workbook) + spreadsheet_data.data = data_string + session.commit() + else: + raise ApiError(code='missing_spreadsheet', + message=f"The spreadshhet you want to modify does not exist. Workflow ID is {workflow_id}, and IRB Doc Code is {parameters['irb_doc_code']}") else: raise ApiError(code='missing_parameters', - message='The modify_spreadsheet script requires 4 parameters: upload_workflow_id, irb_doc_code, cell, and text') + message='The modify_spreadsheet script requires 3 parameters: irb_doc_code, cell, and text') diff --git a/tests/data/modify_spreadsheet/modify_spreadsheet.bpmn b/tests/data/modify_spreadsheet/modify_spreadsheet.bpmn index bff8cbd2..cb3cbba8 100644 --- a/tests/data/modify_spreadsheet/modify_spreadsheet.bpmn +++ b/tests/data/modify_spreadsheet/modify_spreadsheet.bpmn @@ -7,11 +7,10 @@ - + - Flow_1tbcg16 + Flow_10ulj3l - @@ -28,14 +27,8 @@ Flow_0fpc32g Flow_10ulj3l - result = modify_spreadsheet('Finance_BCA', cell_indicator, input_text) + modify_spreadsheet('Finance_BCA', cell_indicator, input_text) - - ## Modify Results -{{ results }} - Flow_10ulj3l - Flow_1tbcg16 - ## File Upload {{ Finance_BCA }} @@ -59,13 +52,9 @@ - - - - - + @@ -88,15 +77,12 @@ - - + + - - - diff --git a/tests/scripts/test_modify_spreadsheet.py b/tests/scripts/test_modify_spreadsheet.py index 327b58ec..81237c4b 100644 --- a/tests/scripts/test_modify_spreadsheet.py +++ b/tests/scripts/test_modify_spreadsheet.py @@ -12,6 +12,19 @@ import os class TestModifySpreadsheet(BaseTest): + def upload_spreadsheet(self, workflow, task, irb_doc_code): + filepath = os.path.join(app.root_path, '..', 'tests', 'data', + 'modify_spreadsheet', 'test_spreadsheet.xlsx') + with open(filepath, 'br') as f_open: + ss_data = f_open.read() + + file_model = UserFileService.add_workflow_file(workflow_id=workflow.id, + task_spec_name=task.name, + name="test_spreadsheet.xlsx", content_type="text", + binary_data=ss_data, irb_doc_code=irb_doc_code) + workflow_api = self.complete_form(workflow, task, {irb_doc_code: {'id': file_model.id}}) + return workflow_api + @staticmethod def get_sheet(workflow_id, irb_doc_code): spreadsheet = session.query(FileModel). \ @@ -34,16 +47,7 @@ class TestModifySpreadsheet(BaseTest): workflow_api = self.get_workflow_api(workflow) task = workflow_api.next_task - filepath = os.path.join(app.root_path, '..', 'tests', 'data', - 'modify_spreadsheet', 'test_spreadsheet.xlsx') - with open(filepath, 'br') as f_open: - ss_data = f_open.read() - - file_model = UserFileService.add_workflow_file(workflow_id=workflow.id, - task_spec_name=task.name, - name="test_spreadsheet.xlsx", content_type="text", - binary_data=ss_data, irb_doc_code=irb_doc_code) - workflow_api = self.complete_form(workflow, task, {irb_doc_code: {'id': file_model.id}}) + workflow_api = self.upload_spreadsheet(workflow, task, irb_doc_code) sheet = self.get_sheet(workflow.id, irb_doc_code) self.assertEqual(None, sheet[cell_indicator].value) @@ -54,3 +58,21 @@ class TestModifySpreadsheet(BaseTest): sheet = self.get_sheet(workflow.id, irb_doc_code) self.assertEqual(input_text, sheet[cell_indicator].value) + + def test_missing_spreadsheet(self): + """The modify_spreadsheet has Finance_BCA hard-coded as the spreadsheet to modify. + In this test we upload a spreadsheet with a different doc code, + and assert that we raise an error when the Finance_BCA spreadsheet does not exist""" + irb_doc_code = 'Finance_GPRF' + cell_indicator = 'C4' + input_text = 'This is my input text.' + + workflow = self.create_workflow('modify_spreadsheet') + workflow_api = self.get_workflow_api(workflow) + task = workflow_api.next_task + + workflow_api = self.upload_spreadsheet(workflow, task, irb_doc_code) + task = workflow_api.next_task + with self.assertRaises(AssertionError): + self.complete_form(workflow, task, {'cell_indicator': cell_indicator, + 'input_text': input_text})