#![cfg(feature = "std")]
use graphitesql::Connection;
use std::process::Command;
fn sqlite3_available() -> bool {
Command::new("sqlite3").arg("--version").output().is_ok()
}
#[test]
fn generated_column_rejects_dotted_self_reference() {
let mut c = Connection::open_memory().unwrap();
assert!(c
.execute("CREATE TABLE t(a, b AS (t.a))")
.unwrap_err()
.to_string()
.contains("the \".\" operator prohibited in generated columns"));
let mut c = Connection::open_memory().unwrap();
assert!(c
.execute("CREATE TABLE t(a, b AS (abs(t.a)))")
.unwrap_err()
.to_string()
.contains("the \".\" operator prohibited in generated columns"));
let mut c = Connection::open_memory().unwrap();
assert!(c
.execute("CREATE TABLE t(a, b AS (other.a))")
.unwrap_err()
.to_string()
.contains("no such column: other.a"));
let mut c = Connection::open_memory().unwrap();
assert!(c
.execute("CREATE TABLE t(a, b AS (t.nope))")
.unwrap_err()
.to_string()
.contains("no such column: t.nope"));
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a, b AS (a + 1))").unwrap();
}
#[test]
fn check_accepts_dotted_self_reference() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a, b, CHECK(t.a > 0))").unwrap();
let mut c = Connection::open_memory().unwrap();
assert!(c
.execute("CREATE TABLE t(a, CHECK(other.a > 0))")
.unwrap_err()
.to_string()
.contains("no such column: other.a"));
}
#[test]
fn index_key_rejects_dotted_self_reference() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a, b)").unwrap();
assert!(c
.execute("CREATE INDEX i ON t(t.a)")
.unwrap_err()
.to_string()
.contains("the \".\" operator prohibited in index expressions"));
assert!(c
.execute("CREATE INDEX i ON t(abs(t.a))")
.unwrap_err()
.to_string()
.contains("the \".\" operator prohibited in index expressions"));
assert!(c
.execute("CREATE INDEX i ON t(other.a)")
.unwrap_err()
.to_string()
.contains("no such column: other.a"));
assert!(c
.execute("CREATE INDEX i ON t(t.nope)")
.unwrap_err()
.to_string()
.contains("no such column: t.nope"));
c.execute("CREATE INDEX ok ON t(abs(a), b)").unwrap();
}
#[test]
fn partial_where_accepts_dotted_self_reference() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE TABLE t(a, b)").unwrap();
c.execute("CREATE INDEX i ON t(a) WHERE t.b > 0").unwrap();
assert!(c
.execute("CREATE INDEX j ON t(a) WHERE other.b > 0")
.unwrap_err()
.to_string()
.contains("no such column: other.b"));
assert!(c
.execute("CREATE INDEX k ON t(a) WHERE t.nope > 0")
.unwrap_err()
.to_string()
.contains("no such column: t.nope"));
}
#[test]
fn matches_sqlite_cli() {
if !sqlite3_available() {
eprintln!("sqlite3 CLI not found; skipping");
return;
}
let g = env!("CARGO_BIN_EXE_graphitesql");
let run = |bin: &str, sql: &str| -> String {
let out = Command::new(bin).arg(":memory:").arg(sql).output().unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
if !stdout.trim().is_empty() {
return stdout.trim_end().to_string();
}
String::from_utf8_lossy(&out.stderr)
.lines()
.find(|l| !l.trim_start().starts_with('^'))
.unwrap_or("")
.trim_start_matches("Error: in prepare, ")
.trim_start_matches("Error: ")
.trim_start_matches("error: ")
.to_string()
};
for sql in [
"CREATE TABLE t(a, b AS (t.a))",
"CREATE TABLE t(a, b AS (abs(t.a)))",
"CREATE TABLE t(a, b AS (other.a))",
"CREATE TABLE t(a, b AS (t.nope))",
"CREATE TABLE t(a, b AS (a))",
"CREATE TABLE t(a, b, CHECK(t.a > 0))",
"CREATE TABLE t(a, CHECK(other.a > 0))",
"CREATE TABLE t(a, CHECK(t.nope > 0))",
"CREATE TABLE t(a,b); CREATE INDEX i ON t(t.a)",
"CREATE TABLE t(a,b); CREATE INDEX i ON t(abs(t.a))",
"CREATE TABLE t(a,b); CREATE INDEX i ON t(other.a)",
"CREATE TABLE t(a,b); CREATE INDEX i ON t(t.nope)",
"CREATE TABLE t(a,b); CREATE INDEX i ON t(rowid)",
"CREATE TABLE t(a,b); CREATE INDEX i ON t(a+b)",
"CREATE TABLE t(a,b); CREATE INDEX i ON t(a) WHERE t.b > 0",
"CREATE TABLE t(a,b); CREATE INDEX i ON t(a) WHERE other.b > 0",
"CREATE TABLE t(a,b); CREATE INDEX i ON t(a) WHERE t.nope > 0",
] {
assert_eq!(run("sqlite3", sql), run(g, sql), "for {sql}");
}
}