test_code.py 10 KB

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