diff --git a/.github/workflows/api-tests.yml b/.github/workflows/api-tests.yml index 2f68cb816b..a0407de843 100644 --- a/.github/workflows/api-tests.yml +++ b/.github/workflows/api-tests.yml @@ -39,6 +39,14 @@ jobs: - name: Run Tool run: dev/pytest/pytest_tools.sh + - name: Set up Sandbox + uses: hoverkraft-tech/compose-action@v2.0.0 + with: + compose-file: | + docker/docker-compose.middleware.yaml + services: | + sandbox + - name: Run Workflow run: dev/pytest/pytest_workflow.sh diff --git a/api/pyproject.toml b/api/pyproject.toml index 0002c61436..1e9f53cdb3 100644 --- a/api/pyproject.toml +++ b/api/pyproject.toml @@ -55,4 +55,6 @@ HUGGINGFACE_TEXT_GEN_ENDPOINT_URL = "a" HUGGINGFACE_TEXT2TEXT_GEN_ENDPOINT_URL = "b" HUGGINGFACE_EMBEDDINGS_ENDPOINT_URL = "c" MOCK_SWITCH = "true" -CODE_MAX_STRING_LENGTH = "80000" \ No newline at end of file +CODE_MAX_STRING_LENGTH = "80000" +CODE_EXECUTION_ENDPOINT="http://127.0.0.1:8194" +CODE_EXECUTION_API_KEY="dify-sandbox" diff --git a/api/tests/integration_tests/tools/code/__init__.py b/api/tests/integration_tests/tools/code/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/api/tests/integration_tests/workflow/nodes/code_executor/__init__.py b/api/tests/integration_tests/workflow/nodes/code_executor/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/api/tests/integration_tests/workflow/nodes/code_executor/test_code_javascript.py b/api/tests/integration_tests/workflow/nodes/code_executor/test_code_javascript.py new file mode 100644 index 0000000000..c794ae8e4b --- /dev/null +++ b/api/tests/integration_tests/workflow/nodes/code_executor/test_code_javascript.py @@ -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' diff --git a/api/tests/integration_tests/workflow/nodes/code_executor/test_code_jina2.py b/api/tests/integration_tests/workflow/nodes/code_executor/test_code_jina2.py new file mode 100644 index 0000000000..aae3c7acec --- /dev/null +++ b/api/tests/integration_tests/workflow/nodes/code_executor/test_code_jina2.py @@ -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 == '<>Hello World<>\n' diff --git a/api/tests/integration_tests/workflow/nodes/code_executor/test_code_python3.py b/api/tests/integration_tests/workflow/nodes/code_executor/test_code_python3.py new file mode 100644 index 0000000000..1983bc5e6b --- /dev/null +++ b/api/tests/integration_tests/workflow/nodes/code_executor/test_code_python3.py @@ -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'