You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_workflow.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import dataclasses
  2. import json
  3. from unittest import mock
  4. from uuid import uuid4
  5. from constants import HIDDEN_VALUE
  6. from core.file.enums import FileTransferMethod, FileType
  7. from core.file.models import File
  8. from core.variables import FloatVariable, IntegerVariable, SecretVariable, StringVariable
  9. from core.variables.segments import IntegerSegment, Segment
  10. from factories.variable_factory import build_segment
  11. from models.workflow import Workflow, WorkflowDraftVariable, WorkflowNodeExecutionModel, is_system_variable_editable
  12. def test_environment_variables():
  13. # tenant_id context variable removed - using current_user.current_tenant_id directly
  14. # Create a Workflow instance
  15. workflow = Workflow(
  16. tenant_id="tenant_id",
  17. app_id="app_id",
  18. type="workflow",
  19. version="draft",
  20. graph="{}",
  21. features="{}",
  22. created_by="account_id",
  23. environment_variables=[],
  24. conversation_variables=[],
  25. )
  26. # Create some EnvironmentVariable instances
  27. variable1 = StringVariable.model_validate(
  28. {"name": "var1", "value": "value1", "id": str(uuid4()), "selector": ["env", "var1"]}
  29. )
  30. variable2 = IntegerVariable.model_validate(
  31. {"name": "var2", "value": 123, "id": str(uuid4()), "selector": ["env", "var2"]}
  32. )
  33. variable3 = SecretVariable.model_validate(
  34. {"name": "var3", "value": "secret", "id": str(uuid4()), "selector": ["env", "var3"]}
  35. )
  36. variable4 = FloatVariable.model_validate(
  37. {"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]}
  38. )
  39. # Mock current_user as an EndUser
  40. mock_user = mock.Mock()
  41. mock_user.tenant_id = "tenant_id"
  42. with (
  43. mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
  44. mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
  45. mock.patch("models.workflow.current_user", mock_user),
  46. ):
  47. # Set the environment_variables property of the Workflow instance
  48. variables = [variable1, variable2, variable3, variable4]
  49. workflow.environment_variables = variables
  50. # Get the environment_variables property and assert its value
  51. assert workflow.environment_variables == variables
  52. def test_update_environment_variables():
  53. # tenant_id context variable removed - using current_user.current_tenant_id directly
  54. # Create a Workflow instance
  55. workflow = Workflow(
  56. tenant_id="tenant_id",
  57. app_id="app_id",
  58. type="workflow",
  59. version="draft",
  60. graph="{}",
  61. features="{}",
  62. created_by="account_id",
  63. environment_variables=[],
  64. conversation_variables=[],
  65. )
  66. # Create some EnvironmentVariable instances
  67. variable1 = StringVariable.model_validate(
  68. {"name": "var1", "value": "value1", "id": str(uuid4()), "selector": ["env", "var1"]}
  69. )
  70. variable2 = IntegerVariable.model_validate(
  71. {"name": "var2", "value": 123, "id": str(uuid4()), "selector": ["env", "var2"]}
  72. )
  73. variable3 = SecretVariable.model_validate(
  74. {"name": "var3", "value": "secret", "id": str(uuid4()), "selector": ["env", "var3"]}
  75. )
  76. variable4 = FloatVariable.model_validate(
  77. {"name": "var4", "value": 3.14, "id": str(uuid4()), "selector": ["env", "var4"]}
  78. )
  79. # Mock current_user as an EndUser
  80. mock_user = mock.Mock()
  81. mock_user.tenant_id = "tenant_id"
  82. with (
  83. mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
  84. mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
  85. mock.patch("models.workflow.current_user", mock_user),
  86. ):
  87. variables = [variable1, variable2, variable3, variable4]
  88. # Set the environment_variables property of the Workflow instance
  89. workflow.environment_variables = variables
  90. assert workflow.environment_variables == [variable1, variable2, variable3, variable4]
  91. # Update the name of variable3 and keep the value as it is
  92. variables[2] = variable3.model_copy(
  93. update={
  94. "name": "new name",
  95. "value": HIDDEN_VALUE,
  96. }
  97. )
  98. workflow.environment_variables = variables
  99. assert workflow.environment_variables[2].name == "new name"
  100. assert workflow.environment_variables[2].value == variable3.value
  101. def test_to_dict():
  102. # tenant_id context variable removed - using current_user.current_tenant_id directly
  103. # Create a Workflow instance
  104. workflow = Workflow(
  105. tenant_id="tenant_id",
  106. app_id="app_id",
  107. type="workflow",
  108. version="draft",
  109. graph="{}",
  110. features="{}",
  111. created_by="account_id",
  112. environment_variables=[],
  113. conversation_variables=[],
  114. )
  115. # Create some EnvironmentVariable instances
  116. # Mock current_user as an EndUser
  117. mock_user = mock.Mock()
  118. mock_user.tenant_id = "tenant_id"
  119. with (
  120. mock.patch("core.helper.encrypter.encrypt_token", return_value="encrypted_token"),
  121. mock.patch("core.helper.encrypter.decrypt_token", return_value="secret"),
  122. mock.patch("models.workflow.current_user", mock_user),
  123. ):
  124. # Set the environment_variables property of the Workflow instance
  125. workflow.environment_variables = [
  126. SecretVariable.model_validate({"name": "secret", "value": "secret", "id": str(uuid4())}),
  127. StringVariable.model_validate({"name": "text", "value": "text", "id": str(uuid4())}),
  128. ]
  129. workflow_dict = workflow.to_dict()
  130. assert workflow_dict["environment_variables"][0]["value"] == ""
  131. assert workflow_dict["environment_variables"][1]["value"] == "text"
  132. workflow_dict = workflow.to_dict(include_secret=True)
  133. assert workflow_dict["environment_variables"][0]["value"] == "secret"
  134. assert workflow_dict["environment_variables"][1]["value"] == "text"
  135. class TestWorkflowNodeExecution:
  136. def test_execution_metadata_dict(self):
  137. node_exec = WorkflowNodeExecutionModel()
  138. node_exec.execution_metadata = None
  139. assert node_exec.execution_metadata_dict == {}
  140. original = {"a": 1, "b": ["2"]}
  141. node_exec.execution_metadata = json.dumps(original)
  142. assert node_exec.execution_metadata_dict == original
  143. class TestIsSystemVariableEditable:
  144. def test_is_system_variable(self):
  145. cases = [
  146. ("query", True),
  147. ("files", True),
  148. ("dialogue_count", False),
  149. ("conversation_id", False),
  150. ("user_id", False),
  151. ("app_id", False),
  152. ("workflow_id", False),
  153. ("workflow_run_id", False),
  154. ]
  155. for name, editable in cases:
  156. assert editable == is_system_variable_editable(name)
  157. assert is_system_variable_editable("invalid_or_new_system_variable") == False
  158. class TestWorkflowDraftVariableGetValue:
  159. def test_get_value_by_case(self):
  160. @dataclasses.dataclass
  161. class TestCase:
  162. name: str
  163. value: Segment
  164. tenant_id = "test_tenant_id"
  165. test_file = File(
  166. tenant_id=tenant_id,
  167. type=FileType.IMAGE,
  168. transfer_method=FileTransferMethod.REMOTE_URL,
  169. remote_url="https://example.com/example.jpg",
  170. filename="example.jpg",
  171. extension=".jpg",
  172. mime_type="image/jpeg",
  173. size=100,
  174. )
  175. cases: list[TestCase] = [
  176. TestCase(
  177. name="number/int",
  178. value=build_segment(1),
  179. ),
  180. TestCase(
  181. name="number/float",
  182. value=build_segment(1.0),
  183. ),
  184. TestCase(
  185. name="string",
  186. value=build_segment("a"),
  187. ),
  188. TestCase(
  189. name="object",
  190. value=build_segment({}),
  191. ),
  192. TestCase(
  193. name="file",
  194. value=build_segment(test_file),
  195. ),
  196. TestCase(
  197. name="array[any]",
  198. value=build_segment([1, "a"]),
  199. ),
  200. TestCase(
  201. name="array[string]",
  202. value=build_segment(["a", "b"]),
  203. ),
  204. TestCase(
  205. name="array[number]/int",
  206. value=build_segment([1, 2]),
  207. ),
  208. TestCase(
  209. name="array[number]/float",
  210. value=build_segment([1.0, 2.0]),
  211. ),
  212. TestCase(
  213. name="array[number]/mixed",
  214. value=build_segment([1, 2.0]),
  215. ),
  216. TestCase(
  217. name="array[object]",
  218. value=build_segment([{}, {"a": 1}]),
  219. ),
  220. TestCase(
  221. name="none",
  222. value=build_segment(None),
  223. ),
  224. ]
  225. for idx, c in enumerate(cases, 1):
  226. fail_msg = f"test case {c.name} failed, index={idx}"
  227. draft_var = WorkflowDraftVariable()
  228. draft_var.set_value(c.value)
  229. assert c.value == draft_var.get_value(), fail_msg
  230. def test_file_variable_preserves_all_fields(self):
  231. """Test that File type variables preserve all fields during encoding/decoding."""
  232. tenant_id = "test_tenant_id"
  233. # Create a File with specific field values
  234. test_file = File(
  235. id="test_file_id",
  236. tenant_id=tenant_id,
  237. type=FileType.IMAGE,
  238. transfer_method=FileTransferMethod.REMOTE_URL,
  239. remote_url="https://example.com/test.jpg",
  240. filename="test.jpg",
  241. extension=".jpg",
  242. mime_type="image/jpeg",
  243. size=12345, # Specific size to test preservation
  244. storage_key="test_storage_key",
  245. )
  246. # Create a FileSegment and WorkflowDraftVariable
  247. file_segment = build_segment(test_file)
  248. draft_var = WorkflowDraftVariable()
  249. draft_var.set_value(file_segment)
  250. # Retrieve the value and verify all fields are preserved
  251. retrieved_segment = draft_var.get_value()
  252. retrieved_file = retrieved_segment.value
  253. # Verify all important fields are preserved
  254. assert retrieved_file.id == test_file.id
  255. assert retrieved_file.tenant_id == test_file.tenant_id
  256. assert retrieved_file.type == test_file.type
  257. assert retrieved_file.transfer_method == test_file.transfer_method
  258. assert retrieved_file.remote_url == test_file.remote_url
  259. assert retrieved_file.filename == test_file.filename
  260. assert retrieved_file.extension == test_file.extension
  261. assert retrieved_file.mime_type == test_file.mime_type
  262. assert retrieved_file.size == test_file.size # This was the main issue being fixed
  263. # Note: storage_key is not serialized in model_dump() so it won't be preserved
  264. # Verify the segments have the same type and the important fields match
  265. assert file_segment.value_type == retrieved_segment.value_type
  266. def test_get_and_set_value(self):
  267. draft_var = WorkflowDraftVariable()
  268. int_var = IntegerSegment(value=1)
  269. draft_var.set_value(int_var)
  270. value = draft_var.get_value()
  271. assert value == int_var