mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-10 03:16:51 +08:00
improve: test CodeExecutor with code templates and extract CodeLanguage enum (#4098)
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import pytest
|
||||
|
||||
from core.helper.code_executor.code_executor import CodeExecutionException, CodeExecutor
|
||||
|
||||
CODE_LANGUAGE = 'unsupported_language'
|
||||
|
||||
|
||||
def test_unsupported_with_code_template():
|
||||
with pytest.raises(CodeExecutionException) as e:
|
||||
CodeExecutor.execute_workflow_code_template(language=CODE_LANGUAGE, code='', inputs={})
|
||||
assert str(e.value) == f'Unsupported language {CODE_LANGUAGE}'
|
||||
@@ -1,6 +1,9 @@
|
||||
from core.helper.code_executor.code_executor import CodeExecutor
|
||||
from textwrap import dedent
|
||||
|
||||
CODE_LANGUAGE = 'javascript'
|
||||
from core.helper.code_executor.code_executor import CodeExecutor, CodeLanguage
|
||||
from core.workflow.nodes.code.code_node import JAVASCRIPT_DEFAULT_CODE
|
||||
|
||||
CODE_LANGUAGE = CodeLanguage.JAVASCRIPT
|
||||
|
||||
|
||||
def test_javascript_plain():
|
||||
@@ -10,9 +13,15 @@ def test_javascript_plain():
|
||||
|
||||
|
||||
def test_javascript_json():
|
||||
code = """
|
||||
obj = {'Hello': 'World'}
|
||||
console.log(JSON.stringify(obj))
|
||||
"""
|
||||
code = dedent("""
|
||||
obj = {'Hello': 'World'}
|
||||
console.log(JSON.stringify(obj))
|
||||
""")
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
assert result == '{"Hello":"World"}\n'
|
||||
|
||||
|
||||
def test_javascript_with_code_template():
|
||||
result = CodeExecutor.execute_workflow_code_template(
|
||||
language=CODE_LANGUAGE, code=JAVASCRIPT_DEFAULT_CODE, inputs={'arg1': 'Hello', 'arg2': 'World'})
|
||||
assert result == {'result': 'HelloWorld'}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import base64
|
||||
|
||||
from core.helper.code_executor.code_executor import CodeExecutor
|
||||
from core.helper.code_executor.code_executor import CodeExecutor, CodeLanguage
|
||||
from core.helper.code_executor.jinja2_transformer import JINJA2_PRELOAD, PYTHON_RUNNER
|
||||
|
||||
CODE_LANGUAGE = 'jinja2'
|
||||
CODE_LANGUAGE = CodeLanguage.JINJA2
|
||||
|
||||
|
||||
def test_jinja2():
|
||||
@@ -12,3 +12,9 @@ def test_jinja2():
|
||||
code = PYTHON_RUNNER.replace('{{code}}', template).replace('{{inputs}}', inputs)
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload=JINJA2_PRELOAD, code=code)
|
||||
assert result == '<<RESULT>>Hello World<<RESULT>>\n'
|
||||
|
||||
|
||||
def test_jinja2_with_code_template():
|
||||
result = CodeExecutor.execute_workflow_code_template(
|
||||
language=CODE_LANGUAGE, code='Hello {{template}}', inputs={'template': 'World'})
|
||||
assert result == {'result': 'Hello World'}
|
||||
@@ -1,6 +1,9 @@
|
||||
from core.helper.code_executor.code_executor import CodeExecutor
|
||||
from textwrap import dedent
|
||||
|
||||
CODE_LANGUAGE = 'python3'
|
||||
from core.helper.code_executor.code_executor import CodeExecutor, CodeLanguage
|
||||
from core.workflow.nodes.code.code_node import PYTHON_DEFAULT_CODE
|
||||
|
||||
CODE_LANGUAGE = CodeLanguage.PYTHON3
|
||||
|
||||
|
||||
def test_python3_plain():
|
||||
@@ -10,9 +13,15 @@ def test_python3_plain():
|
||||
|
||||
|
||||
def test_python3_json():
|
||||
code = """
|
||||
import json
|
||||
print(json.dumps({'Hello': 'World'}))
|
||||
"""
|
||||
code = dedent("""
|
||||
import json
|
||||
print(json.dumps({'Hello': 'World'}))
|
||||
""")
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
assert result == '{"Hello": "World"}\n'
|
||||
|
||||
|
||||
def test_python3_with_code_template():
|
||||
result = CodeExecutor.execute_workflow_code_template(
|
||||
language=CODE_LANGUAGE, code=PYTHON_DEFAULT_CODE, inputs={'arg1': 'Hello', 'arg2': 'World'})
|
||||
assert result == {'result': 'HelloWorld'}
|
||||
|
||||
Reference in New Issue
Block a user