#![cfg(feature = "std")]
use graphitesql::{Connection, Value};
fn one(c: &Connection, sql: &str) -> Value {
let r = c.query(sql).unwrap();
assert_eq!(r.rows.len(), 1, "{sql} should return exactly one row");
r.rows[0][0].clone()
}
#[test]
fn tuning_pragma_getters_report_sqlite_defaults() {
let c = Connection::open_memory().unwrap();
let int_cases: &[(&str, i64)] = &[
("cache_size", -2000),
("synchronous", 2),
("temp_store", 0),
("secure_delete", 0),
("read_uncommitted", 0),
("cell_size_check", 0),
("checkpoint_fullfsync", 0),
("fullfsync", 0),
("busy_timeout", 0),
("wal_autocheckpoint", 1000),
("max_page_count", 4294967294),
];
for (name, want) in int_cases {
assert_eq!(
one(&c, &alloc_pragma(name)),
Value::Integer(*want),
"PRAGMA {name}"
);
}
assert_eq!(one(&c, "PRAGMA locking_mode"), Value::Text("normal".into()));
assert_eq!(one(&c, "PRAGMA journal_mode"), Value::Text("memory".into()));
}
#[test]
fn setting_an_unexposed_tuning_pragma_is_accepted_and_ignored() {
let mut c = Connection::open_memory().unwrap();
for s in [
"PRAGMA cache_size=8000",
"PRAGMA synchronous=OFF",
"PRAGMA temp_store=MEMORY",
"PRAGMA busy_timeout=5000",
"PRAGMA locking_mode=EXCLUSIVE",
"PRAGMA secure_delete=ON",
] {
c.execute(s)
.unwrap_or_else(|e| panic!("{s} should be accepted: {e:?}"));
}
c.execute("CREATE TABLE t(a)").unwrap();
c.execute("INSERT INTO t VALUES(1)").unwrap();
assert_eq!(one(&c, "SELECT a FROM t"), Value::Integer(1));
}
fn alloc_pragma(name: &str) -> String {
format!("PRAGMA {name}")
}
#[test]
fn legacy_and_boolean_noop_pragmas_report_sqlite_defaults() {
let c = Connection::open_memory().unwrap();
let zero: &[&str] = &[
"legacy_alter_table",
"count_changes",
"full_column_names",
"empty_result_callbacks",
"defer_foreign_keys",
"ignore_check_constraints",
"reverse_unordered_selects",
"query_only",
"writable_schema",
"threads",
"soft_heap_limit",
"hard_heap_limit",
];
for name in zero {
assert_eq!(
one(&c, &alloc_pragma(name)),
Value::Integer(0),
"PRAGMA {name}"
);
}
assert_eq!(one(&c, "PRAGMA short_column_names"), Value::Integer(1));
assert_eq!(one(&c, "PRAGMA automatic_index"), Value::Integer(1));
for name in ["legacy_file_format", "case_sensitive_like"] {
assert!(
c.query(&alloc_pragma(name)).unwrap().rows.is_empty(),
"PRAGMA {name} should yield no rows"
);
}
let mut m = Connection::open_memory().unwrap();
for s in [
"PRAGMA query_only=1",
"PRAGMA legacy_alter_table=1",
"PRAGMA case_sensitive_like=1",
"PRAGMA automatic_index=0",
] {
m.execute(s).unwrap_or_else(|e| panic!("{s}: {e:?}"));
}
}
#[test]
fn cache_size_round_trips_the_set_value() {
let mut c = Connection::open_memory().unwrap();
assert_eq!(one(&c, "PRAGMA cache_size"), Value::Integer(-2000));
c.execute("PRAGMA cache_size=2000").unwrap();
assert_eq!(one(&c, "PRAGMA cache_size"), Value::Integer(2000));
c.execute("PRAGMA cache_size=-4000").unwrap();
assert_eq!(one(&c, "PRAGMA cache_size"), Value::Integer(-4000));
}
#[test]
fn mmap_size_yields_no_rows() {
let mut c = Connection::open_memory().unwrap();
assert!(c.query("PRAGMA mmap_size").unwrap().rows.is_empty());
c.execute("PRAGMA mmap_size=65536").unwrap();
assert!(c.query("PRAGMA mmap_size").unwrap().rows.is_empty());
}