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_code.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import time
  2. import uuid
  3. from os import getenv
  4. from typing import cast
  5. import pytest
  6. from core.app.entities.app_invoke_entities import InvokeFrom
  7. from core.workflow.entities.node_entities import NodeRunResult
  8. from core.workflow.entities.variable_pool import VariablePool
  9. from core.workflow.entities.workflow_node_execution import WorkflowNodeExecutionStatus
  10. from core.workflow.enums import SystemVariableKey
  11. from core.workflow.graph_engine.entities.graph import Graph
  12. from core.workflow.graph_engine.entities.graph_init_params import GraphInitParams
  13. from core.workflow.graph_engine.entities.graph_runtime_state import GraphRuntimeState
  14. from core.workflow.nodes.code.code_node import CodeNode
  15. from core.workflow.nodes.code.entities import CodeNodeData
  16. from models.enums import UserFrom
  17. from models.workflow import WorkflowType
  18. from tests.integration_tests.workflow.nodes.__mock.code_executor import setup_code_executor_mock
  19. CODE_MAX_STRING_LENGTH = int(getenv("CODE_MAX_STRING_LENGTH", "10000"))
  20. def init_code_node(code_config: dict):
  21. graph_config = {
  22. "edges": [
  23. {
  24. "id": "start-source-code-target",
  25. "source": "start",
  26. "target": "code",
  27. },
  28. ],
  29. "nodes": [{"data": {"type": "start"}, "id": "start"}, code_config],
  30. }
  31. graph = Graph.init(graph_config=graph_config)
  32. init_params = GraphInitParams(
  33. tenant_id="1",
  34. app_id="1",
  35. workflow_type=WorkflowType.WORKFLOW,
  36. workflow_id="1",
  37. graph_config=graph_config,
  38. user_id="1",
  39. user_from=UserFrom.ACCOUNT,
  40. invoke_from=InvokeFrom.DEBUGGER,
  41. call_depth=0,
  42. )
  43. # construct variable pool
  44. variable_pool = VariablePool(
  45. system_variables={SystemVariableKey.FILES: [], SystemVariableKey.USER_ID: "aaa"},
  46. user_inputs={},
  47. environment_variables=[],
  48. conversation_variables=[],
  49. )
  50. variable_pool.add(["code", "123", "args1"], 1)
  51. variable_pool.add(["code", "123", "args2"], 2)
  52. node = CodeNode(
  53. id=str(uuid.uuid4()),
  54. graph_init_params=init_params,
  55. graph=graph,
  56. graph_runtime_state=GraphRuntimeState(variable_pool=variable_pool, start_at=time.perf_counter()),
  57. config=code_config,
  58. )
  59. return node
  60. @pytest.mark.parametrize("setup_code_executor_mock", [["none"]], indirect=True)
  61. def test_execute_code(setup_code_executor_mock):
  62. code = """
  63. def main(args1: int, args2: int) -> dict:
  64. return {
  65. "result": args1 + args2,
  66. }
  67. """
  68. # trim first 4 spaces at the beginning of each line
  69. code = "\n".join([line[4:] for line in code.split("\n")])
  70. code_config = {
  71. "id": "code",
  72. "data": {
  73. "outputs": {
  74. "result": {
  75. "type": "number",
  76. },
  77. },
  78. "title": "123",
  79. "variables": [
  80. {
  81. "variable": "args1",
  82. "value_selector": ["1", "123", "args1"],
  83. },
  84. {"variable": "args2", "value_selector": ["1", "123", "args2"]},
  85. ],
  86. "answer": "123",
  87. "code_language": "python3",
  88. "code": code,
  89. },
  90. }
  91. node = init_code_node(code_config)
  92. node.graph_runtime_state.variable_pool.add(["1", "123", "args1"], 1)
  93. node.graph_runtime_state.variable_pool.add(["1", "123", "args2"], 2)
  94. # execute node
  95. result = node._run()
  96. assert isinstance(result, NodeRunResult)
  97. assert result.status == WorkflowNodeExecutionStatus.SUCCEEDED
  98. assert result.outputs is not None
  99. assert result.outputs["result"] == 3
  100. assert result.error is None
  101. @pytest.mark.parametrize("setup_code_executor_mock", [["none"]], indirect=True)
  102. def test_execute_code_output_validator(setup_code_executor_mock):
  103. code = """
  104. def main(args1: int, args2: int) -> dict:
  105. return {
  106. "result": args1 + args2,
  107. }
  108. """
  109. # trim first 4 spaces at the beginning of each line
  110. code = "\n".join([line[4:] for line in code.split("\n")])
  111. code_config = {
  112. "id": "code",
  113. "data": {
  114. "outputs": {
  115. "result": {
  116. "type": "string",
  117. },
  118. },
  119. "title": "123",
  120. "variables": [
  121. {
  122. "variable": "args1",
  123. "value_selector": ["1", "123", "args1"],
  124. },
  125. {"variable": "args2", "value_selector": ["1", "123", "args2"]},
  126. ],
  127. "answer": "123",
  128. "code_language": "python3",
  129. "code": code,
  130. },
  131. }
  132. node = init_code_node(code_config)
  133. node.graph_runtime_state.variable_pool.add(["1", "123", "args1"], 1)
  134. node.graph_runtime_state.variable_pool.add(["1", "123", "args2"], 2)
  135. # execute node
  136. result = node._run()
  137. assert isinstance(result, NodeRunResult)
  138. assert result.status == WorkflowNodeExecutionStatus.FAILED
  139. assert result.error == "Output variable `result` must be a string"
  140. def test_execute_code_output_validator_depth():
  141. code = """
  142. def main(args1: int, args2: int) -> dict:
  143. return {
  144. "result": {
  145. "result": args1 + args2,
  146. }
  147. }
  148. """
  149. # trim first 4 spaces at the beginning of each line
  150. code = "\n".join([line[4:] for line in code.split("\n")])
  151. code_config = {
  152. "id": "code",
  153. "data": {
  154. "outputs": {
  155. "string_validator": {
  156. "type": "string",
  157. },
  158. "number_validator": {
  159. "type": "number",
  160. },
  161. "number_array_validator": {
  162. "type": "array[number]",
  163. },
  164. "string_array_validator": {
  165. "type": "array[string]",
  166. },
  167. "object_validator": {
  168. "type": "object",
  169. "children": {
  170. "result": {
  171. "type": "number",
  172. },
  173. "depth": {
  174. "type": "object",
  175. "children": {
  176. "depth": {
  177. "type": "object",
  178. "children": {
  179. "depth": {
  180. "type": "number",
  181. }
  182. },
  183. }
  184. },
  185. },
  186. },
  187. },
  188. },
  189. "title": "123",
  190. "variables": [
  191. {
  192. "variable": "args1",
  193. "value_selector": ["1", "123", "args1"],
  194. },
  195. {"variable": "args2", "value_selector": ["1", "123", "args2"]},
  196. ],
  197. "answer": "123",
  198. "code_language": "python3",
  199. "code": code,
  200. },
  201. }
  202. node = init_code_node(code_config)
  203. # construct result
  204. result = {
  205. "number_validator": 1,
  206. "string_validator": "1",
  207. "number_array_validator": [1, 2, 3, 3.333],
  208. "string_array_validator": ["1", "2", "3"],
  209. "object_validator": {"result": 1, "depth": {"depth": {"depth": 1}}},
  210. }
  211. node.node_data = cast(CodeNodeData, node.node_data)
  212. # validate
  213. node._transform_result(result, node.node_data.outputs)
  214. # construct result
  215. result = {
  216. "number_validator": "1",
  217. "string_validator": 1,
  218. "number_array_validator": ["1", "2", "3", "3.333"],
  219. "string_array_validator": [1, 2, 3],
  220. "object_validator": {"result": "1", "depth": {"depth": {"depth": "1"}}},
  221. }
  222. # validate
  223. with pytest.raises(ValueError):
  224. node._transform_result(result, node.node_data.outputs)
  225. # construct result
  226. result = {
  227. "number_validator": 1,
  228. "string_validator": (CODE_MAX_STRING_LENGTH + 1) * "1",
  229. "number_array_validator": [1, 2, 3, 3.333],
  230. "string_array_validator": ["1", "2", "3"],
  231. "object_validator": {"result": 1, "depth": {"depth": {"depth": 1}}},
  232. }
  233. # validate
  234. with pytest.raises(ValueError):
  235. node._transform_result(result, node.node_data.outputs)
  236. # construct result
  237. result = {
  238. "number_validator": 1,
  239. "string_validator": "1",
  240. "number_array_validator": [1, 2, 3, 3.333] * 2000,
  241. "string_array_validator": ["1", "2", "3"],
  242. "object_validator": {"result": 1, "depth": {"depth": {"depth": 1}}},
  243. }
  244. # validate
  245. with pytest.raises(ValueError):
  246. node._transform_result(result, node.node_data.outputs)
  247. def test_execute_code_output_object_list():
  248. code = """
  249. def main(args1: int, args2: int) -> dict:
  250. return {
  251. "result": {
  252. "result": args1 + args2,
  253. }
  254. }
  255. """
  256. # trim first 4 spaces at the beginning of each line
  257. code = "\n".join([line[4:] for line in code.split("\n")])
  258. code_config = {
  259. "id": "code",
  260. "data": {
  261. "outputs": {
  262. "object_list": {
  263. "type": "array[object]",
  264. },
  265. },
  266. "title": "123",
  267. "variables": [
  268. {
  269. "variable": "args1",
  270. "value_selector": ["1", "123", "args1"],
  271. },
  272. {"variable": "args2", "value_selector": ["1", "123", "args2"]},
  273. ],
  274. "answer": "123",
  275. "code_language": "python3",
  276. "code": code,
  277. },
  278. }
  279. node = init_code_node(code_config)
  280. # construct result
  281. result = {
  282. "object_list": [
  283. {
  284. "result": 1,
  285. },
  286. {
  287. "result": 2,
  288. },
  289. {
  290. "result": [1, 2, 3],
  291. },
  292. ]
  293. }
  294. node.node_data = cast(CodeNodeData, node.node_data)
  295. # validate
  296. node._transform_result(result, node.node_data.outputs)
  297. # construct result
  298. result = {
  299. "object_list": [
  300. {
  301. "result": 1,
  302. },
  303. {
  304. "result": 2,
  305. },
  306. {
  307. "result": [1, 2, 3],
  308. },
  309. 1,
  310. ]
  311. }
  312. # validate
  313. with pytest.raises(ValueError):
  314. node._transform_result(result, node.node_data.outputs)