def test_workflow_complex_run(self): """Tests running workflow with several data objects""" from invenio.bibworkflow_model import BibWorkflowObject from invenio.bibworkflow_api import start self.test_data = [{'data': 1}, {'data': "wwww"}, {'data': 20}] final_data = [{'data': 19}, {'data': "wwww"}, {'data': 38}] workflow = start(workflow_name="test_workflow_2", data=self.test_data, module_name="unit_tests") # Keep id for cleanup after self.id_workflows.append(workflow.uuid) # Get parent objects of the workflow we just ran objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None) # noqa E711 # Let's check that we found anything. # There should only be three objects self.assertEqual(objects.count(), 3) all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid) self.assertEqual(all_objects.count(), 6) for obj in objects.all(): # The child object should have the final or halted version self.assertTrue(obj.child_objects[0].version in (CFG_OBJECT_VERSION.FINAL, CFG_OBJECT_VERSION.HALTED)) # Making sure the final data is correct self.assertTrue(obj.child_objects[0].get_data() in final_data)
def test_workflow_basic_run(self): """Tests running workflow with one data object""" from invenio.bibworkflow_model import BibWorkflowObject from invenio.bibworkflow_api import start self.test_data = {'data': 20} initial_data = self.test_data final_data = {'data': 41} workflow = start(workflow_name="test_workflow", data=[self.test_data], module_name="unit_tests") # Keep id for cleanup after self.id_workflows.append(workflow.uuid) # Get parent object of the workflow we just ran initial_object = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None) # noqa E711 all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid) # There should only be 2 objects (initial, final) self.assertEqual(all_objects.count(), 2) self._check_workflow_execution(initial_object, initial_data, final_data)
def run(self): """ Runs or resumes the workflow """ current_workflow = self.get_workflow_from_db() if current_workflow and \ current_workflow.status != CFG_WORKFLOW_STATUS.NEW: # Resume workflow if there are object to resume for workflow in resume_objects_in_workflow( id_workflow=self.uuid, start_point="restart_task"): # There should only be one object self.eng = workflow break else: # Start workflow from beginning self.eng = start(workflow_name=self.deposition_type, data=[self.obj], uuid=self.get_uuid()) self.bib_obj = self.eng.getObjects().next()[1]
def test_redis_for_halted(self): from invenio.bibworkflow_model import BibWorkflowObject from invenio.bibworkflow_api import start from invenio.bibworkflow_utils import set_up_redis initial_data = {'data': 1} workflow = start(workflow_name="test_workflow", data=[initial_data], module_name="unit_tests") obj = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent != None).one() rs = set_up_redis() entry1 = rs.smembers("holdingpen_sort:publisher:Desy") entry2 = rs.smembers("holdingpen_sort:category:lower_than_20") self.assertTrue(str(obj.id) in entry1) self.assertTrue(str(obj.id) in entry2)
def test_restart_workflow(self): """Tests restarting workflow for given workflow id""" from invenio.bibworkflow_model import BibWorkflowObject from invenio.bibworkflow_api import (start, start_by_wid) initial_data = {'data': 1} # testing restarting from previous task init_workflow = start(workflow_name="test_workflow", data=[initial_data], module_name="unit_tests") init_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == init_workflow.uuid) restarted_workflow = start_by_wid(wid=init_workflow.uuid, module_name="unit_tests") restarted_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == restarted_workflow.uuid) self.assertEqual(restarted_objects.count(), 1) self.assertEqual(restarted_objects[0].version, init_objects[1].version) self.assertEqual(restarted_objects[0].id_parent, init_objects[0].id) self.assertEqual(restarted_objects[0].get_data(), init_objects[1].get_data())
def test_workflow_recordxml(self): """Tests runnning a record ingestion workflow""" from invenio.bibworkflow_model import BibWorkflowObject from invenio.bibworkflow_api import start initial_data = {'data': self.recxml, 'type': "text/xml"} workflow = start(workflow_name="marcxml_workflow", data=[initial_data], module_name="unit_tests") # Keep id for cleanup after self.id_workflows.append(workflow.uuid) # Get parent object of the workflow we just ran objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None) # noqa E711 all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid) self.assertEqual(all_objects.count(), 2) print map(lambda x: x, objects) self._check_workflow_execution(objects, initial_data, None)
def test_simplified_data(self): """Tests running workflow with simplified data.""" from invenio.bibworkflow_model import BibWorkflowObject from invenio.bibworkflow_api import start self.test_data = 20 initial_data = self.test_data final_data = 41 workflow = start(workflow_name="simplified_data_test_workflow", data=[self.test_data], module_name="unit_tests") # Keep id for cleanup after self.id_workflows.append(workflow.uuid) # Get parent object of the workflow we just ran # NOTE: ignore PEP8 here for None objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None) # noqa E711 all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid) self.assertEqual(all_objects.count(), 2) self._check_workflow_execution(objects, initial_data, final_data)
def test_continue_execution_for_object(self): """Tests continuing execution of workflow for object given object from prev, current and next task""" from invenio.bibworkflow_model import BibWorkflowObject from invenio.bibworkflow_api import (start, continue_oid) initial_data = {'data': 1} final_data_prev = {'data': 3} final_data_curr = {'data': 2} final_data_next = {'data': 9} # testing restarting from previous task init_workflow = start("test_workflow", data=[initial_data], module_name="unit_tests") obj_halted = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == init_workflow.uuid, BibWorkflowObject.version == CFG_OBJECT_VERSION.HALTED).first() workflow = continue_oid(oid=obj_halted.id, start_point="restart_prev", module_name="unit_tests") new_object = BibWorkflowObject.query.filter( BibWorkflowObject.id == obj_halted.id) self.assertEqual(new_object.count(), 1) self.assertEqual(new_object[0].get_data(), final_data_prev) all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid) self.assertEqual(all_objects.count(), 2) # testing restarting from current task init_workflow2 = start(workflow_name="test_workflow", data=[initial_data], module_name="unit_tests") obj_halted2 = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == init_workflow2.uuid, BibWorkflowObject.version == CFG_OBJECT_VERSION.HALTED).first() workflow2 = continue_oid(oid=obj_halted.id, start_point="restart_task") object2 = BibWorkflowObject.query.filter( BibWorkflowObject.id == obj_halted2.id) self.assertEqual(object2.count(), 1) self.assertEqual(object2[0].get_data(), final_data_curr) all_objects2 = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow2.uuid) self.assertEqual(all_objects2.count(), 2) # testing continuing from next task init_workflow3 = start(workflow_name="test_workflow", data=[initial_data], module_name="unit_tests") obj_halted3 = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == init_workflow3.uuid, BibWorkflowObject.version == CFG_OBJECT_VERSION.HALTED).first() workflow3 = continue_oid(oid=obj_halted3.id, start_point="continue_next", module_name="unit_tests") object3 = BibWorkflowObject.query.filter( BibWorkflowObject.id == obj_halted3.id) self.assertEqual(object3.count(), 1) self.assertEqual(object3[0].get_data(), final_data_next) all_objects3 = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow3.uuid) self.assertEqual(all_objects3.count(), 2)