def test_workflow_for_finished_object(self): """Test starting workflow with finished object given.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start from invenio_workflows.engine import WorkflowStatus current = BibWorkflowObject() current.set_data(20) current.save() workflow = start(workflow_name="demo_workflow", data=[current], module_name="unit_tests") self.assertEqual(WorkflowStatus.COMPLETED, workflow.status) self.assertEqual(ObjectVersion.COMPLETED, current.version) self.assertEqual(38, current.get_data()) previous = BibWorkflowObject.query.get(current.id) workflow_2 = start(workflow_name="demo_workflow", data=[previous], module_name="unit_tests") self.assertEqual(WorkflowStatus.COMPLETED, workflow_2.status) self.assertEqual(ObjectVersion.COMPLETED, previous.version) self.assertEqual(56, previous.get_data())
def test_acces_to_undefineworkflow(self): """Test of access to undefined workflow.""" from invenio_workflows.api import start try: start("@thisisnotatrueworkflow@", ["my_false_data"], random_kay_args="value") except Exception as e: from invenio_workflows.errors import \ WorkflowDefinitionError self.assertTrue(isinstance(e, WorkflowDefinitionError))
def test_workflows_exceptions(self): """Test for workflows exception.""" from invenio_workflows.errors import WorkflowError from invenio_workflows.api import start try: start("demo_workflow_error", [2], module_name="unit_tests") except Exception as e: self.assertTrue(isinstance(e, WorkflowError)) self.assertTrue("ZeroDivisionError" in e.message) self.assertTrue("call_a()" in e.message) self.assertTrue("call_b()" in e.message) self.assertTrue("call_c()" in e.message)
def test_object_creation_complete(self): """ Test status of object before/after workflow. When created before calling API, with "high" test-data that will make the workflow complete. """ from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.engine import WorkflowStatus from invenio_workflows.api import start test_object = BibWorkflowObject() test_object.set_data(20) test_object.save() self.assertEqual(ObjectVersion.INITIAL, test_object.version) self.assertEqual(None, test_object.id_parent) self.assertEqual(20, test_object.get_data()) engine = start('demo_workflow', [test_object], module_name="unit_tests") self.assertEqual(38, test_object.get_data()) self.assertEqual(None, test_object.id_parent) self.assertEqual(WorkflowStatus.COMPLETED, engine.status) self.assertEqual(ObjectVersion.COMPLETED, test_object.version)
def test_halt_in_branch(self): """Test halt task when in conditionnal branch.""" from workflow.patterns import IF_ELSE from invenio_workflows.registry import workflows from invenio_workflows.api import start from invenio_workflows.engine import WorkflowStatus from invenio_workflows.models import (BibWorkflowObjectLog, ObjectVersion) def always_true(obj, eng): return True def halt_engine(obj, eng): return eng.halt("Test") class BranchTest(object): workflow = [IF_ELSE(always_true, [halt_engine], [halt_engine])] workflows['branchtest'] = BranchTest data = [set(('somekey', 'somevalue'))] eng = start('branchtest', data, module_name="unit_tests") idx, obj = list(eng.getObjects())[0] self.assertEqual(ObjectVersion.WAITING, obj.version) self.assertEqual(WorkflowStatus.HALTED, eng.status) self.assertEqual( 0, BibWorkflowObjectLog.get(id_object=obj.id, log_type=logging.ERROR).count())
def test_halt(self): """Test halt task.""" from invenio_workflows.registry import workflows from invenio_workflows.api import start from invenio_workflows.engine import WorkflowStatus from invenio_workflows.models import (BibWorkflowObjectLog, ObjectVersion) def halt_engine(obj, eng): return eng.halt("Test") class HaltTest(object): workflow = [halt_engine] workflows['halttest'] = HaltTest data = [set(('somekey', 'somevalue'))] eng = start('halttest', data, module_name="unit_tests") idx, obj = list(eng.getObjects())[0] self.assertEqual(ObjectVersion.WAITING, obj.version) self.assertEqual(WorkflowStatus.HALTED, eng.status) self.assertEqual( 0, BibWorkflowObjectLog.get(id_object=obj.id, log_type=logging.ERROR).count())
def test_restart_workflow(self): """Test restarting workflow for given workflow id.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start, start_by_wid initial_data = 1 init_workflow = start(workflow_name="demo_workflow", data=[initial_data], module_name="unit_tests") init_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == init_workflow.uuid).order_by( BibWorkflowObject.id).all() self.assertEqual(2, len(init_objects)) restarted_workflow = start_by_wid(wid=init_workflow.uuid, module_name="unit_tests") # We expect the same workflow to be re-started self.assertTrue(init_workflow.uuid == restarted_workflow.uuid) restarted_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == restarted_workflow.uuid).order_by( BibWorkflowObject.id).all() # This time we should only have one more initial object self.assertEqual(2, len(restarted_objects)) # Last object will be INITIAL self.assertEqual(ObjectVersion.INITIAL, restarted_objects[1].version) self.assertEqual(restarted_objects[1].id_parent, restarted_objects[0].id)
def test_object_creation_halt(self): """Test status of object before/after workflow. When created before calling API, with "low" test-data that will make the workflow halt. """ from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start from invenio_workflows.engine import WorkflowStatus test_object = BibWorkflowObject() test_object.set_data(2) test_object.save() self.assertEqual(ObjectVersion.INITIAL, test_object.version) self.assertEqual(None, test_object.id_parent) self.assertEqual(2, test_object.get_data()) engine = start('demo_workflow', [test_object], module_name="unit_tests") self.assertEqual(2, test_object.get_data()) self.assertEqual(ObjectVersion.WAITING, test_object.version) self.assertEqual(WorkflowStatus.HALTED, engine.status)
def test_workflow_object_creation_simple(self): """Test to see if the right snapshots or object versions are created.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start initial_data = 22 final_data = 40 workflow = start(workflow_name="demo_workflow", data=[initial_data], module_name="unit_tests") # Get parent object of the workflow we just ran initial_object = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None).first() # noqa E711 test_object = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == initial_object.id).first() all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid).order_by( BibWorkflowObject.id).all() # There should only be 2 objects (initial, final) self.assertEqual(2, len(all_objects)) self.assertEqual(test_object.id_parent, initial_object.id) self.assertEqual(ObjectVersion.COMPLETED, initial_object.version) self.assertEqual(final_data, initial_object.get_data()) self.assertEqual(initial_data, test_object.get_data()) self.assertEqual(ObjectVersion.INITIAL, test_object.version)
def test_workflow_marcxml(self): """Test runnning a record ingestion workflow with a action step.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.engine import WorkflowStatus from invenio_workflows.api import start initial_data = self.recxml workflow = start(workflow_name="marcxml_workflow", data=[initial_data], module_name="unit_tests") # Get objects of the workflow we just ran objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None # noqa E711 ).order_by(BibWorkflowObject.id).all() self._check_workflow_execution(objects, initial_data) all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid ).order_by(BibWorkflowObject.id).all() self.assertEqual(2, len(all_objects)) self.assertEqual(WorkflowStatus.HALTED, workflow.status) current = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.version == ObjectVersion.HALTED ).one() self.assertEqual(current.get_action(), "approval")
def test_workflow_object_creation(self): """Test to see if the right snapshots or object versions are created.""" from flask import current_app from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start current_app.config['WORKFLOWS_SNAPSHOTS_ENABLED'] = True initial_data = 22 final_data = 40 test_object = BibWorkflowObject() test_object.set_data(initial_data) test_object.save() workflow = start(workflow_name="demo_workflow", data=[test_object], module_name="unit_tests") # Get parent object of the workflow we just ran initial_object = BibWorkflowObject.query.filter( BibWorkflowObject.id_parent == test_object.id).one() all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid ).order_by(BibWorkflowObject.id).all() # There should only be 2 objects (initial, final) self.assertEqual(2, len(all_objects)) self.assertEqual(test_object.id, initial_object.id_parent) self.assertEqual(ObjectVersion.INITIAL, initial_object.version) self.assertEqual(initial_data, initial_object.get_data()) self.assertEqual(final_data, test_object.get_data()) self.assertEqual(ObjectVersion.COMPLETED, test_object.version)
def test_workflow_object_creation_simple(self): """Test to see if the right snapshots or object versions are created.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start initial_data = 22 final_data = 40 workflow = start(workflow_name="demo_workflow", data=[initial_data], module_name="unit_tests") # Get parent object of the workflow we just ran initial_object = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None).first() # noqa E711 test_object = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == initial_object.id).first() all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid ).order_by(BibWorkflowObject.id).all() # There should only be 2 objects (initial, final) self.assertEqual(2, len(all_objects)) self.assertEqual(test_object.id_parent, initial_object.id) self.assertEqual(ObjectVersion.COMPLETED, initial_object.version) self.assertEqual(final_data, initial_object.get_data()) self.assertEqual(initial_data, test_object.get_data()) self.assertEqual(ObjectVersion.INITIAL, test_object.version)
def test_restart_workflow(self): """Test restarting workflow for given workflow id.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start, start_by_wid initial_data = 1 init_workflow = start(workflow_name="demo_workflow", data=[initial_data], module_name="unit_tests") init_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == init_workflow.uuid ).order_by(BibWorkflowObject.id).all() self.assertEqual(2, len(init_objects)) restarted_workflow = start_by_wid(wid=init_workflow.uuid, module_name="unit_tests") # We expect the same workflow to be re-started self.assertTrue(init_workflow.uuid == restarted_workflow.uuid) restarted_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == restarted_workflow.uuid ).order_by(BibWorkflowObject.id).all() # This time we should only have one more initial object self.assertEqual(2, len(restarted_objects)) # Last object will be INITIAL self.assertEqual(ObjectVersion.INITIAL, restarted_objects[1].version) self.assertEqual(restarted_objects[1].id_parent, restarted_objects[0].id)
def test_workflow_marcxml(self): """Test runnning a record ingestion workflow with a action step.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.engine import WorkflowStatus from invenio_workflows.api import start initial_data = self.recxml workflow = start(workflow_name="marcxml_workflow", data=[initial_data], module_name="unit_tests") # Get objects of the workflow we just ran objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None # noqa E711 ).order_by(BibWorkflowObject.id).all() self._check_workflow_execution(objects, initial_data) all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid).order_by( BibWorkflowObject.id).all() self.assertEqual(2, len(all_objects)) self.assertEqual(WorkflowStatus.HALTED, workflow.status) current = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.version == ObjectVersion.HALTED).one() self.assertEqual(current.get_action(), "approval")
def test_logic_tasks_continue(self): """Test that the logic tasks work correctly when continuing.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import (start, continue_oid) from invenio_workflows.engine import WorkflowStatus test_object = BibWorkflowObject() test_object.set_data(0) test_object.save() workflow = start('demo_workflow_logic', [test_object], module_name="unit_tests") self.assertEqual(5, test_object.get_data()) self.assertEqual("lt9", test_object.get_extra_data()["test"]) workflow = continue_oid(test_object.id) self.assertEqual(6, test_object.get_data()) self.assertEqual("lt9", test_object.get_extra_data()["test"]) workflow = continue_oid(test_object.id) self.assertEqual(9, test_object.get_data()) self.assertEqual("gte9", test_object.get_extra_data()["test"]) workflow = continue_oid(test_object.id) self.assertEqual(15, test_object.get_data()) self.assertEqual("gte9", test_object.get_extra_data()["test"]) workflow = continue_oid(test_object.id) self.assertEqual(ObjectVersion.COMPLETED, test_object.version) self.assertEqual(WorkflowStatus.COMPLETED, workflow.status)
def test_halt_in_branch(self): """Test halt task when in conditionnal branch.""" from workflow.patterns import IF_ELSE from invenio_workflows.registry import workflows from invenio_workflows.api import start from invenio_workflows.engine import WorkflowStatus from invenio_workflows.models import (BibWorkflowObjectLog, ObjectVersion) def always_true(obj, eng): return True def halt_engine(obj, eng): return eng.halt("Test") class BranchTest(object): workflow = [ IF_ELSE(always_true, [halt_engine], [halt_engine]) ] workflows['branchtest'] = BranchTest data = [set(('somekey', 'somevalue'))] eng = start('branchtest', data, module_name="unit_tests") idx, obj = list(eng.getObjects())[0] self.assertEqual(ObjectVersion.WAITING, obj.version) self.assertEqual(WorkflowStatus.HALTED, eng.status) self.assertEqual(0, BibWorkflowObjectLog.get( id_object=obj.id, log_type=logging.ERROR).count())
def test_payload_model_creation(self): """A workflow can specify a model to encapsulate behaviour.""" from invenio_workflows.api import start workflow = start("payload_model_fixture", data=[self.some_record], module_name="unit_tests") modified_object = workflow.completed_objects[0] p = workflow.workflow_definition.model(modified_object) sip = p.get_latest_sip() self.assertTrue(sip.metadata)
def test_harvesting_workflow_with_match(self, search): """Test an harvesting workflow when the record already exists in Holding Pen.""" from invenio_workflows.api import start # Mock Elasticsearch search for Holding Pen check search.return_value = (["1"], 1) # Mock Elasticsearch DELETE hook responses.add( responses.DELETE, re.compile(".*holdingpen-.*/record/(\d+)"), status=404 ) # Mock check in record database responses.add( responses.GET, re.compile(".*hep/record/_search"), status=200, body="""{ "hits": { "total": 0, "max_score": null, "hits": [] } }""", content_type="application/json" ) # Mock check in Holding Pen responses.add( responses.GET, re.compile(".*holdingpen-.*/record/_search"), status=200, body="""{ "hits": { "total": 0, "max_score": null, "hits": [{ "_index": "hep_v1", "_type": "record", "_id": "1", "_score": 1, "_source": {} }] } }""", content_type="application/json" ) workflow = start('harvesting_fixture', data=[self.record_oai_arxiv_plots], module_name='unit_tests') # XXX(jacquerie): find a better check self.assertEqual(workflow.objects, [])
def test_payload_sip_creation(self): """A Payload has a sip.""" from invenio_workflows.api import start from inspire.modules.workflows.models import Payload workflow = start("payload_fixture", data=[self.some_record], module_name="unit_tests") modified_object = workflow.completed_objects[0] p = Payload(modified_object) sip = p.get_latest_sip() self.assertTrue(sip.metadata)
def test_harvesting_workflow_accepted(self, search): """Test a full harvesting workflow.""" from invenio_base.globals import cfg from invenio_workflows.api import start from inspire.utils.helpers import get_record_from_obj # Mock Elasticsearch search for Holding Pen check search.return_value = [] responses.add(responses.GET, cfg["WORKFLOWS_MATCH_REMOTE_SERVER_URL"], body="[]", status=200) responses.add( responses.GET, "http://arxiv.org/e-print/1511.01097", content_type="application/x-eprint-tar", body=self.arxiv_tarball_accept.read(), status=200, adding_headers={"Content-Encoding": "x-gzip"}, ) responses.add( responses.GET, "http://arxiv.org/pdf/1511.01097", content_type="application/pdf", body=self.arxiv_pdf_accept.read(), status=200, stream=True, ) robotupload_url = os.path.join( cfg.get("CFG_ROBOTUPLOAD_SUBMISSION_BASEURL"), "batchuploader/robotupload/insert" ) responses.add( responses.POST, robotupload_url, body="[INFO] bibupload batchupload --insert /dummy/file/path\n", status=200 ) workflow = start("harvesting_fixture", data=[self.record_oai_arxiv_accept], module_name="unit_tests") # Let's get the halted record obj = workflow.halted_objects[0] # Now let's resolve it as accepted and continue obj.remove_action() obj.extra_data["approved"] = True obj.extra_data["core"] = True obj.set_extra_data(obj.extra_data) obj.save() workflow = obj.continue_workflow() record = get_record_from_obj(obj, workflow) # Now it is CORE self.assertTrue("CORE" in record.get("collections.primary"))
def test_harvesting_workflow_rejected(self, search): """Test a full harvesting workflow.""" from invenio_base.globals import cfg from invenio_workflows.api import start from inspire.utils.helpers import get_record_from_obj # Mock Elasticsearch search for Holding Pen check search.return_value = [] responses.add(responses.GET, cfg["WORKFLOWS_MATCH_REMOTE_SERVER_URL"], body="[]", status=200) responses.add( responses.GET, "http://arxiv.org/e-print/1407.7587", content_type="application/x-eprint-tar", body=self.arxiv_tarball.read(), status=200, adding_headers={"Content-Encoding": "x-gzip"}, ) responses.add( responses.GET, "http://arxiv.org/pdf/1407.7587", content_type="application/pdf", body=self.arxiv_pdf.read(), status=200, stream=True, ) workflow = start("harvesting_fixture", data=[self.record_oai_arxiv_plots], module_name="unit_tests") # Let's get the record metadata and check contents obj = workflow.completed_objects[0] record = get_record_from_obj(obj, workflow) # This record should be rejected self.assertFalse(obj.extra_data["approved"]) # Files should have been attached (tarball + pdf) self.assertTrue(len(obj.data["files"]) == 2) # Some plots/files should have been added to FFTs self.assertTrue(record.get("fft")) # A publication note should have been extracted self.assertTrue(record.get("publication_info")) # A prediction should have been made self.assertTrue(obj.get_tasks_results().get("arxiv_guessing")) # It is not CORE self.assertFalse("CORE" in record.get("collections.primary"))
def test_payload_creation(self): """A Payload can be created.""" from invenio_workflows.api import start from invenio_workflows.engine import WorkflowStatus workflow = start("payload_fixture", data=[self.some_record], module_name="unit_tests") self.assertEqual(WorkflowStatus.COMPLETED, workflow.status) self.assertTrue(len(workflow.completed_objects) == 1) modified_object = workflow.completed_objects[0] for l in ["files", "sips", "type", "drafts", "title"]: self.assertIn(l, modified_object.data)
def test_harvesting_workflow_with_match(self): """Test an harvesting workflow when the record already exists.""" from invenio_base.globals import cfg from invenio_workflows.api import start # Mock Elasticsearch DELETE hook httpretty.register_uri(httpretty.DELETE, re.compile(".*holdingpen/record/(\d+)"), status=404) httpretty.register_uri(httpretty.GET, cfg["WORKFLOWS_MATCH_REMOTE_SERVER_URL"], body="[1212]", status=200) workflow = start("harvesting_fixture", data=[self.record_oai_arxiv_plots], module_name="unit_tests") # XXX(jacquerie): find a better check self.assertEqual(workflow.objects, [])
def test_workflows_tasks(self): """Test delayed workflows in non delayed one.""" from invenio_workflows.models import BibWorkflowObject from invenio_workflows.api import start test_object = BibWorkflowObject() test_object.save() test_object.set_data(22) engine = start("demo_workflow_workflows", [test_object], module_name="unit_tests") from invenio_workflows.engine import WorkflowStatus self.assertEqual(0, engine.get_extra_data()["_nb_workflow_failed"]) self.assertEqual(WorkflowStatus.COMPLETED, engine.status) self.assertEqual(0, test_object.get_tasks_results()["review_workflow"][0]["result"]["failed"]) self.assertEqual(4, test_object.get_extra_data()["nbworkflowrunning"]) self.assertEqual(21, engine.get_extra_data()["_nb_workflow_finish"])
def test_workflow_without_workflow_object_saved(self): """Test that the logic tasks work correctly.""" from invenio_workflows.models import BibWorkflowObject from invenio_workflows.api import start, start_by_wid test_object = BibWorkflowObject() test_object.set_data(0) test_object.save() workflow = start('demo_workflow_logic', [test_object], module_name="unit_tests") self.assertEqual(5, test_object.get_data()) self.assertEqual("lt9", test_object.get_extra_data()["test"]) start_by_wid(workflow.uuid) test_object.delete(test_object.id)
def test_workflow_without_workflow_object_saved(self): """Test that the logic tasks work correctly.""" from invenio_workflows.models import BibWorkflowObject from invenio_workflows.api import start, start_by_wid test_object = BibWorkflowObject() test_object.set_data(0) test_object.save() workflow = start( 'demo_workflow_logic', [test_object], module_name="unit_tests") self.assertEqual(5, test_object.get_data()) self.assertEqual("lt9", test_object.get_extra_data()["test"]) start_by_wid(workflow.uuid) test_object.delete(test_object.id)
def test_workflow_complex_run(self): """Test running workflow with several data objects.""" from invenio_workflows.models import BibWorkflowObject from invenio_workflows.api import start self.test_data = [1, 20] workflow = start(workflow_name="demo_workflow", data=self.test_data, module_name="unit_tests") # Get parent objects of the workflow we just ran objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid ).order_by(BibWorkflowObject.id).all() self.assertEqual(2, len(objects)) self.assertEqual(1, objects[0].get_data()) self.assertEqual(38, objects[1].get_data())
def test_workflow_for_halted_object(self): """Test workflow with continuing a halted object.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start, continue_oid from invenio_workflows.engine import WorkflowStatus current = BibWorkflowObject() current.set_data(self.recxml) current.save() workflow = start(workflow_name="marcxml_workflow", data=[current], module_name="unit_tests") self.assertEqual(WorkflowStatus.HALTED, workflow.status) self.assertEqual(ObjectVersion.HALTED, current.version) workflow = continue_oid(current.id, module_name="unit_tests") self.assertEqual(WorkflowStatus.COMPLETED, workflow.status) self.assertEqual(ObjectVersion.COMPLETED, current.version)
def test_workflow_object_creation_simple(self): """Test to see if the right snapshots or object versions are created.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start initial_data = 22 final_data = 40 workflow = start(workflow_name="demo_workflow", data=[initial_data], module_name="unit_tests") all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid ).order_by(BibWorkflowObject.id).all() self.assertEqual(1, len(all_objects)) self.assertEqual(ObjectVersion.COMPLETED, all_objects[0].version) self.assertEqual(final_data, all_objects[0].get_data())
def test_harvesting_workflow_with_match(self): """Test an harvesting workflow when the record already exists.""" from invenio.base.globals import cfg from invenio_workflows.api import start httpretty.HTTPretty.allow_net_connect = False httpretty.register_uri( httpretty.GET, cfg['WORKFLOWS_MATCH_REMOTE_SERVER_URL'], body='[1212]', status=200 ) workflow = start('harvesting_fixture', data=[self.record_oai_arxiv_plots], module_name='unit_tests') # XXX(jacquerie): find a better check self.assertEqual(workflow.objects, [])
def test_continue_execution_for_object(self): """Test continuing execution of workflow for object given.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start, continue_oid initial_data = 1 # testing restarting from previous task init_workflow = start("demo_workflow", data=[initial_data], module_name="unit_tests") obj_halted = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == init_workflow.uuid, BibWorkflowObject.version == ObjectVersion.WAITING ).first() self.assertTrue(obj_halted) self.assertEqual(1, obj_halted.get_data()) # Try to restart, we should halt again actually. continue_oid(oid=obj_halted.id, start_point="restart_task", module_name="unit_tests") self.assertEqual(1, obj_halted.get_data()) self.assertEqual(ObjectVersion.WAITING, obj_halted.version) # We skip to next part, this should work continue_oid(oid=obj_halted.id, module_name="unit_tests") self.assertEqual(19, obj_halted.get_data()) self.assertEqual(ObjectVersion.COMPLETED, obj_halted.version) # Let's do that last task again, shall we? continue_oid(oid=obj_halted.id, start_point="restart_prev", module_name="unit_tests") self.assertEqual(37, obj_halted.get_data()) self.assertEqual(ObjectVersion.COMPLETED, obj_halted.version)
def test_continue_execution_for_object(self): """Test continuing execution of workflow for object given.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start, continue_oid initial_data = 1 # testing restarting from previous task init_workflow = start("demo_workflow", data=[initial_data], module_name="unit_tests") obj_halted = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == init_workflow.uuid, BibWorkflowObject.version == ObjectVersion.WAITING).first() self.assertTrue(obj_halted) self.assertEqual(1, obj_halted.get_data()) # Try to restart, we should halt again actually. continue_oid(oid=obj_halted.id, start_point="restart_task", module_name="unit_tests") self.assertEqual(1, obj_halted.get_data()) self.assertEqual(ObjectVersion.WAITING, obj_halted.version) # We skip to next part, this should work continue_oid(oid=obj_halted.id, module_name="unit_tests") self.assertEqual(19, obj_halted.get_data()) self.assertEqual(ObjectVersion.COMPLETED, obj_halted.version) # Let's do that last task again, shall we? continue_oid(oid=obj_halted.id, start_point="restart_prev", module_name="unit_tests") self.assertEqual(37, obj_halted.get_data()) self.assertEqual(ObjectVersion.COMPLETED, obj_halted.version)
def test_workflow_approve_step(self): """Test runnning a record ingestion workflow with a action step.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.engine import WorkflowStatus from invenio_workflows.api import start initial_data = 1 workflow = start(workflow_name="demo_workflow_approve", data=[initial_data], module_name="unit_tests") # Get objects of the workflow we just ran objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid ).order_by(BibWorkflowObject.id).all() # Let's check that we found anything. There should only be one object self.assertEqual(len(objects), 1) self.assertEqual(ObjectVersion.HALTED, objects[0].version) self.assertEqual(WorkflowStatus.HALTED, workflow.status) self.assertEqual(objects[0].get_action(), "approval")
def test_logic_tasks_restart(self): """Test that the logic tasks work correctly when restarted.""" from invenio_workflows.models import BibWorkflowObject from invenio_workflows.api import (start, start_by_wid) test_object = BibWorkflowObject() test_object.set_data(0) test_object.save() # Initial run workflow = start('demo_workflow_logic', [test_object], module_name="unit_tests") self.assertEqual(5, test_object.get_data()) self.assertEqual("lt9", test_object.get_extra_data()["test"]) # Reset before re-starting (reset Iterator data) workflow.reset_extra_data() workflow = start_by_wid(workflow.uuid) self.assertEqual(5, test_object.get_data()) self.assertEqual("lt9", test_object.get_extra_data()["test"])
def test_workflow_restarts(self): """Check if all is well when restarting a workflow several times.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start, continue_oid from invenio_workflows.engine import WorkflowStatus test_object = BibWorkflowObject() random.seed(time.time()) tries = 15 test_object.set_data(tries) test_object.save() engine = start('demo_workflow_hardcore', [test_object], module_name="unit_tests") for i in range(0, tries): self.assertEqual(engine.status, WorkflowStatus.HALTED) for my_object_b in engine.getObjects(): engine = continue_oid(my_object_b[1].id, "restart_task") self.assertEqual(0, test_object.get_data()) self.assertEqual(ObjectVersion.COMPLETED, test_object.version) self.assertEqual(WorkflowStatus.COMPLETED, engine.status)
def test_workflow_complex_run(self): """Test running workflow with several data objects.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start self.test_data = [1, 20] final_data = [1, 38] workflow = start(workflow_name="demo_workflow", data=self.test_data, module_name="unit_tests") # Get parent objects of the workflow we just ran objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None # noqa E711 ).order_by(BibWorkflowObject.id).all() # Let's check that we found anything. # There should only be three objects self.assertEqual(2, len(objects)) all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid).order_by( BibWorkflowObject.id).all() self.assertEqual(4, len(all_objects)) for obj in objects: # The child object should have the final or halted version self.assertTrue( obj.child_objects[0].version in (ObjectVersion.INITIAL, ObjectVersion.HALTED)) # Making sure the final data is correct self.assertTrue(obj.get_data() in final_data) self.assertTrue(obj.child_objects[0].get_data() in self.test_data)
def test_halt(self): """Test halt task.""" from invenio_workflows.registry import workflows from invenio_workflows.api import start from invenio_workflows.engine import WorkflowStatus from invenio_workflows.models import (BibWorkflowObjectLog, ObjectVersion) def halt_engine(obj, eng): return eng.halt("Test") class HaltTest(object): workflow = [halt_engine] workflows['halttest'] = HaltTest data = [set(('somekey', 'somevalue'))] eng = start('halttest', data, module_name="unit_tests") idx, obj = list(eng.getObjects())[0] self.assertEqual(ObjectVersion.WAITING, obj.version) self.assertEqual(WorkflowStatus.HALTED, eng.status) self.assertEqual(0, BibWorkflowObjectLog.get( id_object=obj.id, log_type=logging.ERROR).count())
def test_workflow_complex_run(self): """Test running workflow with several data objects.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start self.test_data = [1, 20] final_data = [1, 38] workflow = start(workflow_name="demo_workflow", data=self.test_data, module_name="unit_tests") # Get parent objects of the workflow we just ran objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid, BibWorkflowObject.id_parent == None # noqa E711 ).order_by(BibWorkflowObject.id).all() # Let's check that we found anything. # There should only be three objects self.assertEqual(2, len(objects)) all_objects = BibWorkflowObject.query.filter( BibWorkflowObject.id_workflow == workflow.uuid ).order_by(BibWorkflowObject.id).all() self.assertEqual(4, len(all_objects)) for obj in objects: # The child object should have the final or halted version self.assertTrue(obj.child_objects[0].version in (ObjectVersion.INITIAL, ObjectVersion.HALTED)) # Making sure the final data is correct self.assertTrue(obj.get_data() in final_data) self.assertTrue(obj.child_objects[0].get_data() in self.test_data)