Spaces:
Sleeping
Sleeping
| import os | |
| import sys | |
| # Add root folder to sys.path so we can import sandbox and sql_playground | |
| sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| import sandbox | |
| import sql_playground | |
| def test_python_sandbox(): | |
| print("--- Testing Python Sandbox ---") | |
| # 1. Correct Submission Test | |
| correct_code = "def solve(a, b):\n return a + b" | |
| test_cases = [ | |
| {"input": [1, 2], "expected": 3}, | |
| {"input": [10, -5], "expected": 5} | |
| ] | |
| res_correct = sandbox.run_python_sandbox(correct_code, test_cases) | |
| print("Correct Code Result:") | |
| print("Success:", res_correct.get("success")) | |
| print("Sandbox Type:", res_correct.get("sandbox_type")) | |
| if res_correct.get("success"): | |
| for r in res_correct["results"]: | |
| print(f" Test {r['index'] + 1}: status={r['status']}, got={r['got']}") | |
| # 2. Logic Failure Test | |
| failed_code = "def solve(a, b):\n return a - b" | |
| res_failed = sandbox.run_python_sandbox(failed_code, test_cases) | |
| print("\nFailed Code Result:") | |
| print("Success:", res_failed.get("success")) | |
| if res_failed.get("success"): | |
| for r in res_failed["results"]: | |
| print(f" Test {r['index'] + 1}: status={r['status']}, got={r['got']}, expected={r['expected']}") | |
| def test_sql_playground(): | |
| print("\n--- Testing SQL Playground ---") | |
| user_id = "test_developer_123" | |
| challenge = { | |
| "title": "High Earners", | |
| "objective": "Find employees with salary greater than 85000", | |
| "ddl": "CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, department TEXT, salary INTEGER);", | |
| "inserts": "INSERT INTO employees VALUES (1, 'Alice', 'Engineering', 95000); INSERT INTO employees VALUES (2, 'Bob', 'HR', 75000); INSERT INTO employees VALUES (3, 'Charlie', 'Engineering', 100000);", | |
| "validation_query": "SELECT name FROM employees WHERE salary > 85000;", | |
| "challenge_type": "SELECT", | |
| "points": 150 | |
| } | |
| # 1. Init DB | |
| init_res = sql_playground.init_user_db(user_id, challenge) | |
| print("DB Init Result:", init_res) | |
| # 2. Run correct SELECT query | |
| correct_query = "SELECT name FROM employees WHERE salary > 85000" | |
| res_correct = sql_playground.run_user_query(user_id, correct_query, challenge) | |
| print("\nCorrect Query Result:") | |
| print("Success:", res_correct.get("success")) | |
| print("Is Correct:", res_correct.get("is_correct")) | |
| print("Columns:", res_correct.get("columns")) | |
| print("Rows:", res_correct.get("rows")) | |
| # 3. Run incorrect SELECT query | |
| incorrect_query = "SELECT name FROM employees" | |
| res_incorrect = sql_playground.run_user_query(user_id, incorrect_query, challenge) | |
| print("\nIncorrect Query Result:") | |
| print("Success:", res_incorrect.get("success")) | |
| print("Is Correct:", res_incorrect.get("is_correct")) | |
| # 4. Test safety check | |
| unsafe_query = "SELECT name FROM employees; DROP TABLE employees;" | |
| res_unsafe = sql_playground.run_user_query(user_id, unsafe_query, challenge) | |
| print("\nUnsafe Query Result:") | |
| print("Success:", res_unsafe.get("success")) | |
| print("Error:", res_unsafe.get("error")) | |
| if __name__ == '__main__': | |
| test_python_sandbox() | |
| test_sql_playground() | |