test_services.py•2.86 kB
"""
Test script to demonstrate the new service layer functionality.
This can be used for manual testing and verification.
"""
from query_repository import get_query_repository
from sql_service import QueryFormatter
def test_query_repository():
    """Test that the query repository returns the expected queries."""
    repo = get_query_repository()
    
    # Test that queries exist with the correct CO-XX-XX format
    test_keys = [
        "CO-01-01-table-formats",
        "CO-01-01-managed-tables", 
        "CO-01-02",
        "CO-01-03",
        "CO-01-04",
        "CO-01-06-serverless",
        "CO-01-06-sql",
        "CO-01-08",
        "CO-02-01",
        "CO-02-02",
        "CO-03-01",
        "CO-03-02-tagging",
        "CO-03-02-popular"
    ]
    
    print("Testing Query Repository:")
    print("=" * 50)
    
    for key in test_keys:
        query = repo.get_query(key)
        if query:
            print(f"✓ {key}: Found query ({len(query.strip())} chars)")
        else:
            print(f"✗ {key}: Query not found!")
    
    print(f"\nTotal queries in repository: {len(repo.list_queries())}")
    print("Query keys:", list(repo.list_queries().keys()))
def test_query_formatter():
    """Test the query formatter functions."""
    print("\nTesting Query Formatter:")
    print("=" * 50)
    
    # Test with mock data
    mock_table_data = [("DELTA", 150), ("PARQUET", 75), ("JSON", 25)]
    result = QueryFormatter.format_table_formats(mock_table_data)
    print("Table formats formatter test:")
    print(result)
    
    mock_percentage_data = [("MANAGED", 85), ("EXTERNAL", 15)]
    result = QueryFormatter.format_table_types_distribution(mock_percentage_data)
    print("\nTable types distribution formatter test:")
    print(result)
def demonstrate_service_usage():
    """Show how the new service layer simplifies function calls."""
    print("\nService Usage Demonstration:")
    print("=" * 50)
    
    repo = get_query_repository()
    
    print("Before (old approach):")
    print("- 30+ lines of boilerplate SQL connection code")
    print("- Inline query strings mixed with connection logic")
    print("- Repetitive error handling")
    print("- Manual result formatting")
    
    print("\nAfter (new service layer):")
    print("def COST_OPTIMISATION_C0_01_01_TABLE_TYPES():")
    print("    query = query_repo.get_query('CO-01-01-table-formats')")
    print("    return sql_service.execute_query_with_formatting(query, QueryFormatter.format_table_formats)")
    print()
    print("Benefits:")
    print("- 3 lines instead of 30+")
    print("- Clean separation of concerns")
    print("- Centralized query management")
    print("- Reusable formatters")
    print("- Easy to test and maintain")
if __name__ == "__main__":
    test_query_repository()
    test_query_formatter()
    demonstrate_service_usage()