|
12 | 12 | # limitations under the License. |
13 | 13 | # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. ========= |
14 | 14 |
|
| 15 | +from unittest.mock import patch |
| 16 | + |
15 | 17 | import pytest |
16 | 18 |
|
17 | 19 | from camel.interpreters import InterpreterError, MicrosandboxInterpreter |
|
23 | 25 | @pytest.fixture |
24 | 26 | def interpreter(): |
25 | 27 | """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 | + ) |
32 | 38 |
|
33 | 39 |
|
34 | 40 | def test_initialization(): |
35 | 41 | """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 |
47 | 57 |
|
48 | 58 |
|
49 | 59 | def test_supported_code_types(): |
50 | 60 | """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() |
92 | 67 |
|
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 |
103 | 72 |
|
104 | 73 |
|
105 | 74 | def test_unsupported_language(interpreter): |
106 | 75 | """Test unsupported language raises error.""" |
107 | | - with pytest.raises(InterpreterError): |
| 76 | + with pytest.raises(InterpreterError) as exc_info: |
108 | 77 | interpreter.run("test", "unsupported_language") |
109 | 78 |
|
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) |
0 commit comments