#![allow(dead_code)]
use assert_cmd::Command;
use rusqlite::Connection;
use tempfile::TempDir;
#[path = "../common/mod.rs"]
pub mod common;
pub fn sgr_cmd() -> Command {
let mock_dir = common::mock_llm_path();
let mut c = Command::cargo_bin("sqlite-graphrag").expect("sqlite-graphrag binary not found");
c.env("PATH", common::prepend_path(&mock_dir));
c
}
pub fn sgr_on(tmp: &TempDir, db_path: &std::path::Path) -> Command {
let mut c = sgr_cmd();
common::plant_db_path(&tmp.path().join("config"), db_path);
c.env("HOME", tmp.path().join("home"))
.env("XDG_CACHE_HOME", tmp.path().join("xdg_cache"))
.env("XDG_CONFIG_HOME", tmp.path().join("xdg_config"))
.env("XDG_DATA_HOME", tmp.path().join("xdg_data"))
.env("XDG_RUNTIME_DIR", tmp.path().join("xdg_runtime"))
.arg("--config-dir")
.arg(tmp.path().join("config"))
.arg("--embedding-model")
.arg(common::openrouter_mock::STUB_MODEL)
.arg("--cache-dir")
.arg(tmp.path().join("cache"))
.arg("--use-active")
.arg("--skip-memory-guard");
c
}
pub fn init_isolated_db() -> (TempDir, std::path::PathBuf) {
let tmp = TempDir::new().expect("TempDir must be created");
let db_path = tmp.path().join("test.sqlite");
sgr_on(&tmp, &db_path).args(["init"]).assert().success();
(tmp, db_path)
}
pub fn conn_ro(db_path: &std::path::Path) -> Connection {
Connection::open(db_path).expect("database connection must work")
}
pub fn table_exists(conn: &Connection, name: &str) -> bool {
let count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type IN ('table','view') AND name = ?1",
rusqlite::params![name],
|row| row.get(0),
)
.unwrap_or(0);
count > 0
}
pub fn trigger_exists(conn: &Connection, name: &str) -> bool {
let count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'trigger' AND name = ?1",
rusqlite::params![name],
|row| row.get(0),
)
.unwrap_or(0);
count > 0
}
pub fn index_exists(conn: &Connection, name: &str) -> bool {
let count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name = ?1",
rusqlite::params![name],
|row| row.get(0),
)
.unwrap_or(0);
count > 0
}