Spaces:
Sleeping
Sleeping
File size: 6,877 Bytes
ba5110e |
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 |
"""
Test cases for Code Executor tool.
Tests sandbox execution, SymPy integration, and correction loop.
"""
import pytest
from backend.tools.code_executor import execute_python_code
class TestCodeExecutor:
"""Test suite for code executor sandbox."""
# ==================== BASIC EXECUTION TESTS ====================
def test_simple_print(self):
"""TC-CE-001: Test basic print statement."""
success, result = execute_python_code('print("Hello World")')
assert success is True
assert "Hello World" in result
def test_arithmetic_calculation(self):
"""TC-CE-002: Test basic arithmetic."""
success, result = execute_python_code('print(2 + 3 * 4)')
assert success is True
assert "14" in result
def test_variable_assignment(self):
"""TC-CE-003: Test variable assignment and output."""
code = """
x = 10
y = 20
print(x + y)
"""
success, result = execute_python_code(code)
assert success is True
assert "30" in result
# ==================== SYMPY ALGEBRA TESTS ====================
def test_solve_quadratic(self):
"""TC-CE-004: Solve quadratic equation x² - 5x + 6 = 0."""
code = 'x = symbols("x"); print(solve(x**2 - 5*x + 6, x))'
success, result = execute_python_code(code)
assert success is True
assert "2" in result and "3" in result
def test_solve_linear_system(self):
"""TC-CE-005: Solve system of linear equations."""
code = """
x, y = symbols('x y')
eqs = [x + y - 5, x - y - 1]
solution = solve(eqs, [x, y])
print(solution)
"""
success, result = execute_python_code(code)
assert success is True
assert "3" in result # x = 3
assert "2" in result # y = 2
def test_matrix_operations(self):
"""TC-CE-006: Test matrix operations."""
code = """
A = Matrix([[1, 2], [3, 4]])
print("Determinant:", A.det())
print("Inverse exists:", A.inv() is not None)
"""
success, result = execute_python_code(code)
assert success is True
assert "-2" in result # det = 1*4 - 2*3 = -2
def test_differentiation(self):
"""TC-CE-007: Test calculus - differentiation."""
code = """
x = symbols('x')
f = x**3 + 2*x**2 - x + 1
derivative = diff(f, x)
print(derivative)
"""
success, result = execute_python_code(code)
assert success is True
assert "3*x**2" in result or "3x²" in result.replace(" ", "")
def test_integration(self):
"""TC-CE-008: Test calculus - integration."""
code = """
x = symbols('x')
f = 2*x + 1
integral = integrate(f, x)
print(integral)
"""
success, result = execute_python_code(code)
assert success is True
assert "x**2" in result or "x²" in result
def test_simplify_expression(self):
"""TC-CE-009: Test expression simplification."""
code = """
x = symbols('x')
expr = (x**2 - 1)/(x - 1)
simplified = simplify(expr)
print(simplified)
"""
success, result = execute_python_code(code)
assert success is True
assert "x + 1" in result
def test_factor_polynomial(self):
"""TC-CE-010: Test polynomial factorization."""
code = """
x = symbols('x')
poly = x**2 - 4
factored = factor(poly)
print(factored)
"""
success, result = execute_python_code(code)
assert success is True
assert "(x - 2)" in result and "(x + 2)" in result
# ==================== IMPORT STRIPPING TESTS ====================
def test_import_stripping(self):
"""TC-CE-011: Import statements should be stripped (pre-loaded)."""
code = """
from sympy import symbols, solve
x = symbols('x')
print(solve(x - 5, x))
"""
success, result = execute_python_code(code)
assert success is True
assert "5" in result
# ==================== ERROR HANDLING TESTS ====================
def test_syntax_error(self):
"""TC-CE-012: Test syntax error handling."""
success, result = execute_python_code('print("unclosed string')
assert success is False
assert "error" in result.lower() or "Error" in result
def test_runtime_error(self):
"""TC-CE-013: Test runtime error handling."""
success, result = execute_python_code('print(1/0)')
assert success is False
assert "ZeroDivision" in result or "error" in result.lower()
def test_undefined_variable(self):
"""TC-CE-014: Test undefined variable error."""
success, result = execute_python_code('print(undefined_var)')
assert success is False
assert "error" in result.lower()
# ==================== SECURITY TESTS ====================
def test_no_file_access(self):
"""TC-CE-015: File operations should be blocked."""
success, result = execute_python_code('open("/etc/passwd")')
assert success is False
def test_no_os_module(self):
"""TC-CE-016: OS module should not be available for system commands."""
# os.system is not available in sandbox (os not in safe_globals)
success, result = execute_python_code('os.system("ls")')
assert success is False
assert "error" in result.lower() or "os" in result.lower()
# ==================== LATEX OUTPUT TESTS ====================
def test_latex_output(self):
"""TC-CE-017: Test LaTeX output generation."""
code = """
x = symbols('x')
expr = x**2 + 2*x + 1
print(latex(expr))
"""
success, result = execute_python_code(code)
assert success is True
assert "x^{2}" in result or "x**2" in result
class TestCodeExecutorAdvanced:
"""Advanced algebra test cases."""
def test_group_theory_cyclic(self):
"""TC-CE-018: Test group operations (mod arithmetic)."""
code = """
# Check if Z_5 under addition is cyclic
# Generator test: 1 generates all elements
elements = [(1 * i) % 5 for i in range(5)]
print("Generated elements:", set(elements))
print("Is cyclic:", len(set(elements)) == 5)
"""
success, result = execute_python_code(code)
assert success is True
assert "Is cyclic: True" in result
def test_eigenvalues(self):
"""TC-CE-019: Test eigenvalue computation."""
code = """
A = Matrix([[4, 1], [2, 3]])
eigenvals = A.eigenvals()
print("Eigenvalues:", eigenvals)
"""
success, result = execute_python_code(code)
assert success is True
assert "5" in result or "2" in result
def test_gcd_lcm(self):
"""TC-CE-020: Test GCD and LCM functions."""
code = """
print("GCD(12, 18):", gcd(12, 18))
print("LCM(4, 6):", lcm(4, 6))
"""
success, result = execute_python_code(code)
assert success is True
assert "6" in result # GCD = 6
assert "12" in result # LCM = 12
|