File size: 8,426 Bytes
bc39556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad5be9b
bc39556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d88f966
bc39556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad5be9b
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
End-to-end flow tests for phi3-mini-sql-generator demo.
Run with: python tests/e2e_flow_test.py

Model must be loaded first. Call app.load_model(app.FINE_TUNED_MODEL_ID)
before running these tests.
"""

import app
import types

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def sql_out(result):
    return result[4]

def status(result):
    return result[6]

def reset_model_state():
    app._model = None
    app._tokenizer = None
    app._current_model_id = None


def check_sql(result, expected_fragments, description):
    """Print and assert SQL output checks."""
    sql = sql_out(result)
    status_msg = status(result)
    ok = True
    for frag in expected_fragments:
        if frag not in sql:
            print(f"  FAIL: missing '{frag}' in output")
            ok = False
    if ok:
        print(f"  OK: {description}")
        print(f"  SQL: {sql[:200]}")
    return ok


# ---------------------------------------------------------------------------
# Scenario 1: Parser still works (no model call)
# ---------------------------------------------------------------------------

def test_scenario1_parser_keeps_working():
    print("\n=== Scenario 1: Parser — accented columns ===")
    result = app.generate_response(
        "criar tabela animal com nome nome cientifico e especie",
        [], "", None, None
    )
    fragments = ["CREATE TABLE animal", "nome TEXT", "cientifico TEXT", "especie TEXT"]
    return check_sql(result, fragments, "3 columns from Portuguese message")


# ---------------------------------------------------------------------------
# Scenario 2: SELECT all
# ---------------------------------------------------------------------------

def test_scenario2_select_all():
    print("\n=== Scenario 2: SELECT all rows ===")
    schema = app.PRESETS["employees"]
    result = app.generate_response(
        "liste todos os funcionarios",
        [], schema, app.FINE_TUNED_MODEL_KEY, None
    )
    sql = sql_out(result)
    status_msg = status(result)
    ok = True
    if "SELECT" not in sql.upper():
        print(f"  FAIL: no SELECT in output")
        ok = False
    if "FROM" not in sql.upper():
        print(f"  FAIL: no FROM in output")
        ok = False
    if ok:
        print(f"  OK: generated SELECT")
        print(f"  SQL: {sql}")
    return ok


# ---------------------------------------------------------------------------
# Scenario 3: SELECT with WHERE filter
# ---------------------------------------------------------------------------

def test_scenario3_select_with_filter():
    print("\n=== Scenario 3: SELECT with WHERE ===")
    schema = app.PRESETS["employees"]
    result = app.generate_response(
        "mostre os funcionarios do departamento de vendas",
        [], schema, app.FINE_TUNED_MODEL_KEY, None
    )
    sql = sql_out(result)
    ok = True
    if "SELECT" not in sql.upper():
        print(f"  FAIL: no SELECT")
        ok = False
    if "WHERE" not in sql.upper():
        print(f"  FAIL: no WHERE")
        ok = False
    if "department" in sql.lower() or "vendas" in sql.lower():
        print(f"  OK: WHERE clause present")
        print(f"  SQL: {sql}")
    else:
        print(f"  FAIL: filter condition missing")
        ok = False
    return ok


# ---------------------------------------------------------------------------
# Scenario 4: Aggregate (COUNT, AVG, GROUP BY)
# ---------------------------------------------------------------------------

def test_scenario4_aggregates():
    print("\n=== Scenario 4: Aggregate query ===")
    schema = app.PRESETS["employees"]
    result = app.generate_response(
        "qual a media de salarios por departamento",
        [], schema, app.FINE_TUNED_MODEL_KEY, None
    )
    sql = sql_out(result)
    ok = True
    checks = ["SELECT", "AVG", "GROUP BY"]
    for c in checks:
        if c not in sql.upper():
            print(f"  FAIL: missing '{c}'")
            ok = False
    if ok:
        print(f"  OK: aggregate query generated")
        print(f"  SQL: {sql}")
    return ok


# ---------------------------------------------------------------------------
# Scenario 5: Natural language SQL (Issue 3)
# ---------------------------------------------------------------------------

def test_scenario5_natural_language():
    print("\n=== Scenario 5: Natural language SQL (Issue 3) ===")
    schema = app.PRESETS["products"]
    result = app.generate_response(
        "what is the most expensive product",
        [], schema, app.FINE_TUNED_MODEL_KEY, None
    )
    sql = sql_out(result)
    status_msg = status(result)
    ok = True
    if not sql.strip():
        print(f"  FAIL: no SQL generated — model returned: {status_msg[:100]}")
        ok = False
    elif "SELECT" not in sql.upper():
        print(f"  FAIL: output is not SQL: {sql[:100]}")
        ok = False
    else:
        print(f"  OK: natural language produced SQL")
        print(f"  SQL: {sql}")
    return ok


# ---------------------------------------------------------------------------
# Scenario 6: Multi-turn flow (create → add → remove → query)
# ---------------------------------------------------------------------------

def test_scenario6_multiturn_flow():
    print("\n=== Scenario 6: Multi-turn schema build + query ===")
    ok = True

    # Step 1: Create table
    r1 = app.generate_response(
        "crie tabela vendas com id produto quantidade total",
        [], "", None, None
    )
    if not check_sql(r1, ["CREATE TABLE vendas", "id INTEGER", "produto TEXT", "quantidade INTEGER", "total NUMERIC"], "Step 1: CREATE TABLE"):
        ok = False

    # Step 2: Add column
    r2 = app.generate_response("adicione desconto", r1[0], "", None, None)
    if not check_sql(r2, ["desconto NUMERIC", "CREATE TABLE vendas"], "Step 2: ADD COLUMN"):
        ok = False

    # Step 3: Remove column
    r3 = app.generate_response("remova quantidade", r2[0], "", None, None)
    sql3 = sql_out(r3)
    # CORRECT: quantidade should NOT be in SQL (it was removed)
    if "quantidade" in sql3:
        print(f"  FAIL: 'quantidade' still in table after remove (regression)")
        ok = False
    else:
        print(f"  OK: Step 3: REMOVE COLUMN - 'quantidade' removed")
    # Verify remaining columns still exist
    for col in ["id", "produto", "desconto", "total"]:
        if col not in sql3:
            print(f"  FAIL: column '{col}' missing after remove")
            ok = False

    # Step 4: Query (model call)
    final_schema = sql_out(r3)
    r4 = app.generate_response(
        "quanto vendemos no total",
        r3[0], final_schema, app.FINE_TUNED_MODEL_KEY, None
    )
    sql4 = sql_out(r4)
    if "SELECT" not in sql4.upper():
        print(f"  FAIL: Step 4 no SELECT generated. Status: {status(r4)[:100]}")
        ok = False
    else:
        print(f"  OK: Step 4: model generated SQL from multi-turn context")
        print(f"  SQL: {sql4}")

    return ok


# ---------------------------------------------------------------------------
# Run all
# ---------------------------------------------------------------------------

def run_all():
    if app._model is None:
        print("ERROR: model not loaded. Run app.load_model(app.FINE_TUNED_MODEL_ID) first.")
        return

    results = {}
    results["s1_parser"] = test_scenario1_parser_keeps_working()
    results["s2_select_all"] = test_scenario2_select_all()
    results["s3_where"] = test_scenario3_select_with_filter()
    results["s4_aggregates"] = test_scenario4_aggregates()
    results["s5_natlang"] = test_scenario5_natural_language()
    results["s6_multiturn"] = test_scenario6_multiturn_flow()

    print("\n" + "=" * 50)
    print("SUMMARY")
    print("=" * 50)
    passed = sum(1 for v in results.values() if v)
    total = len(results)
    for name, result in results.items():
        mark = "PASS" if result else "FAIL"
        print(f"  {mark}  {name}")
    print(f"\n  Total: {passed}/{total} passed")

    return passed == total


if __name__ == "__main__":
    # Check model loaded
    if app._model is None:
        print("Model not loaded. Call app.load_model(app.FINE_TUNED_MODEL_ID) then re-run.")
        print("From python: python -c \"import app; app.load_model(app.FINE_TUNED_MODEL_ID); exec(open('tests/e2e_flow_test.py').read())\"")
    else:
        run_all()