mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-09 10:56:52 +08:00
test: add integration tests on CodeExecutor with the sandbox service (#4015)
This commit is contained in:
0
api/tests/integration_tests/tools/code/__init__.py
Normal file
0
api/tests/integration_tests/tools/code/__init__.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from core.helper.code_executor.code_executor import CodeExecutor
|
||||
|
||||
CODE_LANGUAGE = 'javascript'
|
||||
|
||||
|
||||
def test_javascript_plain():
|
||||
code = 'console.log("Hello World")'
|
||||
result_message = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
assert result_message == 'Hello World\n'
|
||||
|
||||
|
||||
def test_javascript_json():
|
||||
code = """
|
||||
obj = {'Hello': 'World'}
|
||||
console.log(JSON.stringify(obj))
|
||||
"""
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
assert result == '{"Hello":"World"}\n'
|
||||
@@ -0,0 +1,14 @@
|
||||
import base64
|
||||
|
||||
from core.helper.code_executor.code_executor import CodeExecutor
|
||||
from core.helper.code_executor.jinja2_transformer import JINJA2_PRELOAD, PYTHON_RUNNER
|
||||
|
||||
CODE_LANGUAGE = 'jinja2'
|
||||
|
||||
|
||||
def test_jinja2():
|
||||
template = 'Hello {{template}}'
|
||||
inputs = base64.b64encode(b'{"template": "World"}').decode('utf-8')
|
||||
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'
|
||||
@@ -0,0 +1,18 @@
|
||||
from core.helper.code_executor.code_executor import CodeExecutor
|
||||
|
||||
CODE_LANGUAGE = 'python3'
|
||||
|
||||
|
||||
def test_python3_plain():
|
||||
code = 'print("Hello World")'
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
assert result == 'Hello World\n'
|
||||
|
||||
|
||||
def test_python3_json():
|
||||
code = """
|
||||
import json
|
||||
print(json.dumps({'Hello': 'World'}))
|
||||
"""
|
||||
result = CodeExecutor.execute_code(language=CODE_LANGUAGE, preload='', code=code)
|
||||
assert result == '{"Hello": "World"}\n'
|
||||
Reference in New Issue
Block a user