test_code.py 9.5 KB

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