Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test_workflow.py 5.9KB

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