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

108 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""
Test script for JSON to CSV Converter
Verifies the converter works with actual troupe standup data.
"""
import json
import csv
import subprocess
import sys
from pathlib import Path
def test_conversion():
"""Test JSON to CSV conversion with real data."""
success = True
errors = []
# Test file path
input_json = "/root/troupe-standup/inbox/seed-2026-09-26.json"
output_csv = "/root/troupe-standup/wizard-work/spark/test_output.csv"
script_path = "/root/.hermes/wizard-outputs/spark/json_to_csv_converter.py"
# Run the converter
result = subprocess.run(
["python3", script_path, input_json, output_csv],
capture_output=True,
text=True
)
if result.returncode != 0:
errors.append(f"Conversion failed with code {result.returncode}")
errors.append(f"stderr: {result.stderr}")
success = False
else:
# Verify output exists
if not Path(output_csv).exists():
errors.append("Output CSV not created")
success = False
else:
# Read and verify CSV
with open(output_csv, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
rows = list(reader)
if len(rows) < 2: # Header + at least 1 data row
errors.append(f"CSV has insufficient rows: {len(rows)}")
success = False
else:
header = rows[0]
# Check that flattened fields exist (cutover, decided, pending)
found_fields = []
for h in header:
if 'cutover' in h or 'decided' in h or 'pending' in h:
found_fields.append(h)
if len(found_fields) < 3:
errors.append(f"Missing expected flattened fields in header: {header}")
success = False
# Verify with JSON schema structure
try:
with open(input_json, 'r', encoding='utf-8') as f:
data = json.load(f)
# Verify JSON has expected structure
if 'decided' not in data:
errors.append("Input JSON missing 'decided' field")
success = False
if 'pending' not in data:
errors.append("Input JSON missing 'pending' field")
success = False
except Exception as e:
errors.append(f"Failed to load input JSON: {e}")
success = False
# Report results
print("=" * 60)
print("JSON to CSV Converter Test Results")
print("=" * 60)
if success:
print("STATUS: PASSED")
print(f"Input: {input_json}")
print(f"Output: {output_csv}")
# Show sample output
with open(output_csv, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
rows = list(reader)
print(f"\nCSV Rows: {len(rows)}")
print(f"Columns: {len(rows[0]) if rows else 0}")
print(f"Sample header fields: {rows[0][:8] if rows else []}")
else:
print("STATUS: FAILED")
for error in errors:
print(f" - {error}")
print("=" * 60)
return success
if __name__ == '__main__':
success = test_conversion()
sys.exit(0 if success else 1)