Files
troupe-standup/wizard-work/spark/test_json_validator.py
T

86 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Test suite for json_validate_standalone.py"""
import json
import sys
import subprocess
import tempfile
from pathlib import Path
def run_validator(files):
"""Run validator and return (exit_code, output)."""
cmd = ['python3', '/root/.hermes/wizard-outputs/spark/json_validate_standalone.py'] + files
result = subprocess.run(cmd, capture_output=True, text=True)
return result.returncode, result.stdout
def test_valid_json():
"""Test that valid JSON returns exit code 0."""
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
json.dump({"test": "data"}, f)
f.flush()
exit_code, output = run_validator([f.name])
Path(f.name).unlink()
assert exit_code == 0, f"Expected 0, got {exit_code}"
assert "VALID:" in output, f"Expected 'VALID:' in output"
return True
def test_invalid_json():
"""Test that invalid JSON returns exit code 1."""
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
f.write('{invalid json')
f.flush()
exit_code, output = run_validator([f.name])
Path(f.name).unlink()
assert exit_code == 1, f"Expected 1, got {exit_code}"
assert "INVALID:" in output, f"Expected 'INVALID:' in output"
return True
def test_nonexistent_file():
"""Test handling of nonexistent file."""
exit_code, output = run_validator(['/nonexistent/file.json'])
assert exit_code == 1, f"Expected 1 for nonexistent file"
assert "File not found" in output, f"Expected 'File not found' in output"
return True
def test_multiple_files_mixed():
"""Test with multiple files, some valid some invalid."""
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f1:
json.dump({"a": 1}, f1)
f1.flush()
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f2:
f2.write('{bad}')
f2.flush()
exit_code, output = run_validator([f1.name, f2.name])
Path(f1.name).unlink()
Path(f2.name).unlink()
assert exit_code == 1, "Should fail due to invalid file"
assert "VALID:" in output and "INVALID:" in output
return True
def main():
tests = [
("Valid JSON", test_valid_json),
("Invalid JSON", test_invalid_json),
("Nonexistent file", test_nonexistent_file),
("Multiple files mixed", test_multiple_files_mixed),
]
passed = 0
failed = 0
for name, test_fn in tests:
try:
test_fn()
print(f"PASS: {name}")
passed += 1
except AssertionError as e:
print(f"FAIL: {name} - {e}")
failed += 1
except Exception as e:
print(f"ERROR: {name} - {e}")
failed += 1
print(f"\nResults: {passed} passed, {failed} failed")
return 0 if failed == 0 else 1
if __name__ == "__main__":
sys.exit(main())