58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
"""Tests for `tht session documents --json` and build_documents."""
|
|
import json
|
|
|
|
from typer.testing import CliRunner
|
|
|
|
from tht.config import DatabaseConfig
|
|
from tht.session.store import build_documents, create_session
|
|
from tht.cli.session_cmd import session_app
|
|
|
|
|
|
def _db():
|
|
return DatabaseConfig(
|
|
database="testdb", user="u", password="p", # noqa: S106
|
|
**{"schema": "public"},
|
|
)
|
|
|
|
|
|
def test_build_documents_always_has_original_question(tmp_path):
|
|
m = create_session("quante ablazioni nel 2024", _db(), tmp_path)
|
|
docs = build_documents(m, tmp_path / m.id)
|
|
keys = [d["key"] for d in docs]
|
|
assert keys[0] == "question"
|
|
assert docs[0]["format"] == "text"
|
|
assert docs[0]["content"] == "quante ablazioni nel 2024"
|
|
# question.md is written by create_session -> revised_question present
|
|
assert "revised_question" in keys
|
|
|
|
|
|
def test_build_documents_includes_existing_artifacts_only(tmp_path):
|
|
m = create_session("q", _db(), tmp_path)
|
|
sdir = tmp_path / m.id
|
|
(sdir / "sql_final.sql").write_text("SELECT 1")
|
|
(sdir / "schema_linking.json").write_text('{"question":"q","candidates":[]}')
|
|
docs = {d["key"]: d for d in build_documents(m, sdir)}
|
|
assert docs["sql"]["format"] == "sql"
|
|
assert docs["sql"]["content"] == "SELECT 1"
|
|
assert docs["schema_linking"]["format"] == "schema-linking"
|
|
assert "validation_report" not in docs # not written
|
|
|
|
|
|
def test_cli_documents_json(tmp_path, monkeypatch):
|
|
m = create_session("q", _db(), tmp_path)
|
|
from tht.cli import session_cmd
|
|
|
|
class FakePaths:
|
|
sessions = tmp_path
|
|
|
|
class FakeCfg:
|
|
paths = FakePaths()
|
|
database = _db()
|
|
|
|
monkeypatch.setattr(session_cmd, "_load_config_or_exit", lambda _: FakeCfg())
|
|
res = CliRunner().invoke(session_app, ["documents", m.id, "--json"])
|
|
assert res.exit_code == 0, res.output
|
|
docs = json.loads(res.output)
|
|
assert isinstance(docs, list)
|
|
assert docs[0]["key"] == "question"
|