92 lines
3.5 KiB
Python
92 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Test for json_validate_simple.py - verifies the validator works correctly."""
|
|
|
|
import subprocess
|
|
import tempfile
|
|
import os
|
|
import json
|
|
|
|
def test_json_validator():
|
|
"""Run tests on json_validate_simple.py"""
|
|
results = []
|
|
|
|
# Create temp files for testing
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
# Test 1: Valid JSON object
|
|
valid_obj = os.path.join(temp_dir, "valid_obj.json")
|
|
with open(valid_obj, 'w') as f:
|
|
json.dump({"key": "value", "num": 42}, f)
|
|
|
|
result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', valid_obj],
|
|
capture_output=True, text=True)
|
|
if result.returncode == 0 and 'VALID' in result.stdout:
|
|
results.append(("Test 1 (valid JSON object)", "PASS"))
|
|
else:
|
|
results.append(("Test 1 (valid JSON object)", f"FAIL: stdout={result.stdout}, stderr={result.stderr}, code={result.returncode}"))
|
|
|
|
# Test 2: Valid JSON array
|
|
valid_arr = os.path.join(temp_dir, "valid_arr.json")
|
|
with open(valid_arr, 'w') as f:
|
|
json.dump([1, 2, 3], f)
|
|
|
|
result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', valid_arr],
|
|
capture_output=True, text=True)
|
|
if result.returncode == 0 and 'VALID' in result.stdout:
|
|
results.append(("Test 2 (valid JSON array)", "PASS"))
|
|
else:
|
|
results.append(("Test 2 (valid JSON array)", f"FAIL: {result.stdout} {result.stderr}"))
|
|
|
|
# Test 3: Invalid JSON (missing comma)
|
|
invalid_json = os.path.join(temp_dir, "invalid.json")
|
|
with open(invalid_json, 'w') as f:
|
|
f.write('{"key": "value" "num": 42}')
|
|
|
|
result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', invalid_json],
|
|
capture_output=True, text=True)
|
|
if result.returncode != 0 and 'Invalid JSON' in result.stderr:
|
|
results.append(("Test 3 (invalid JSON)", "PASS"))
|
|
else:
|
|
results.append(("Test 3 (invalid JSON)", f"FAIL: should have stderr with 'Invalid JSON'"))
|
|
|
|
# Test 4: Nonexistent file
|
|
result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', '/nonexistent.json'],
|
|
capture_output=True, text=True)
|
|
if result.returncode != 0:
|
|
results.append(("Test 4 (nonexistent file)", "PASS"))
|
|
else:
|
|
results.append(("Test 4 (nonexistent file)", "FAIL: should have exited nonzero"))
|
|
|
|
# Test 5: Empty string (valid JSON null)
|
|
empty_valid = os.path.join(temp_dir, "empty_val.json")
|
|
with open(empty_valid, 'w') as f:
|
|
f.write("null")
|
|
|
|
result = subprocess.run(['python3', '/root/.hermes/wizard-outputs/spark/json_validate_simple.py', empty_valid],
|
|
capture_output=True, text=True)
|
|
if result.returncode == 0 and 'VALID' in result.stdout:
|
|
results.append(("Test 5 (null JSON)", "PASS"))
|
|
else:
|
|
results.append(("Test 5 (null JSON)", f"FAIL: {result.stdout} {result.stderr}"))
|
|
|
|
# Cleanup
|
|
for f in os.listdir(temp_dir):
|
|
os.unlink(os.path.join(temp_dir, f))
|
|
os.rmdir(temp_dir)
|
|
|
|
return results
|
|
|
|
if __name__ == "__main__":
|
|
results = test_json_validator()
|
|
all_pass = True
|
|
for name, status in results:
|
|
print(f"{status}: {name}")
|
|
if status != "PASS":
|
|
all_pass = False
|
|
|
|
if all_pass:
|
|
print("\nALL TESTS PASSED")
|
|
exit(0)
|
|
else:
|
|
print("\nSOME TESTS FAILED")
|
|
exit(1) |