#!/usr/bin/env python3 """ Test script for the Croissant Converter Gradio app. """ import json import tempfile import os from pathlib import Path # Test the converter functions def test_converter(): print("Testing Croissant Converter...") try: from croissant_converter.converter import convert_content, detect_format # Test JSON-LD to TOML conversion jsonld_content = '''{ "@context": { "@vocab": "https://schema.org/", "cr": "https://mlcommons.org/croissant/" }, "@type": "sc:Dataset", "name": "Test Dataset", "description": "A test dataset", "version": "1.0.0" }''' print("1. Testing JSON-LD to TOML conversion...") toml_result, output_format = convert_content(jsonld_content, "toml") print("✅ JSON-LD to TOML conversion successful") # Test TOML to JSON-LD conversion print("\n2. Testing TOML to JSON-LD conversion...") jsonld_result, output_format = convert_content(toml_result, "jsonld") print("✅ TOML to JSON-LD conversion successful") # Test format detection print("\n3. Testing format detection...") detected_jsonld = detect_format(jsonld_content) detected_toml = detect_format(toml_result) print(f"✅ Format detection: JSON-LD={detected_jsonld}, TOML={detected_toml}") # Test auto-conversion print("\n4. Testing auto-conversion...") auto_result, auto_format = convert_content(jsonld_content) print(f"✅ Auto-conversion: {detect_format(jsonld_content)} → {auto_format}") print("\n🎉 All converter tests passed!") return True except Exception as e: print(f"❌ Converter test failed: {e}") import traceback traceback.print_exc() return False def test_gradio_functions(): print("\nTesting Gradio interface functions...") try: # Import the app functions import sys sys.path.append('.') # Test content creation functions from app import create_example_content, create_example_toml print("1. Testing example content creation...") jsonld_example = create_example_content() toml_example = create_example_toml() assert len(jsonld_example) > 0, "JSON-LD example should not be empty" assert len(toml_example) > 0, "TOML example should not be empty" assert '"@context"' in jsonld_example, "JSON-LD should contain @context" assert '[metadata]' in toml_example, "TOML should contain metadata section" print("✅ Example content creation works") # Test processing functions (mock file uploads) print("\n2. Testing content processing...") from app import process_pasted_content status, result = process_pasted_content(jsonld_example, "auto") assert "successful" in status.lower(), f"Processing should succeed: {status}" assert len(result) > 0, "Result should not be empty" print("✅ Content processing works") print("\n🎉 All Gradio function tests passed!") return True except Exception as e: print(f"❌ Gradio function test failed: {e}") import traceback traceback.print_exc() return False def main(): print("Croissant Converter Test Suite") print("=" * 40) success = True # Test converter success &= test_converter() # Test Gradio functions success &= test_gradio_functions() print("\n" + "=" * 40) if success: print("✅ All tests passed! The Gradio app is ready to deploy.") print("\nTo run the app locally:") print(" python app.py") print("\nTo deploy to Hugging Face Spaces:") print(" 1. Create a new Space on huggingface.co") print(" 2. Upload all files to the Space repository") print(" 3. The app will automatically deploy") else: print("❌ Some tests failed. Please check the errors above.") if __name__ == "__main__": main()