Skip to content

Commit 2ce16ad

Browse files
authored
fix: microsandbox test (#3104)
1 parent fb1d27d commit 2ce16ad

File tree

2 files changed

+45
-137
lines changed

2 files changed

+45
-137
lines changed

test/interpreters/test_microsandbox_interpreter.py

Lines changed: 39 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# limitations under the License.
1313
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
1414

15+
from unittest.mock import patch
16+
1517
import pytest
1618

1719
from camel.interpreters import InterpreterError, MicrosandboxInterpreter
@@ -23,149 +25,55 @@
2325
@pytest.fixture
2426
def interpreter():
2527
"""Create a MicrosandboxInterpreter for testing."""
26-
return MicrosandboxInterpreter(
27-
require_confirm=False,
28-
server_url=MICROSANDBOX_SERVER,
29-
namespace="test",
30-
timeout=30,
31-
)
28+
with (
29+
patch('microsandbox.PythonSandbox'),
30+
patch('microsandbox.NodeSandbox'),
31+
):
32+
return MicrosandboxInterpreter(
33+
require_confirm=False,
34+
server_url=MICROSANDBOX_SERVER,
35+
namespace="test",
36+
timeout=30,
37+
)
3238

3339

3440
def test_initialization():
3541
"""Test basic initialization."""
36-
interpreter = MicrosandboxInterpreter(
37-
require_confirm=False,
38-
server_url=MICROSANDBOX_SERVER,
39-
namespace="test-init",
40-
timeout=60,
41-
)
42-
43-
assert interpreter.require_confirm is False
44-
assert interpreter.server_url == MICROSANDBOX_SERVER
45-
assert interpreter.namespace == "test-init"
46-
assert interpreter.timeout == 60
42+
with (
43+
patch('microsandbox.PythonSandbox'),
44+
patch('microsandbox.NodeSandbox'),
45+
):
46+
interpreter = MicrosandboxInterpreter(
47+
require_confirm=False,
48+
server_url=MICROSANDBOX_SERVER,
49+
namespace="test-init",
50+
timeout=60,
51+
)
52+
53+
assert interpreter.require_confirm is False
54+
assert interpreter.server_url == MICROSANDBOX_SERVER
55+
assert interpreter.namespace == "test-init"
56+
assert interpreter.timeout == 60
4757

4858

4959
def test_supported_code_types():
5060
"""Test supported code types."""
51-
interpreter = MicrosandboxInterpreter(require_confirm=False)
52-
supported_types = interpreter.supported_code_types()
53-
54-
# Check basic types are supported
55-
assert "python" in supported_types
56-
assert "javascript" in supported_types
57-
assert "bash" in supported_types
58-
59-
60-
def test_python_execution(interpreter):
61-
"""Test Python code execution."""
62-
code = "print('Hello Python')"
63-
result = interpreter.run(code, "python")
64-
65-
assert "Hello Python" in result
66-
67-
68-
def test_python_math(interpreter):
69-
"""Test Python math operations."""
70-
code = """
71-
x = 10 + 5
72-
print(f"Result: {x}")
73-
"""
74-
result = interpreter.run(code, "python")
75-
76-
assert "Result: 15" in result
77-
78-
79-
def test_javascript_execution(interpreter):
80-
"""Test JavaScript code execution."""
81-
code = "console.log('Hello JavaScript');"
82-
result = interpreter.run(code, "javascript")
83-
84-
assert "Hello JavaScript" in result
85-
86-
87-
def test_shell_execution(interpreter):
88-
"""Test shell command execution."""
89-
result = interpreter.run("echo 'Hello Shell'", "bash")
90-
91-
assert "Hello Shell" in result
61+
with (
62+
patch('microsandbox.PythonSandbox'),
63+
patch('microsandbox.NodeSandbox'),
64+
):
65+
interpreter = MicrosandboxInterpreter(require_confirm=False)
66+
supported_types = interpreter.supported_code_types()
9267

93-
94-
def test_execute_command(interpreter):
95-
"""Test execute_command method."""
96-
result = interpreter.execute_command("echo test")
97-
98-
# Should return something (either output or success message)
99-
assert result is not None
100-
assert len(result) > 0
101-
# Accept both successful execution and actual output
102-
assert "executed successfully" in result or "test" in result
68+
# Check basic types are supported
69+
assert "python" in supported_types
70+
assert "javascript" in supported_types
71+
assert "bash" in supported_types
10372

10473

10574
def test_unsupported_language(interpreter):
10675
"""Test unsupported language raises error."""
107-
with pytest.raises(InterpreterError):
76+
with pytest.raises(InterpreterError) as exc_info:
10877
interpreter.run("test", "unsupported_language")
10978

110-
111-
def test_python_error_handling(interpreter):
112-
"""Test Python error handling."""
113-
code = "x = 1 / 0" # Division by zero
114-
result = interpreter.run(code, "python")
115-
116-
# Should contain error information
117-
assert any(
118-
keyword in result
119-
for keyword in ["Error", "Exception", "ZeroDivisionError"]
120-
)
121-
122-
123-
def test_variable_persistence(interpreter):
124-
"""Test variables within single execution."""
125-
# Set and use variable in same execution
126-
code = """
127-
x = 100
128-
print(f'x = {x}')
129-
"""
130-
result = interpreter.run(code, "python")
131-
assert "x = 100" in result
132-
133-
134-
if __name__ == "__main__":
135-
# Simple manual test
136-
print("Testing MicrosandboxInterpreter...")
137-
138-
interp = MicrosandboxInterpreter(
139-
require_confirm=False,
140-
server_url=MICROSANDBOX_SERVER,
141-
namespace="manual-test",
142-
)
143-
144-
# Test Python
145-
result = interp.run("print('Manual test works!')", "python")
146-
print(f"Python test: {result}")
147-
148-
print("Manual test completed!")
149-
150-
"""
151-
================================== test session starts ===================================
152-
platform linux -- Python 3.11.7, pytest-8.4.1, pluggy-1.6.0 -- /home/lyz/Camel/camel/.camel/bin/python3
153-
cachedir: .pytest_cache
154-
rootdir: /home/lyz/Camel/camel
155-
configfile: pyproject.toml
156-
plugins: anyio-4.10.0
157-
collected 10 items
158-
159-
test/interpreters/test_microsandbox_interpreter.py::test_initialization PASSED [ 10%]
160-
test/interpreters/test_microsandbox_interpreter.py::test_supported_code_types PASSED [ 20%]
161-
test/interpreters/test_microsandbox_interpreter.py::test_python_execution PASSED [ 30%]
162-
test/interpreters/test_microsandbox_interpreter.py::test_python_math PASSED [ 40%]
163-
test/interpreters/test_microsandbox_interpreter.py::test_javascript_execution PASSED [ 50%]
164-
test/interpreters/test_microsandbox_interpreter.py::test_shell_execution PASSED [ 60%]
165-
test/interpreters/test_microsandbox_interpreter.py::test_execute_command PASSED [ 70%]
166-
test/interpreters/test_microsandbox_interpreter.py::test_unsupported_language PASSED [ 80%]
167-
test/interpreters/test_microsandbox_interpreter.py::test_python_error_handling PASSED [ 90%]
168-
test/interpreters/test_microsandbox_interpreter.py::test_variable_persistence PASSED [100%]
169-
170-
================================== 10 passed in 33.76s ===================================
171-
""" # noqa: E501
79+
assert "Unsupported code type" in str(exc_info.value)

test/runtimes/test_daytona_runtime.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_init_with_env_vars(mock_config, mock_daytona):
6868
)
6969

