def test_payload_file_creation(self): """Can add a file to a Payload.""" from invenio_workflows.models import BibWorkflowObject from inspire.modules.workflows.models import Payload from inspire.utils.helpers import ( get_file_by_name, add_file_by_name, ) obj = BibWorkflowObject.create_object() obj.save() obj.data = obj.get_data() # FIXME hack until workflow 2.0 payload = Payload.create(workflow_object=obj, type="payload_fixture") payload.save() fd, filename = tempfile.mkstemp() os.close(fd) newpath = add_file_by_name(payload, filename) self.assertTrue(newpath) self.assertTrue(get_file_by_name(payload, os.path.basename(filename))) BibWorkflowObject.delete(obj)
def setUp(self): """Setup tests.""" from invenio_workflows.models import BibWorkflowObject, \ Workflow from uuid import uuid1 as new_uuid self.workflow = Workflow(name='demo_workflow', uuid=new_uuid(), id_user=0, module_name="Unknown") self.bibworkflowobject = BibWorkflowObject(workflow=self.workflow) self.create_objects([self.workflow, self.bibworkflowobject])
def delete_self_and_stop_processing(obj, eng): """Delete both versions of itself and stops the workflow.""" from invenio_workflows.models import BibWorkflowObject # delete snapshot created with original data initial_obj = BibWorkflowObject.query.filter( BibWorkflowObject.id_parent == obj.id ).one() BibWorkflowObject.delete(initial_obj.id) # delete self BibWorkflowObject.delete(obj.id) eng.skipToken()
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_logging_for_workflow_objects_without_workflow(self): """Test run a virtual object out of a workflow for test purpose.""" from invenio_workflows.models import (BibWorkflowObject, BibWorkflowObjectLog, ObjectVersion) initial_data = 20 obj_init = BibWorkflowObject(id_workflow=None, version=ObjectVersion.INITIAL) obj_init.set_data(initial_data) obj_init.save() err_msg = "This is an error message" info_msg = "This is an info message" obj_init.log.info(info_msg) obj_init.log.error("This is an error message") # FIXME: loglevels are simply overwritten somewhere in Celery # even if Celery is not being "used". # # This means loglevel.DEBUG is NOT working at the moment! # debug_msg = "This is a debug message" # obj_init.log.debug(debug_msg) obj_init.save() obj_test = BibWorkflowObjectLog.query.filter( BibWorkflowObjectLog.id_object == obj_init.id).all() messages_found = 0 for current_obj in obj_test: if current_obj.message == info_msg and messages_found == 0: messages_found += 1 elif current_obj.message == err_msg and messages_found == 1: messages_found += 1 self.assertEqual(2, messages_found)
class TestWorkflowModels(InvenioTestCase): """Test meant for testing the models available.""" def setUp(self): """Setup tests.""" from invenio_workflows.models import BibWorkflowObject, \ Workflow from uuid import uuid1 as new_uuid self.workflow = Workflow(name='demo_workflow', uuid=new_uuid(), id_user=0, module_name="Unknown") self.bibworkflowobject = BibWorkflowObject(workflow=self.workflow) self.create_objects([self.workflow, self.bibworkflowobject]) def tearDown(self): """Clean up tests.""" self.delete_objects([self.workflow, self.bibworkflowobject]) def test_deleting_workflow(self): """Test deleting workflow.""" from invenio_workflows.models import BibWorkflowObject, \ Workflow bwo_id = self.bibworkflowobject.id # delete workflow Workflow.delete(self.workflow.uuid) # assert bibworkflowobject is deleted self.assertFalse( db.session.query( BibWorkflowObject.query.filter( BibWorkflowObject.id == bwo_id).exists()).scalar()) def test_deleting_bibworkflowobject(self): """Test deleting workflowobject.""" from invenio_workflows.models import Workflow w_uuid = self.workflow.uuid # delete bibworkflowobject self.bibworkflowobject.delete(self.bibworkflowobject.id) # assert workflow is not deleted self.assertTrue( db.session.query( Workflow.query.filter( Workflow.uuid == w_uuid).exists()).scalar())
def test_restart_failed_workflow(self): """Test restarting workflow for given workflow id.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.engine import WorkflowStatus from invenio_workflows.api import start, start_by_oids from invenio_workflows.errors import WorkflowError initial_data = BibWorkflowObject.create_object() initial_data.set_data(1) initial_data.save() self.assertRaises( WorkflowError, start, workflow_name="demo_workflow_error", data=[initial_data], module_name="unit_tests" ) self.assertEqual(initial_data.version, ObjectVersion.ERROR) restarted_workflow = start_by_oids("demo_workflow", oids=[initial_data.id], module_name="unit_tests") self.assertEqual(initial_data.version, ObjectVersion.WAITING) self.assertEqual(restarted_workflow.status, WorkflowStatus.HALTED)
def test_workflows_tasks_chained(self): """Test delayed workflows in delayed workflow.""" from invenio_workflows.models import BibWorkflowObject from invenio_workflows.api import start_delayed from invenio_workflows.worker_result import uuid_to_workflow test_object = BibWorkflowObject() test_object.set_data(22) test_object.save() async = start_delayed("demo_workflow_workflows", [test_object], module_name="unit_tests") engine = async.get(uuid_to_workflow) from invenio_workflows.engine import WorkflowStatus self.assertEqual(21, engine.get_extra_data()["_nb_workflow_finish"]) self.assertEqual(0, engine.get_extra_data()["_nb_workflow_failed"]) self.assertEqual(WorkflowStatus.COMPLETED, engine.status)
def demoform(): """View for INSPIRE demo form.""" # from inspire.modules.forms.utils import DataExporter form = DemoForm(data={"nickname": "John Doe"}) ctx = { "action": url_for('.demoform'), "name": "inspireForm", "id": "inspireForm", } if form.validate_on_submit(): # If it is needed to post process the form keys, for example to match # the names in the JSONAlchemy, one can use the DataExporter. # The keys will then be renamed using `export_key` parameter. # visitor = DataExporter() # visitor.visit(form) # visitor.data from invenio_workflows.models import BibWorkflowObject from flask.ext.login import current_user myobj = BibWorkflowObject.create_object(id_user=current_user.get_id()) myobj.set_data(form.data) # Start workflow. delayed=True will execute the workflow in the # background using, for example, Celery. myobj.start_workflow("demoworkflow", delayed=True) return render_template('forms/form_demo_success.html', form=form) return render_template('forms/form_demo.html', form=form, **ctx)
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_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_workflow_object_creation(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 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_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_logging_for_workflow_objects_without_workflow(self): """Test run a virtual object out of a workflow for test purpose.""" from invenio_workflows.models import (BibWorkflowObject, BibWorkflowObjectLog, ObjectVersion) initial_data = 20 obj_init = BibWorkflowObject( id_workflow=None, version=ObjectVersion.INITIAL) obj_init.set_data(initial_data) obj_init.save() err_msg = "This is an error message" info_msg = "This is an info message" obj_init.log.info(info_msg) obj_init.log.error("This is an error message") # FIXME: loglevels are simply overwritten somewhere in Celery # even if Celery is not being "used". # # This means loglevel.DEBUG is NOT working at the moment! # debug_msg = "This is a debug message" # obj_init.log.debug(debug_msg) obj_init.save() obj_test = BibWorkflowObjectLog.query.filter( BibWorkflowObjectLog.id_object == obj_init.id).all() messages_found = 0 for current_obj in obj_test: if current_obj.message == info_msg and messages_found == 0: messages_found += 1 elif current_obj.message == err_msg and messages_found == 1: messages_found += 1 self.assertEqual(2, messages_found)
def test_result_abstraction(self): """Test abastraction layer for celery worker.""" from invenio_ext.sqlalchemy import db from invenio_workflows.utils import BibWorkflowObjectIdContainer from invenio_workflows.models import BibWorkflowObject from invenio_workflows.worker_result import AsynchronousResultWrapper bwoic = BibWorkflowObjectIdContainer(None) self.assertEqual(None, bwoic.get_object()) test_object = BibWorkflowObject() test_object.set_data(45) test_object.save() bwoic2 = BibWorkflowObjectIdContainer(test_object) self.assertEqual(bwoic2.get_object().id, test_object.id) result = bwoic2.to_dict() self.assertEqual(bwoic2.from_dict(result).id, test_object.id) db.session.delete(test_object) try: AsynchronousResultWrapper(None) except Exception as e: self.assertTrue(isinstance(e, TypeError))
def test_workflow_object_creation(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 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_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_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_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 __init__(self, workflow_object, type=None, user_id=None): """Init.""" self.workflow_object = workflow_object if not workflow_object: self.files = [] self.drafts = {} self.type = self.get_type(type) self.title = "" self.sips = [] self.workflow_object = BibWorkflowObject.create_object(id_user=user_id) # Ensure default data is set for all objects. self.update() else: self.__setstate__(workflow_object.get_data()) self.engine = None
def __init__(self, workflow_object, type=None, user_id=None): """Init.""" self.workflow_object = workflow_object if not workflow_object: self.files = [] self.drafts = {} self.type = self.get_type(type) self.title = '' self.sips = [] self.workflow_object = BibWorkflowObject.create_object( id_user=user_id, ) # Ensure default data is set for all objects. self.update() else: self.__setstate__(workflow_object.get_data()) self.engine = None
def test_workflow_task_results(self): """Test the setting and getting of task results.""" from invenio_workflows.models import BibWorkflowObject test_object = BibWorkflowObject() test_object.save() # Saving is needed to instantiate default values test_object.add_task_result("test", {"data": "testing"}) results = test_object.get_tasks_results() self.assertEqual(len(results.get("test")), 1) result_item = results.get("test")[0] self.assertEqual({"data": "testing"}, result_item.get("result")) self.assertEqual("workflows/results/default.html", result_item.get("template")) self.assertEqual("test", result_item.get("name"))
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 run_harvest(workflow, **kwargs): """Run given harvesting workflow in Celery.""" from invenio.base.globals import cfg from invenio_workflows.models import BibWorkflowObject args = { "workflow": workflow } args.update(**kwargs) data = BibWorkflowObject.create_object() extra_data = data.get_extra_data() extra_data["args"] = args extra_data["config"] = cfg["HARVESTER_WORKFLOWS_CONFIG"].get(workflow, {}) data.set_extra_data(extra_data) data.set_data({}) data.save() data.start_workflow(workflow, delayed=True)
def submitupdate(): """Form action handler for INSPIRE author update form.""" from inspirehep.modules.forms.utils import DataExporter from invenio_workflows.models import BibWorkflowObject from flask.ext.login import current_user form = AuthorUpdateForm(formdata=request.form) visitor = DataExporter() visitor.visit(form) myobj = BibWorkflowObject.create_object(id_user=current_user.get_id()) myobj.set_data(visitor.data) # Start workflow. delayed=True will execute the workflow in the # background using, for example, Celery. myobj.start_workflow("authorupdate", delayed=True) ctx = { "inspire_url": get_inspire_url(visitor.data) } return render_template('authors/forms/update_success.html', **ctx)
def test_restart_failed_workflow(self): """Test restarting workflow for given workflow id.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.engine import WorkflowStatus from invenio_workflows.api import start, start_by_oids from invenio_workflows.errors import WorkflowError initial_data = BibWorkflowObject.create_object() initial_data.set_data(1) initial_data.save() self.assertRaises(WorkflowError, start, workflow_name="demo_workflow_error", data=[initial_data], module_name="unit_tests") self.assertEqual(initial_data.version, ObjectVersion.ERROR) restarted_workflow = start_by_oids("demo_workflow", oids=[initial_data.id], module_name="unit_tests") self.assertEqual(initial_data.version, ObjectVersion.WAITING) self.assertEqual(restarted_workflow.status, WorkflowStatus.HALTED)
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_dirty_worker(self): """Deep test of celery worker.""" from invenio_workflows.workers.worker_celery import celery_run, celery_restart, celery_continue from invenio_workflows.utils import BibWorkflowObjectIdContainer from invenio_workflows.models import BibWorkflowObject, get_default_extra_data from invenio_workflows.engine import WorkflowStatus test_objectb = BibWorkflowObject() test_objectb.set_data(22) test_objectb.save() data = BibWorkflowObjectIdContainer(test_objectb).to_dict() celery_run("demo_workflow", [data], module_name="unit_tests") self.assertEqual(40, test_objectb.get_data()) test_object = BibWorkflowObject() test_object.set_data(22) test_object.save() test_objectc = BibWorkflowObject() test_objectc.set_data(22) test_objectc.save() data = [test_object, test_objectc] for i in range(0, len(data)): if isinstance(data[i], BibWorkflowObject): data[i] = BibWorkflowObjectIdContainer(data[i]).to_dict() celery_run("demo_workflow", data, module_name="unit_tests") self.assertEqual(40, test_object.get_data()) self.assertEqual(40, test_objectc.get_data()) test_object = BibWorkflowObject() test_object.save() test_object.set_data(0) from invenio_workflows.worker_result import uuid_to_workflow engine = uuid_to_workflow(celery_run("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"]) engine._extra_data = get_default_extra_data() # reset iterators celery_restart(engine.uuid) self.assertEqual(5, test_object.get_data()) self.assertEqual("lt9", test_object.get_extra_data()["test"]) celery_continue(test_object.id, "continue_next") self.assertEqual(6, test_object.get_data()) self.assertEqual("lt9", test_object.get_extra_data()["test"]) celery_continue(test_object.id, "continue_next") self.assertEqual(9, test_object.get_data()) self.assertEqual("gte9", test_object.get_extra_data()["test"]) celery_continue(test_object.id, "continue_next") self.assertEqual(15, test_object.get_data()) self.assertEqual("gte9", test_object.get_extra_data()["test"]) engine = uuid_to_workflow(celery_continue(test_object.id, "continue_next", module_name="unit_tests")) self.assertEqual(WorkflowStatus.COMPLETED, engine.status)
def test_workflow_delay(self): """Test simple delayed workflow.""" from flask import current_app from invenio_workflows.models import BibWorkflowObject from invenio_workflows.api import start_delayed, continue_oid_delayed, start_by_wid_delayed from invenio_workflows.engine import WorkflowStatus current_app.config["WORKFLOWS_SNAPSHOTS_ENABLED"] = True test_objectb = BibWorkflowObject() test_objectb.set_data(20) test_objectb.save() from invenio_workflows.worker_result import uuid_to_workflow asyncr = start_delayed("demo_workflow", [test_objectb], module_name="unit_tests") engineb = asyncr.get(uuid_to_workflow) self.assertEqual(38, test_objectb.get_data()) asyncr = start_by_wid_delayed(engineb.uuid) asyncr.get(uuid_to_workflow) self.assertEqual(38, test_objectb.get_data()) test_objecte = BibWorkflowObject() test_objecte.set_data(2) test_objecte.save() asyncr = start_delayed("demo_workflow", [test_objecte], module_name="unit_tests") engineb = asyncr.get(uuid_to_workflow) asyncr = continue_oid_delayed(test_objecte.id) engineb = asyncr.get(uuid_to_workflow) self.assertEqual(WorkflowStatus.COMPLETED, engineb.status) self.assertEqual(20, test_objecte.get_data())
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_workflow_for_running_object(self): """Test workflow with running object given and watch it fail.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start_by_oids from invenio_workflows.errors import WorkflowObjectVersionError obj_running = BibWorkflowObject() obj_running.set_data(1234) obj_running.save(version=ObjectVersion.RUNNING) try: start_by_oids( 'demo_workflow', [ obj_running.id], module_name="unit_tests") except Exception as e: self.assertTrue(isinstance(e, WorkflowObjectVersionError)) obj_running.delete(e.id_object) obj_running.delete(obj_running) obj_running = BibWorkflowObject() obj_running.set_data(1234) obj_running.save(version=ObjectVersion.RUNNING) try: start_by_oids( 'demo_workflow', [ obj_running.id], module_name="unit_tests") except Exception as e: self.assertTrue(isinstance(e, WorkflowObjectVersionError)) obj_running.delete(e.id_object) obj_running.delete(obj_running) obj_running = BibWorkflowObject() obj_running.set_data(1234) obj_running.save(version=5) try: start_by_oids('demo_workflow', [obj_running.id], module_name="unit_tests") except Exception as e: self.assertTrue(isinstance(e, WorkflowObjectVersionError)) obj_running.delete(e.id_object) obj_running.delete(obj_running)
def test_workflow_for_running_object(self): """Test workflow with running object given and watch it fail.""" from invenio_workflows.models import (BibWorkflowObject, ObjectVersion) from invenio_workflows.api import start_by_oids from invenio_workflows.errors import WorkflowObjectVersionError obj_running = BibWorkflowObject() obj_running.set_data(1234) obj_running.save(version=ObjectVersion.RUNNING) try: start_by_oids('demo_workflow', [obj_running.id], module_name="unit_tests") except Exception as e: self.assertTrue(isinstance(e, WorkflowObjectVersionError)) obj_running.delete(e.id_object) obj_running.delete(obj_running) obj_running = BibWorkflowObject() obj_running.set_data(1234) obj_running.save(version=ObjectVersion.RUNNING) try: start_by_oids('demo_workflow', [obj_running.id], module_name="unit_tests") except Exception as e: self.assertTrue(isinstance(e, WorkflowObjectVersionError)) obj_running.delete(e.id_object) obj_running.delete(obj_running) obj_running = BibWorkflowObject() obj_running.set_data(1234) obj_running.save(version=5) try: start_by_oids('demo_workflow', [obj_running.id], module_name="unit_tests") except Exception as e: self.assertTrue(isinstance(e, WorkflowObjectVersionError)) obj_running.delete(e.id_object) obj_running.delete(obj_running)
def test_workflow_delay(self): """Test simple delayed workflow.""" from invenio_workflows.models import BibWorkflowObject from invenio_workflows.api import (start_delayed, continue_oid_delayed, start_by_wid_delayed) from invenio_workflows.engine import WorkflowStatus test_objectb = BibWorkflowObject() test_objectb.set_data(20) test_objectb.save() from invenio_workflows.worker_result import uuid_to_workflow asyncr = start_delayed('demo_workflow', [test_objectb], module_name="unit_tests") engineb = asyncr.get(uuid_to_workflow) self.assertEqual(38, test_objectb.get_data()) asyncr = start_by_wid_delayed(engineb.uuid) asyncr.get(uuid_to_workflow) self.assertEqual(38, test_objectb.get_data()) test_objecte = BibWorkflowObject() test_objecte.set_data(2) test_objecte.save() asyncr = start_delayed('demo_workflow', [test_objecte], module_name="unit_tests") engineb = asyncr.get(uuid_to_workflow) asyncr = continue_oid_delayed(test_objecte.id) engineb = asyncr.get(uuid_to_workflow) self.assertEqual(WorkflowStatus.COMPLETED, engineb.status) self.assertEqual(20, test_objecte.get_data())