File size: 22,087 Bytes
5dd1bb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
"""Tests for synthetic database schema introspection utilities."""

from __future__ import annotations

import json
import sqlite3
from pathlib import Path

import pytest

from sql_env.server.synthetic.generate import (
    VariantResult,
    generate_variant,
    generate_variants_for_question,
)
from sql_env.server.synthetic.__main__ import main as synthetic_cli_main
from sql_env.server.synthetic.mutations import (
    MutationResult,
    TableSchema,
    detect_bridge_tables,
    duplicate_bridge_rows,
    get_table_schemas,
    inject_irrelevant_rows,
    remap_ids,
)
from sql_env.server.synthetic.validate import validate_gold_sql


def _sqlite_table_definitions(db_path: Path) -> list[tuple[str, str]]:
    with sqlite3.connect(db_path) as connection:
        cursor = connection.cursor()
        cursor.execute(
            "SELECT name, sql FROM sqlite_master "
            "WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name"
        )
        return [(str(row[0]), str(row[1])) for row in cursor.fetchall()]


def _find_real_spider_case() -> tuple[Path, str, str] | None:
    repo_root = Path(__file__).resolve().parents[1]
    question_files = [
        repo_root / "data" / "questions" / "questions_train.json",
        repo_root / "data" / "questions" / "questions_eval.json",
    ]

    for question_file in question_files:
        if not question_file.exists():
            continue

        questions = json.loads(question_file.read_text(encoding="utf-8"))
        for question in questions:
            db_name = question.get("database_name")
            gold_sql = question.get("gold_sql")
            if not isinstance(db_name, str) or not isinstance(gold_sql, str):
                continue

            db_path = repo_root / "data" / "databases" / db_name / f"{db_name}.sqlite"
            if not db_path.exists():
                continue

            try:
                is_valid, _ = validate_gold_sql(str(db_path), gold_sql)
            except sqlite3.OperationalError:
                continue

            if is_valid:
                return db_path, gold_sql, db_name

    return None


