68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
"""L1: `tht session set-schema-linking` — stdin/file JSON → validate → write."""
|
|
import json
|
|
|
|
from typer.testing import CliRunner
|
|
|
|
from tht.cli.session_cmd import session_app
|
|
from tht.config import DatabaseConfig
|
|
from tht.session.store import create_session
|
|
|
|
|
|
def _db():
|
|
return DatabaseConfig(database="testdb", user="u", password="p", schema="public")
|
|
|
|
|
|
def _patch_cfg(monkeypatch, 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())
|
|
|
|
|
|
_VALID = json.dumps({
|
|
"question": "q",
|
|
"candidates": [{"kind": "table", "name": "fact_x", "decision": "promoted"}],
|
|
})
|
|
|
|
|
|
def test_cli_stdin_writes(tmp_path, monkeypatch):
|
|
m = create_session("q", _db(), tmp_path)
|
|
_patch_cfg(monkeypatch, tmp_path)
|
|
res = CliRunner().invoke(session_app, ["set-schema-linking", m.id, "--file", "-"], input=_VALID)
|
|
assert res.exit_code == 0, res.output
|
|
assert (tmp_path / m.id / "schema_linking.json").exists()
|
|
|
|
|
|
def test_cli_file_writes(tmp_path, monkeypatch):
|
|
m = create_session("q", _db(), tmp_path)
|
|
p = tmp_path / "sl.json"
|
|
p.write_text(_VALID)
|
|
_patch_cfg(monkeypatch, tmp_path)
|
|
res = CliRunner().invoke(session_app, ["set-schema-linking", m.id, "--file", str(p)])
|
|
assert res.exit_code == 0, res.output
|
|
assert (tmp_path / m.id / "schema_linking.json").exists()
|
|
|
|
|
|
def test_cli_invalid_json_exit5(tmp_path, monkeypatch):
|
|
m = create_session("q", _db(), tmp_path)
|
|
_patch_cfg(monkeypatch, tmp_path)
|
|
res = CliRunner().invoke(session_app, ["set-schema-linking", m.id, "--file", "-"], input="{not json")
|
|
assert res.exit_code == 5
|
|
assert not (tmp_path / m.id / "schema_linking.json").exists()
|
|
|
|
|
|
def test_cli_invalid_model_exit5(tmp_path, monkeypatch):
|
|
m = create_session("q", _db(), tmp_path)
|
|
_patch_cfg(monkeypatch, tmp_path)
|
|
res = CliRunner().invoke(
|
|
session_app, ["set-schema-linking", m.id, "--file", "-"], input='{"question":"q","bogus":1}'
|
|
)
|
|
assert res.exit_code == 5
|
|
assert not (tmp_path / m.id / "schema_linking.json").exists()
|