File size: 5,353 Bytes
0646b18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env python3
"""
Test for sandbox local execution with simple code

Tests basic functionality of running code in the local sandbox environment.
"""

from cuga.backend.tools_env.code_sandbox.sandbox import run_local, get_premable


def test_basic_code_execution():
    """Test basic Python code execution in sandbox"""
    code = """
print("Hello, World!")
result = 2 + 3
print(f"2 + 3 = {result}")
"""

    result = run_local(code)

    assert result.exit_code == 0
    assert "Hello, World!" in result.stdout
    assert "2 + 3 = 5" in result.stdout
    assert result.stderr == ""


def test_variable_assignment_and_print():
    """Test variable assignment and printing"""
    code = """
x = 42
y = "test"
z = [1, 2, 3]
print(f"x = {x}")
print(f"y = {y}")
print(f"z = {z}")
"""

    result = run_local(code)

    assert result.exit_code == 0
    assert "x = 42" in result.stdout
    assert "y = test" in result.stdout
    assert "z = [1, 2, 3]" in result.stdout
    assert result.stderr == ""


def test_function_definition_and_call():
    """Test function definition and execution"""
    code = """
def add_numbers(a, b):
    return a + b

result = add_numbers(10, 20)
print(f"10 + 20 = {result}")
"""

    result = run_local(code)

    assert result.exit_code == 0
    assert "10 + 20 = 30" in result.stdout
    assert result.stderr == ""


def test_exception_handling():
    """Test exception handling in executed code"""
    code = """
try:
    result = 1 / 0
except ZeroDivisionError as e:
    print(f"Caught exception: {e}")
finally:
    print("Finally block executed")
"""

    result = run_local(code)

    assert result.exit_code == 0
    assert "Caught exception: division by zero" in result.stdout
    assert "Finally block executed" in result.stdout
    assert result.stderr == ""


def test_import_and_usage():
    """Test importing and using standard library modules"""
    code = """
import json
import datetime

data = {"name": "test", "value": 123}
json_str = json.dumps(data)
print(f"JSON: {json_str}")

now = datetime.datetime.now()
print(f"Current time: {now}")
"""

    result = run_local(code)

    assert result.exit_code == 0
    assert "JSON:" in result.stdout
    assert '"name": "test"' in result.stdout
    assert '"value": 123' in result.stdout
    assert "Current time:" in result.stdout
    assert result.stderr == ""


def test_system_exit_handling():
    """Test handling of SystemExit calls"""
    code = """
print("About to exit")
exit(42)
print("This should not print")
"""

    result = run_local(code)

    assert result.exit_code == 42
    # Note: PythonREPL doesn't capture stdout before SystemExit due to internal redirection
    assert "This should not print" not in result.stdout
    assert "SystemExit: 42" in result.stderr


def test_call_api_function_definition():
    """Test that call_api function is properly defined in preamble"""
    preamble = get_premable(is_local=True)

    assert "def call_api(app_name, api_name, args=None):" in preamble
    assert "if args is None:" in preamble
    assert "args = {}" in preamble


def test_complex_data_structures():
    """Test working with complex data structures"""
    code = """
# Dictionary operations
user = {"name": "Alice", "age": 30, "active": True}
user["city"] = "New York"
print(f"User: {user}")

# List operations
numbers = [1, 2, 3, 4, 5]
squares = [x**2 for x in numbers]
print(f"Squares: {squares}")

# Set operations
unique_items = set([1, 2, 2, 3, 3, 3])
print(f"Unique items: {unique_items}")
"""

    result = run_local(code)

    assert result.exit_code == 0
    assert "User:" in result.stdout
    assert "Alice" in result.stdout
    assert "New York" in result.stdout
    assert "Squares:" in result.stdout
    assert "[1, 4, 9, 16, 25]" in result.stdout
    assert "Unique items:" in result.stdout
    assert "{1, 2, 3}" in result.stdout
    assert result.stderr == ""


def test_string_operations():
    """Test various string operations"""
    code = """
text = "Hello, World!"
print(f"Original: {text}")
print(f"Upper: {text.upper()}")
print(f"Lower: {text.lower()}")
print(f"Length: {len(text)}")
print(f"Split: {text.split(', ')}")

# String formatting
name = "Bob"
age = 25
formatted = f"Name: {name}, Age: {age}"
print(formatted)
"""

    result = run_local(code)

    assert result.exit_code == 0
    assert "Original: Hello, World!" in result.stdout
    assert "Upper: HELLO, WORLD!" in result.stdout
    assert "Lower: hello, world!" in result.stdout
    assert "Length: 13" in result.stdout
    assert "['Hello', 'World!']" in result.stdout
    assert "Name: Bob, Age: 25" in result.stdout
    assert result.stderr == ""


def test_control_flow():
    """Test control flow statements"""
    code = """
# If-elif-else
x = 10
if x < 5:
    result = "small"
elif x < 15:
    result = "medium"
else:
    result = "large"
print(f"Size: {result}")

# For loop
total = 0
for i in range(1, 6):
    total += i
print(f"Sum 1-5: {total}")

# While loop
count = 0
while count < 3:
    print(f"Count: {count}")
    count += 1
"""

    result = run_local(code)

    assert result.exit_code == 0
    assert "Size: medium" in result.stdout
    assert "Sum 1-5: 15" in result.stdout
    assert "Count: 0" in result.stdout
    assert "Count: 1" in result.stdout
    assert "Count: 2" in result.stdout
    assert result.stderr == ""