test_code.py 10.0 KB

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