Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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_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())
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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_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())
Ejemplo n.º 8
0
    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())
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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_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)
Ejemplo n.º 13
0
    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"])
Ejemplo n.º 14
0
    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"])
Ejemplo n.º 15
0
    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)
Ejemplo n.º 16
0
    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)
Ejemplo n.º 17
0
    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)
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
0
    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)