Spaces:
Paused
Paused
Commit ·
c8cb83c
1
Parent(s): 76c97db
test: add end-to-end SQL layer integration tests
Browse filesCo-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
tests/test_sql_integration.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tests/test_sql_integration.py
|
| 2 |
+
"""End-to-end integration test for the SQL layer."""
|
| 3 |
+
import json
|
| 4 |
+
import sqlite3
|
| 5 |
+
import pytest
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_sql_roundtrip(tmp_path):
|
| 9 |
+
"""Test: ingest CSV -> SQL -> retrieve -> format context."""
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from src.sql_ingest import ingest_to_sql
|
| 12 |
+
from src.sql_retriever import execute_sql_query, format_sql_results_as_context, build_schema_summary
|
| 13 |
+
|
| 14 |
+
# Create test data
|
| 15 |
+
kb_dir = tmp_path / "knowledge_base"
|
| 16 |
+
ds_dir = kb_dir / "pts"
|
| 17 |
+
ds_dir.mkdir(parents=True)
|
| 18 |
+
csv_file = ds_dir / "scores.csv"
|
| 19 |
+
csv_file.write_text(
|
| 20 |
+
"Country,Year,PTS_A\n"
|
| 21 |
+
"China,2000,4.0\n"
|
| 22 |
+
"China,2001,4.0\n"
|
| 23 |
+
"China,2002,3.5\n"
|
| 24 |
+
"India,2000,3.0\n"
|
| 25 |
+
"India,2001,3.0\n"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
sql_db_dir = tmp_path / "sql_db"
|
| 29 |
+
cfg = {"paths": {"sql_db": str(sql_db_dir)}, "sql": {"max_rows": 200}}
|
| 30 |
+
files = [(Path(csv_file), "pts")]
|
| 31 |
+
|
| 32 |
+
# Ingest
|
| 33 |
+
schema = ingest_to_sql(files, str(kb_dir), cfg)
|
| 34 |
+
assert len(schema) == 1
|
| 35 |
+
table_name = list(schema.keys())[0]
|
| 36 |
+
|
| 37 |
+
# Build schema summary
|
| 38 |
+
summary = build_schema_summary(schema)
|
| 39 |
+
assert "Country" in summary
|
| 40 |
+
assert "PTS_A" in summary
|
| 41 |
+
|
| 42 |
+
# Execute a query
|
| 43 |
+
sql = f'SELECT Country, Year, PTS_A FROM "{table_name}" WHERE Country = \'China\' ORDER BY Year'
|
| 44 |
+
rows = execute_sql_query(sql, cfg)
|
| 45 |
+
assert len(rows) == 3
|
| 46 |
+
assert all(r["Country"] == "China" for r in rows)
|
| 47 |
+
|
| 48 |
+
# Format as context
|
| 49 |
+
context = format_sql_results_as_context(rows, sql, schema[table_name]["source_file"])
|
| 50 |
+
assert "[CHUNK-SQL-001]" in context
|
| 51 |
+
assert "China" in context
|
| 52 |
+
assert "pts/scores.csv" in context
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def test_sql_aggregation(tmp_path):
|
| 56 |
+
"""Test: aggregation queries work."""
|
| 57 |
+
from pathlib import Path
|
| 58 |
+
from src.sql_ingest import ingest_to_sql
|
| 59 |
+
from src.sql_retriever import execute_sql_query
|
| 60 |
+
|
| 61 |
+
kb_dir = tmp_path / "knowledge_base"
|
| 62 |
+
ds_dir = kb_dir / "ds"
|
| 63 |
+
ds_dir.mkdir(parents=True)
|
| 64 |
+
csv_file = ds_dir / "data.csv"
|
| 65 |
+
csv_file.write_text("Country,Year,Score\nA,2000,1\nB,2000,2\nC,2000,3\n")
|
| 66 |
+
|
| 67 |
+
sql_db_dir = tmp_path / "sql_db"
|
| 68 |
+
cfg = {"paths": {"sql_db": str(sql_db_dir)}, "sql": {"max_rows": 200}}
|
| 69 |
+
files = [(Path(csv_file), "ds")]
|
| 70 |
+
schema = ingest_to_sql(files, str(kb_dir), cfg)
|
| 71 |
+
table_name = list(schema.keys())[0]
|
| 72 |
+
|
| 73 |
+
# Count
|
| 74 |
+
rows = execute_sql_query(f'SELECT COUNT(*) as cnt FROM "{table_name}"', cfg)
|
| 75 |
+
assert rows[0]["cnt"] == 3
|
| 76 |
+
|
| 77 |
+
# Average
|
| 78 |
+
rows = execute_sql_query(f'SELECT AVG(Score) as avg_score FROM "{table_name}"', cfg)
|
| 79 |
+
assert rows[0]["avg_score"] == 2.0
|