burtenshaw's picture
burtenshaw HF Staff
Publish procedural SQL investigation tasks and reproducible environment source
2ce79be verified
Raw History Blame Contribute Delete
2.52 kB
"""Check reference SQL against independently generated Python answers."""
import json
import pytest
from sql_investigation.tasks import TASK_IDS, build_task
def _assert_same_rows(actual, expected):
assert len(actual) == len(expected)
for actual_row, expected_row in zip(sorted(actual), sorted(expected)):
assert len(actual_row) == len(expected_row)
for actual_value, expected_value in zip(actual_row, expected_row):
if isinstance(expected_value, float):
assert actual_value == pytest.approx(expected_value, abs=1e-7)
else:
assert actual_value == expected_value
@pytest.mark.parametrize("task_id", TASK_IDS)
@pytest.mark.parametrize("seed", [*range(10), 737])
def test_oracle_matches_independent_python_answer(task_id, seed):
task = build_task(task_id, seed)
try:
result = task.connection.execute(task.oracle_sql).fetchall()
_assert_same_rows(result, task.expected_rows)
assert 1 <= len(result) <= 20
assert all(isinstance(row, list) for row in task.expected_rows)
json.dumps(task.expected_rows, allow_nan=False)
tables = task.connection.execute(
"SELECT name FROM sqlite_master WHERE type = 'table'"
).fetchall()
assert tables
assert not any(
"answer" in name or "oracle" in name or "expected" in name for (name,) in tables
)
total_rows = sum(
task.connection.execute(f'SELECT COUNT(*) FROM "{name}"').fetchone()[0]
for (name,) in tables
)
assert 30 <= total_rows <= 200
finally:
task.connection.close()
@pytest.mark.parametrize("task_id", TASK_IDS)
def test_tasks_are_reproducible_and_seed_sensitive(task_id):
first = build_task(task_id, 42)
same = build_task(task_id, 42)
other = build_task(task_id, 43)
try:
assert first.instruction == same.instruction
assert first.schema == same.schema
assert first.expected_rows == same.expected_rows
assert list(first.connection.iterdump()) == list(same.connection.iterdump())
assert first.expected_rows != other.expected_rows
assert list(first.connection.iterdump()) != list(other.connection.iterdump())
finally:
first.connection.close()
same.connection.close()
other.connection.close()
def test_unknown_task_is_rejected():
with pytest.raises(ValueError, match="Unknown task_id"):
build_task("not_a_task", 0)