@pytest.fixture
def sample_db_path(tmp_path):
    db_path = tmp_path / "sample.sqlite"
    with sqlite3.connect(db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("PRAGMA foreign_keys = ON")

        cursor.execute(
            "CREATE TABLE departments (id INTEGER PRIMARY KEY, name TEXT NOT NULL)"
        )
        cursor.execute(
            "CREATE TABLE employees ("
            "id INTEGER PRIMARY KEY,"
            "name TEXT NOT NULL,"
            "department_id INTEGER,"
            "FOREIGN KEY(department_id) REFERENCES departments(id)"
            ")"
        )
        cursor.execute(
            "CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT NOT NULL)"
        )
        cursor.execute(
            "CREATE TABLE courses (id INTEGER PRIMARY KEY, title TEXT NOT NULL)"
        )
        cursor.execute(
            "CREATE TABLE enrollments ("
            "student_id INTEGER,"
            "course_id INTEGER,"
            "PRIMARY KEY(student_id, course_id),"
            "FOREIGN KEY(student_id) REFERENCES students(id),"
            "FOREIGN KEY(course_id) REFERENCES courses(id)"
            ")"
        )
        cursor.execute("CREATE TABLE audit_log (event TEXT, created_at TEXT)")

        cursor.execute(
            "INSERT INTO departments (id, name) VALUES (1, 'Engineering'), (2, 'Sales')"
        )
        cursor.execute(
            "INSERT INTO employees (id, name, department_id) VALUES "
            "(1, 'Alice', 1), (2, 'Bob', 2)"
        )
        cursor.execute(
            "INSERT INTO students (id, name) VALUES (1, 'Sam'), (2, 'Riley')"
        )
        cursor.execute(
            "INSERT INTO courses (id, title) VALUES (1, 'Math'), (2, 'Science')"
        )
        cursor.execute(
            "INSERT INTO enrollments (student_id, course_id) VALUES (1, 1), (2, 2)"
        )
        cursor.execute(
            "INSERT INTO audit_log (event, created_at) "
            "VALUES ('seed', '2026-03-27T00:00:00Z')"
        )

        connection.commit()

    return str(db_path)


def test_table_schema_dataclass_fields():
    schema = TableSchema(
        name="enrollments",
        columns=["student_id", "course_id"],
        pk_columns=["student_id", "course_id"],
        fk_columns=[("student_id", "students", "id")],
    )

    assert schema.name == "enrollments"
    assert schema.columns == ["student_id", "course_id"]
    assert schema.pk_columns == ["student_id", "course_id"]
    assert schema.fk_columns == [("student_id", "students", "id")]


def test_mutation_result_dataclass_fields():
    result = MutationResult(
        mutation_name="inject_irrelevant_rows",
        tables_affected=["employees"],
        rows_added=5,
        success=True,
    )

    assert result.mutation_name == "inject_irrelevant_rows"
    assert result.tables_affected == ["employees"]
    assert result.rows_added == 5
    assert result.success is True


def test_get_table_schemas_multi_table_with_fk_and_composite_pk(sample_db_path):
    schemas = get_table_schemas(sample_db_path)
    by_name = {schema.name: schema for schema in schemas}

    assert set(by_name) == {
        "audit_log",
        "courses",
        "departments",
        "employees",
        "enrollments",
        "students",
    }
    assert by_name["departments"].pk_columns == ["id"]
    assert by_name["employees"].fk_columns == [("department_id", "departments", "id")]
    assert by_name["enrollments"].pk_columns == ["student_id", "course_id"]
    assert set(by_name["enrollments"].fk_columns) == {
        ("student_id", "students", "id"),
        ("course_id", "courses", "id"),
    }


def test_get_table_schemas_no_pk_table(sample_db_path):
    schemas = get_table_schemas(sample_db_path)
    by_name = {schema.name: schema for schema in schemas}

    assert by_name["audit_log"].pk_columns == []


def test_get_table_schemas_empty_db(tmp_path):
    db_path = tmp_path / "empty.sqlite"
    sqlite3.connect(db_path).close()

    assert get_table_schemas(str(db_path)) == []


def test_get_table_schemas_nonexistent_db_raises_operational_error(tmp_path):
    missing_path = tmp_path / "missing.sqlite"

    with pytest.raises(sqlite3.OperationalError):
        get_table_schemas(str(missing_path))


def test_detect_bridge_tables_identifies_tables_with_two_or_more_fks(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    assert detect_bridge_tables(schemas) == ["enrollments"]


def test_detect_bridge_tables_empty_when_no_bridge_tables():
    schemas = [
        TableSchema(
            name="employees",
            columns=["id", "department_id"],
            pk_columns=["id"],
            fk_columns=[("department_id", "departments", "id")],
        ),
        TableSchema(
            name="departments",
            columns=["id", "name"],
            pk_columns=["id"],
            fk_columns=[],
        ),
    ]

    assert detect_bridge_tables(schemas) == []


def test_inject_irrelevant_rows_adds_rows_with_new_primary_keys(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    result = inject_irrelevant_rows(sample_db_path, schemas, n_rows=2)

    assert result.mutation_name == "inject_irrelevant_rows"
    assert result.success is True
    assert result.rows_added == 8
    assert result.tables_affected == ["courses", "departments", "employees", "students"]

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT COUNT(*) FROM employees")
        assert cursor.fetchone()[0] == 4

        cursor.execute("SELECT MIN(id), MAX(id) FROM employees")
        assert cursor.fetchone() == (1, 4)


def test_inject_irrelevant_rows_preserves_existing_rows(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    inject_irrelevant_rows(sample_db_path, schemas, n_rows=1)

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT name FROM employees ORDER BY id")
        names = [row[0] for row in cursor.fetchall()]

    assert names[0:2] == ["Alice", "Bob"]


def test_inject_irrelevant_rows_zero_rows_no_change(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    result = inject_irrelevant_rows(sample_db_path, schemas, n_rows=0)

    assert result.rows_added == 0
    assert result.tables_affected == []
    assert result.success is True

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT COUNT(*) FROM employees")
        assert cursor.fetchone()[0] == 2


def test_remap_ids_basic_changes_integer_primary_keys(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT id FROM departments ORDER BY id")
        before_department_ids = [row[0] for row in cursor.fetchall()]

    result = remap_ids(sample_db_path, schemas)

    assert result.mutation_name == "remap_ids"
    assert result.success is True
    assert "departments" in result.tables_affected
    assert "employees" in result.tables_affected
    assert result.rows_added >= 2

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT id FROM departments ORDER BY id")
        after_department_ids = [row[0] for row in cursor.fetchall()]

    assert after_department_ids != before_department_ids
    assert len(after_department_ids) == len(before_department_ids)


def test_remap_ids_updates_foreign_keys_and_preserves_join(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    remap_ids(sample_db_path, schemas)

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()

        cursor.execute("PRAGMA foreign_key_check")
        assert cursor.fetchall() == []

        cursor.execute(
            "SELECT e.name, d.name FROM employees e "
            "JOIN departments d ON e.department_id = d.id "
            "ORDER BY e.name"
        )
        joined = cursor.fetchall()

    assert joined == [("Alice", "Engineering"), ("Bob", "Sales")]


def test_remap_ids_is_bijective_and_preserves_row_counts(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT COUNT(*), COUNT(DISTINCT id) FROM departments")
        before_counts = cursor.fetchone()

    remap_ids(sample_db_path, schemas)

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT COUNT(*), COUNT(DISTINCT id) FROM departments")
        after_counts = cursor.fetchone()

    assert after_counts == before_counts


def test_remap_ids_skips_tables_without_integer_primary_key(tmp_path):
    db_path = tmp_path / "text_pk.sqlite"
    with sqlite3.connect(db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("CREATE TABLE labels (name TEXT PRIMARY KEY, value TEXT)")
        cursor.execute("INSERT INTO labels (name, value) VALUES ('alpha', '1')")
        connection.commit()

    schemas = get_table_schemas(str(db_path))

    result = remap_ids(str(db_path), schemas)

    assert result.success is True
    assert result.rows_added == 0
    assert result.tables_affected == []


def test_duplicate_bridge_rows_adds_rows_for_bridge_tables(tmp_path):
    db_path = tmp_path / "bridge.sqlite"
    with sqlite3.connect(db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("PRAGMA foreign_keys = ON")
        cursor.execute("CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT)")
        cursor.execute("CREATE TABLE clubs (id INTEGER PRIMARY KEY, name TEXT)")
        cursor.execute(
            "CREATE TABLE club_memberships ("
            "student_id INTEGER,"
            "club_id INTEGER,"
            "FOREIGN KEY(student_id) REFERENCES students(id),"
            "FOREIGN KEY(club_id) REFERENCES clubs(id)"
            ")"
        )
        cursor.execute(
            "INSERT INTO students (id, name) VALUES (1, 'Sam'), (2, 'Riley')"
        )
        cursor.execute(
            "INSERT INTO clubs (id, name) VALUES (1, 'Chess'), (2, 'Robotics')"
        )
        cursor.execute(
            "INSERT INTO club_memberships (student_id, club_id) VALUES (1, 1), (2, 2)"
        )
        connection.commit()

    schemas = get_table_schemas(str(db_path))
    bridge_tables = detect_bridge_tables(schemas)

    result = duplicate_bridge_rows(str(db_path), schemas, bridge_tables)

    assert result.mutation_name == "duplicate_bridge_rows"
    assert result.success is True
    assert result.rows_added == 2
    assert result.tables_affected == ["club_memberships"]

    with sqlite3.connect(db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT COUNT(*) FROM club_memberships")
        assert cursor.fetchone()[0] == 4


def test_duplicate_bridge_rows_empty_bridge_tables_returns_noop(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    result = duplicate_bridge_rows(sample_db_path, schemas, [])

    assert result.success is True
    assert result.rows_added == 0
    assert result.tables_affected == []


def test_duplicate_bridge_rows_skips_rows_blocked_by_unique_constraints(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    result = duplicate_bridge_rows(sample_db_path, schemas, ["enrollments"])

    assert result.success is True
    assert result.rows_added == 0
    assert result.tables_affected == []

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT COUNT(*) FROM enrollments")
        assert cursor.fetchone()[0] == 2


def test_duplicate_bridge_rows_ignores_nonexistent_tables(sample_db_path):
    schemas = get_table_schemas(sample_db_path)

    result = duplicate_bridge_rows(sample_db_path, schemas, ["missing_bridge"])

    assert result.success is True
    assert result.rows_added == 0
    assert result.tables_affected == []


def test_validate_gold_sql_valid_returns_serialized_result(sample_db_path):
    is_valid, result = validate_gold_sql(
        sample_db_path,
        "SELECT name FROM employees ORDER BY id",
    )

    assert is_valid is True
    assert result == "[('Alice',), ('Bob',)]"


def test_validate_gold_sql_empty_result_returns_false(sample_db_path):
    is_valid, result = validate_gold_sql(
        sample_db_path,
        "SELECT name FROM employees WHERE id = -1",
    )

    assert is_valid is False
    assert result is None


def test_validate_gold_sql_invalid_query_raises_operational_error(sample_db_path):
    with pytest.raises(sqlite3.OperationalError):
        validate_gold_sql(sample_db_path, "SELECT * FROM definitely_missing_table")


def test_validate_gold_sql_honors_custom_timeout(sample_db_path):
    is_valid, result = validate_gold_sql(
        sample_db_path,
        "SELECT COUNT(*) FROM employees",
        timeout=0.001,
    )

    assert is_valid is True
    assert result == "[(2,)]"


def test_variant_result_dataclass_fields(sample_db_path):
    mutation = MutationResult(
        mutation_name="inject_irrelevant_rows",
        tables_affected=["employees"],
        rows_added=2,
        success=True,
    )
    result = VariantResult(
        variant_path="/tmp/sample_variant.sqlite",
        original_path=sample_db_path,
        mutations_applied=[mutation],
        gold_sql_valid=True,
        gold_answer="[(1,)]",
    )

    assert result.variant_path.endswith("sample_variant.sqlite")
    assert result.original_path == sample_db_path
    assert result.mutations_applied == [mutation]
    assert result.gold_sql_valid is True
    assert result.gold_answer == "[(1,)]"


def test_generate_variant_default_applies_all_mutations_and_creates_file(
    sample_db_path, tmp_path
):
    output_dir = tmp_path / "variants"

    result = generate_variant(
        db_path=sample_db_path,
        gold_sql="SELECT name FROM employees",
        output_dir=str(output_dir),
    )

    assert result.gold_sql_valid is True
    assert len(result.mutations_applied) == 3
    assert [m.mutation_name for m in result.mutations_applied] == [
        "inject_irrelevant_rows",
        "remap_ids",
        "duplicate_bridge_rows",
    ]
    assert (output_dir / "sample_variant_0.sqlite").exists()


def test_generate_variant_single_mutation(sample_db_path, tmp_path):
    output_dir = tmp_path / "variants"

    result = generate_variant(
        db_path=sample_db_path,
        gold_sql="SELECT name FROM employees",
        output_dir=str(output_dir),
        mutations=["inject_irrelevant_rows"],
    )

    assert result.gold_sql_valid is True
    assert [m.mutation_name for m in result.mutations_applied] == [
        "inject_irrelevant_rows"
    ]


def test_generate_variant_does_not_modify_original_db(sample_db_path, tmp_path):
    output_dir = tmp_path / "variants"

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT COUNT(*) FROM employees")
        before_count = cursor.fetchone()[0]

    generate_variant(
        db_path=sample_db_path,
        gold_sql="SELECT name FROM employees",
        output_dir=str(output_dir),
    )

    with sqlite3.connect(sample_db_path) as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT COUNT(*) FROM employees")
        after_count = cursor.fetchone()[0]

    assert before_count == after_count == 2


def test_generate_variant_raises_on_missing_db(tmp_path):
    with pytest.raises(FileNotFoundError):
        generate_variant(
            db_path=str(tmp_path / "missing.sqlite"),
            gold_sql="SELECT 1",
            output_dir=str(tmp_path / "variants"),
        )


def test_generate_variant_raises_on_unknown_mutation(sample_db_path, tmp_path):
    with pytest.raises(ValueError, match="Unknown mutation"):
        generate_variant(
            db_path=sample_db_path,
            gold_sql="SELECT name FROM employees",
            output_dir=str(tmp_path / "variants"),
            mutations=["unknown_mutation"],
        )


def test_generate_variant_invalid_gold_sql_discards_variant(sample_db_path, tmp_path):
    output_dir = tmp_path / "variants"

    result = generate_variant(
        db_path=sample_db_path,
        gold_sql="SELECT name FROM employees WHERE id = 1",
        output_dir=str(output_dir),
    )

    assert result.gold_sql_valid is False
    assert result.gold_answer is None
    assert not (output_dir / "sample_variant_0.sqlite").exists()


def test_generate_variant_uses_variant_id_in_filename(sample_db_path, tmp_path):
    output_dir = tmp_path / "variants"

    result = generate_variant(
        db_path=sample_db_path,
        gold_sql="SELECT name FROM employees",
        output_dir=str(output_dir),
        variant_id=7,
    )

    assert result.variant_path.endswith("sample_variant_7.sqlite")


def test_generate_variants_for_question_default_count(sample_db_path, tmp_path):
    output_dir = tmp_path / "variants"

    results = generate_variants_for_question(
        db_path=sample_db_path,
        gold_sql="SELECT name FROM employees",
        output_dir=str(output_dir),
        n_variants=2,
    )

    assert len(results) == 2
    assert all(result.gold_sql_valid for result in results)


def test_generate_variants_for_question_zero_returns_empty_list(
    sample_db_path, tmp_path
):
    output_dir = tmp_path / "variants"

    results = generate_variants_for_question(
        db_path=sample_db_path,
        gold_sql="SELECT name FROM employees",
        output_dir=str(output_dir),
        n_variants=0,
    )

    assert results == []


def test_generate_variants_for_question_returns_unique_paths(sample_db_path, tmp_path):
    output_dir = tmp_path / "variants"

    results = generate_variants_for_question(
        db_path=sample_db_path,
        gold_sql="SELECT name FROM employees",
        output_dir=str(output_dir),
        n_variants=3,
    )

    paths = [result.variant_path for result in results]
    assert len(paths) == len(set(paths))


def test_synthetic_cli_smoke_generates_variants(sample_db_path, tmp_path, capsys):
    output_dir = tmp_path / "variants_cli"

    exit_code = synthetic_cli_main(
        [
            "--db-path",
            sample_db_path,
            "--gold-sql",
            "SELECT name FROM employees",
            "--output-dir",
            str(output_dir),
            "--n-variants",
            "2",
        ]
    )

    captured = capsys.readouterr()
    assert exit_code == 0
    assert "Generated 2 valid variant(s)" in captured.out
    assert (output_dir / "sample_variant_0.sqlite").exists()
    assert (output_dir / "sample_variant_1.sqlite").exists()


@pytest.mark.slow
def test_generate_variants_integration_with_real_spider_database(tmp_path):
    case = _find_real_spider_case()
    if case is None:
        pytest.skip(
            "No local Spider DB + valid gold SQL pair found. "
            "Run: uv run python scripts/download_spider_databases.py"
        )

    db_path, gold_sql, db_name = case
    output_dir = tmp_path / "spider_variants" / db_name

    results = generate_variants_for_question(
        db_path=str(db_path),
        gold_sql=gold_sql,
        output_dir=str(output_dir),
        n_variants=2,
    )

    assert len(results) >= 1

    variant = results[0]
    variant_path = Path(variant.variant_path)
    assert variant_path.exists()
    assert variant.gold_sql_valid is True
    assert any(mutation.rows_added > 0 for mutation in variant.mutations_applied)

    original_schema = _sqlite_table_definitions(db_path)
    variant_schema = _sqlite_table_definitions(variant_path)
    assert variant_schema == original_schema