File size: 6,035 Bytes
acd8e16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
Test script to verify the NL→SQL Leaderboard system works correctly.
"""

import os
import sys
import time

# Add src to path for imports
sys.path.append('src')

from evaluator import evaluator, DatasetManager
from models_registry import models_registry
from scoring import scoring_engine


def test_dataset_discovery():
    """Test that datasets are discovered correctly."""
    print("Testing dataset discovery...")
    dataset_manager = DatasetManager()
    datasets = dataset_manager.get_datasets()
    print(f"Found datasets: {list(datasets.keys())}")
    
    if "nyc_taxi_small" in datasets:
        print("βœ“ NYC Taxi dataset found")
        return True
    else:
        print("βœ— NYC Taxi dataset not found")
        return False


def test_models_loading():
    """Test that models are loaded correctly."""
    print("\nTesting models loading...")
    models = models_registry.get_models()
    print(f"Found models: {[model.name for model in models]}")
    
    if len(models) > 0:
        print("βœ“ Models loaded successfully")
        return True
    else:
        print("βœ— No models found")
        return False


def test_database_creation():
    """Test database creation for NYC Taxi dataset."""
    print("\nTesting database creation...")
    try:
        dataset_manager = DatasetManager()
        db_path = dataset_manager.create_database("nyc_taxi_small")
        
        if os.path.exists(db_path):
            print("βœ“ Database created successfully")
            # Clean up
            os.remove(db_path)
            return True
        else:
            print("βœ— Database file not created")
            return False
    except Exception as e:
        print(f"βœ— Database creation failed: {e}")
        return False


def test_cases_loading():
    """Test loading test cases."""
    print("\nTesting cases loading...")
    try:
        dataset_manager = DatasetManager()
        cases = dataset_manager.load_cases("nyc_taxi_small")
        print(f"Found {len(cases)} test cases")
        
        if len(cases) > 0:
            print("βœ“ Test cases loaded successfully")
            return True
        else:
            print("βœ— No test cases found")
            return False
    except Exception as e:
        print(f"βœ— Cases loading failed: {e}")
        return False


def test_prompt_templates():
    """Test that prompt templates exist."""
    print("\nTesting prompt templates...")
    dialects = ["presto", "bigquery", "snowflake"]
    all_exist = True
    
    for dialect in dialects:
        template_path = f"prompts/template_{dialect}.txt"
        if os.path.exists(template_path):
            print(f"βœ“ {dialect} template found")
        else:
            print(f"βœ— {dialect} template not found")
            all_exist = False
    
    return all_exist


def test_scoring_engine():
    """Test the scoring engine."""
    print("\nTesting scoring engine...")
    try:
        from scoring import Metrics
        
        # Test with sample metrics
        metrics = Metrics(
            correctness_exact=1.0,
            result_match_f1=0.8,
            exec_success=1.0,
            latency_ms=100.0,
            readability=0.9,
            dialect_ok=1.0
        )
        
        score = scoring_engine.compute_composite_score(metrics)
        print(f"βœ“ Composite score computed: {score}")
        
        if 0.0 <= score <= 1.0:
            print("βœ“ Score is in valid range")
            return True
        else:
            print("βœ— Score is out of valid range")
            return False
    except Exception as e:
        print(f"βœ— Scoring engine test failed: {e}")
        return False


def test_sql_execution():
    """Test SQL execution with DuckDB."""
    print("\nTesting SQL execution...")
    try:
        import duckdb
        
        # Create a simple test database
        conn = duckdb.connect(":memory:")
        conn.execute("CREATE TABLE test (id INTEGER, name VARCHAR(10))")
        conn.execute("INSERT INTO test VALUES (1, 'Alice'), (2, 'Bob')")
        
        # Test query
        result = conn.execute("SELECT COUNT(*) FROM test").fetchdf()
        print(f"βœ“ SQL execution successful: {result.iloc[0, 0]} rows")
        
        conn.close()
        return True
    except Exception as e:
        print(f"βœ— SQL execution failed: {e}")
        return False


def test_sqlglot_transpilation():
    """Test SQL transpilation with sqlglot."""
    print("\nTesting SQL transpilation...")
    try:
        import sqlglot
        
        # Test simple query
        sql = "SELECT COUNT(*) FROM trips"
        parsed = sqlglot.parse_one(sql)
        
        # Transpile to different dialects
        dialects = ["presto", "bigquery", "snowflake"]
        for dialect in dialects:
            transpiled = parsed.sql(dialect=dialect)
            print(f"βœ“ {dialect} transpilation: {transpiled}")
        
        return True
    except Exception as e:
        print(f"βœ— SQL transpilation failed: {e}")
        return False


def main():
    """Run all tests."""
    print("NL→SQL Leaderboard System Test")
    print("=" * 40)
    
    tests = [
        test_dataset_discovery,
        test_models_loading,
        test_database_creation,
        test_cases_loading,
        test_prompt_templates,
        test_scoring_engine,
        test_sql_execution,
        test_sqlglot_transpilation
    ]
    
    passed = 0
    total = len(tests)
    
    for test in tests:
        try:
            if test():
                passed += 1
        except Exception as e:
            print(f"βœ— Test {test.__name__} failed with exception: {e}")
    
    print("\n" + "=" * 40)
    print(f"Test Results: {passed}/{total} tests passed")
    
    if passed == total:
        print("πŸŽ‰ All tests passed! The system is ready to use.")
        return True
    else:
        print("❌ Some tests failed. Please check the issues above.")
        return False


if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)