您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test_workflow.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import json
  2. from unittest import mock
  3. from uuid import uuid4
  4. import contexts
  5. from constants import HIDDEN_VALUE
  6. from core.variables import FloatVariable, IntegerVariable, SecretVariable, StringVariable
  7. from models.workflow import Workflow, WorkflowNodeExecution
  8. def test_environment_variables():
  9. contexts.tenant_id.set("tenant_id")
  10. # Create a Workflow instance
  11. workflow = Workflow(
  12. tenant_id="tenant_id",
  13. app_id="app_id",
  14. type="workflow",
  15. version="draft",
  16. graph="{}",
  17. features="{}",
  18. created_by="account_id",
  19. environment_variables=[],
  20. conversation_variables=[],
  21. )
  22. # Create some EnvironmentVariable instances
  23. variable1 = StringVariable.model_validate(
  24. {"name": "var1", "value": "value1", "id": str(uuid4()), "selector": ["env", "var1"]}
  25. )
  26. variable2 = IntegerVariable.model_validate(
  27. {"name": "var2", "value": 123, "id": str(uuid4()), "selector": ["env", "var2"]}
  28. )
  29. variable3 = SecretVariable.model_validate(
  30. {"name": "var3", "value": "secret", "id": str(uuid4()), "selector": ["env", "var3"]}
  31. )
  32. variable4 = FloatVariable.model_validate(
  33. {"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]}
  34. )
  35. with (
  36. mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
  37. mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
  38. ):
  39. # Set the environment_variables property of the Workflow instance
  40. variables = [variable1, variable2, variable3, variable4]
  41. workflow.environment_variables = variables
  42. # Get the environment_variables property and assert its value
  43. assert workflow.environment_variables == variables
  44. def test_update_environment_variables():
  45. contexts.tenant_id.set("tenant_id")
  46. # Create a Workflow instance
  47. workflow = Workflow(
  48. tenant_id="tenant_id",
  49. app_id="app_id",
  50. type="workflow",
  51. version="draft",
  52. graph="{}",
  53. features="{}",
  54. created_by="account_id",
  55. environment_variables=[],
  56. conversation_variables=[],
  57. )
  58. # Create some EnvironmentVariable instances
  59. variable1 = StringVariable.model_validate(
  60. {"name": "var1", "value": "value1", "id": str(uuid4()), "selector": ["env", "var1"]}
  61. )
  62. variable2 = IntegerVariable.model_validate(
  63. {"name": "var2", "value": 123, "id": str(uuid4()), "selector": ["env", "var2"]}
  64. )
  65. variable3 = SecretVariable.model_validate(
  66. {"name": "var3", "value": "secret", "id": str(uuid4()), "selector": ["env", "var3"]}
  67. )
  68. variable4 = FloatVariable.model_validate(
  69. {"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]}
  70. )
  71. with (
  72. mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
  73. mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
  74. ):
  75. variables = [variable1, variable2, variable3, variable4]
  76. # Set the environment_variables property of the Workflow instance
  77. workflow.environment_variables = variables
  78. assert workflow.environment_variables == [variable1, variable2, variable3, variable4]
  79. # Update the name of variable3 and keep the value as it is
  80. variables[2] = variable3.model_copy(
  81. update={
  82. "name": "new name",
  83. "value": HIDDEN_VALUE,
  84. }
  85. )
  86. workflow.environment_variables = variables
  87. assert workflow.environment_variables[2].name == "new name"
  88. assert workflow.environment_variables[2].value == variable3.value
  89. def test_to_dict():
  90. contexts.tenant_id.set("tenant_id")
  91. # Create a Workflow instance
  92. workflow = Workflow(
  93. tenant_id="tenant_id",
  94. app_id="app_id",
  95. type="workflow",
  96. version="draft",
  97. graph="{}",
  98. features="{}",
  99. created_by="account_id",
  100. environment_variables=[],
  101. conversation_variables=[],
  102. )
  103. # Create some EnvironmentVariable instances
  104. with (
  105. mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
  106. mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
  107. ):
  108. # Set the environment_variables property of the Workflow instance
  109. workflow.environment_variables = [
  110. SecretVariable.model_validate({"name": "secret", "value": "secret", "id": str(uuid4())}),
  111. StringVariable.model_validate({"name": "text", "value": "text", "id": str(uuid4())}),
  112. ]
  113. workflow_dict = workflow.to_dict()
  114. assert workflow_dict["environment_variables"][0]["value"] == ""
  115. assert workflow_dict["environment_variables"][1]["value"] == "text"
  116. workflow_dict = workflow.to_dict(include_secret=True)
  117. assert workflow_dict["environment_variables"][0]["value"] == "secret"
  118. assert workflow_dict["environment_variables"][1]["value"] == "text"
  119. class TestWorkflowNodeExecution:
  120. def test_execution_metadata_dict(self):
  121. node_exec = WorkflowNodeExecution()
  122. node_exec.execution_metadata = None
  123. assert node_exec.execution_metadata_dict == {}
  124. original = {"a": 1, "b": ["2"]}
  125. node_exec.execution_metadata = json.dumps(original)
  126. assert node_exec.execution_metadata_dict == original