#![cfg(feature = "std")]
use graphitesql::Connection;
fn err_msg(c: &mut Connection, sql: &str) -> String {
let e = c.execute(sql).unwrap_err();
format!("{e}")
}
#[test]
fn message_names_table_and_columns() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE VIRTUAL TABLE t USING rtree(id,x0,x1)")
.unwrap();
assert_eq!(
err_msg(&mut c, "INSERT INTO t VALUES(1,2,1)"),
"rtree constraint failed: t.(x0<=x1)"
);
}
#[test]
fn message_names_first_failing_pair() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE VIRTUAL TABLE t USING rtree(id,x0,x1,y0,y1)")
.unwrap();
assert_eq!(
err_msg(&mut c, "INSERT INTO t VALUES(1,0,5,9,2)"),
"rtree constraint failed: t.(y0<=y1)"
);
}
#[test]
fn near_equal_coords_round_to_same_f32_and_are_accepted() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE VIRTUAL TABLE t USING rtree(id,x0,x1)")
.unwrap();
c.execute("INSERT INTO t VALUES(1,1.000000000001,1.0)")
.unwrap();
let r = c.query("SELECT id,x0,x1 FROM t").unwrap();
assert_eq!(r.rows.len(), 1);
}
#[test]
fn coordinate_check_precedes_uniqueness_check() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE VIRTUAL TABLE t USING rtree(id,x0,x1)")
.unwrap();
c.execute("INSERT INTO t VALUES(1,0,5)").unwrap();
assert_eq!(
err_msg(&mut c, "INSERT INTO t VALUES(1,9,2)"),
"rtree constraint failed: t.(x0<=x1)"
);
assert_eq!(
err_msg(&mut c, "INSERT INTO t VALUES(1,0,5)"),
"UNIQUE constraint failed: t.id"
);
}
#[test]
fn conflict_modes_and_bad_coords() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE VIRTUAL TABLE t USING rtree(id,x0,x1)")
.unwrap();
c.execute("INSERT OR IGNORE INTO t VALUES(1,9,2)").unwrap();
assert_eq!(c.query("SELECT count(*) FROM t").unwrap().rows.len(), 1);
assert_eq!(
c.query("SELECT count(*) FROM t").unwrap().rows[0][0],
graphitesql::Value::Integer(0)
);
assert_eq!(
err_msg(&mut c, "INSERT OR REPLACE INTO t VALUES(1,9,2)"),
"rtree constraint failed: t.(x0<=x1)"
);
}
#[test]
fn update_to_inverted_coords_is_rejected() {
let mut c = Connection::open_memory().unwrap();
c.execute("CREATE VIRTUAL TABLE t USING rtree(id,x0,x1)")
.unwrap();
c.execute("INSERT INTO t VALUES(1,0,5)").unwrap();
assert_eq!(
err_msg(&mut c, "UPDATE t SET x0=9, x1=2 WHERE id=1"),
"rtree constraint failed: t.(x0<=x1)"
);
}