Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
test_workflow.py124 linesDownload Raw Back to models
1from unittest import mock2from uuid import uuid43 4import contexts5from constants import HIDDEN_VALUE6from core.variables import FloatVariable, IntegerVariable, SecretVariable, StringVariable7from models.workflow import Workflow8 9 10def test_environment_variables():11    contexts.tenant_id.set("tenant_id")12 13    # Create a Workflow instance14    workflow = Workflow(15        tenant_id="tenant_id",16        app_id="app_id",17        type="workflow",18        version="draft",19        graph="{}",20        features="{}",21        created_by="account_id",22        environment_variables=[],23        conversation_variables=[],24    )25 26    # Create some EnvironmentVariable instances27    variable1 = StringVariable.model_validate({"name": "var1", "value": "value1", "id": str(uuid4())})28    variable2 = IntegerVariable.model_validate({"name": "var2", "value": 123, "id": str(uuid4())})29    variable3 = SecretVariable.model_validate({"name": "var3", "value": "secret", "id": str(uuid4())})30    variable4 = FloatVariable.model_validate({"name": "var4", "value": 3.14, "id": str(uuid4())})31 32    with (33        mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),34        mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),35    ):36        # Set the environment_variables property of the Workflow instance37        variables = [variable1, variable2, variable3, variable4]38        workflow.environment_variables = variables39 40        # Get the environment_variables property and assert its value41        assert workflow.environment_variables == variables42 43 44def test_update_environment_variables():45    contexts.tenant_id.set("tenant_id")46 47    # Create a Workflow instance48    workflow = Workflow(49        tenant_id="tenant_id",50        app_id="app_id",51        type="workflow",52        version="draft",53        graph="{}",54        features="{}",55        created_by="account_id",56        environment_variables=[],57        conversation_variables=[],58    )59 60    # Create some EnvironmentVariable instances61    variable1 = StringVariable.model_validate({"name": "var1", "value": "value1", "id": str(uuid4())})62    variable2 = IntegerVariable.model_validate({"name": "var2", "value": 123, "id": str(uuid4())})63    variable3 = SecretVariable.model_validate({"name": "var3", "value": "secret", "id": str(uuid4())})64    variable4 = FloatVariable.model_validate({"name": "var4", "value": 3.14, "id": str(uuid4())})65 66    with (67        mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),68        mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),69    ):70        variables = [variable1, variable2, variable3, variable4]71 72        # Set the environment_variables property of the Workflow instance73        workflow.environment_variables = variables74        assert workflow.environment_variables == [variable1, variable2, variable3, variable4]75 76        # Update the name of variable3 and keep the value as it is77        variables[2] = variable3.model_copy(78            update={79                "name": "new name",80                "value": HIDDEN_VALUE,81            }82        )83 84        workflow.environment_variables = variables85        assert workflow.environment_variables[2].name == "new name"86        assert workflow.environment_variables[2].value == variable3.value87 88 89def test_to_dict():90    contexts.tenant_id.set("tenant_id")91 92    # Create a Workflow instance93    workflow = Workflow(94        tenant_id="tenant_id",95        app_id="app_id",96        type="workflow",97        version="draft",98        graph="{}",99        features="{}",100        created_by="account_id",101        environment_variables=[],102        conversation_variables=[],103    )104 105    # Create some EnvironmentVariable instances106 107    with (108        mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),109        mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),110    ):111        # Set the environment_variables property of the Workflow instance112        workflow.environment_variables = [113            SecretVariable.model_validate({"name": "secret", "value": "secret", "id": str(uuid4())}),114            StringVariable.model_validate({"name": "text", "value": "text", "id": str(uuid4())}),115        ]116 117        workflow_dict = workflow.to_dict()118        assert workflow_dict["environment_variables"][0]["value"] == ""119        assert workflow_dict["environment_variables"][1]["value"] == "text"120 121        workflow_dict = workflow.to_dict(include_secret=True)122        assert workflow_dict["environment_variables"][0]["value"] == "secret"123        assert workflow_dict["environment_variables"][1]["value"] == "text"124