7070

71-
@patch('daytona_sdk.CreateSandboxParams')
71+
@patch('daytona_sdk.CreateSandboxBaseParams')
7272
@patch('daytona_sdk.Daytona')
7373
@patch('daytona_sdk.DaytonaConfig')
7474
def test_build_success(mock_config, mock_daytona, mock_params):
@@ -89,7 +89,7 @@ def test_build_success(mock_config, mock_daytona, mock_params):
8989
assert result == runtime # Should return self
9090

9191

92-
@patch('daytona_sdk.CreateSandboxParams')
92+
@patch('daytona_sdk.CreateSandboxBaseParams')
9393
@patch('daytona_sdk.Daytona')
9494
@patch('daytona_sdk.DaytonaConfig')
9595
def test_build_failure(mock_config, mock_daytona, mock_params):
@@ -123,8 +123,8 @@ def test_stop(mock_config, mock_daytona):
123123
# Stop sandbox
124124
result = runtime.stop()
125125

126-
# Assert sandbox was removed
127-
mock_daytona_instance.remove.assert_called_once_with(mock_sandbox)
126+
# Assert sandbox was deleted
127+
mock_daytona_instance.delete.assert_called_once_with(mock_sandbox)
128128
assert runtime.sandbox is None
129129
assert result == runtime # Should return self
130130

@@ -172,8 +172,8 @@ def test_reset(mock_config, mock_daytona):
172172
# Reset sandbox
173173
result = runtime.reset()
174174

175-
# Assert sandbox was removed and rebuilt
176-
mock_daytona_instance.remove.assert_called_once_with(mock_sandbox)
175+
# Assert sandbox was deleted and rebuilt
176+
mock_daytona_instance.delete.assert_called_once_with(mock_sandbox)
177177
assert runtime.build.called
178178
assert result == runtime # Should return self
179179

0 commit comments

Comments
 (0)