use spg_engine::{Engine, QueryResult};
use spg_storage::{DataType, Value};
fn engine_with(sqls: &[&str]) -> Engine {
let mut eng = Engine::new();
for sql in sqls {
let r = eng
.execute(sql)
.unwrap_or_else(|e| panic!("setup {sql:?}: {e:?}"));
assert!(matches!(r, QueryResult::CommandOk { .. }), "{sql:?}");
}
eng
}
fn select(eng: &mut Engine, sql: &str) -> Vec<Vec<Value<'static>>> {
match eng.execute(sql).unwrap() {
QueryResult::Rows { rows, .. } => rows.into_iter().map(|r| r.values).collect(),
_ => panic!("expected Rows"),
}
}
#[test]
fn ddl_accepts_time_keyword() {
let mut eng = Engine::new();
eng.execute("CREATE TABLE t (id INT NOT NULL, alarm TIME)")
.unwrap();
let bytes = eng.snapshot();
let cat = spg_storage::Catalog::deserialize(&bytes).unwrap();
let schema = cat.get("t").unwrap().schema();
assert!(matches!(schema.columns[1].ty, DataType::Time));
}
#[test]
fn insert_and_select_time_round_trips() {
let mut eng = engine_with(&[
"CREATE TABLE t (id INT NOT NULL, alarm TIME)",
"INSERT INTO t VALUES (1, '14:30:45')",
]);
let rows = select(&mut eng, "SELECT alarm FROM t");
assert_eq!(rows.len(), 1);
let Value::Time(us) = &rows[0][0] else {
panic!("expected Value::Time, got {:?}", rows[0][0]);
};
assert_eq!(*us, 52_245_000_000);
}
#[test]
fn insert_with_microseconds() {
let mut eng = engine_with(&[
"CREATE TABLE t (id INT NOT NULL, alarm TIME)",
"INSERT INTO t VALUES (1, '14:30:45.123456')",
]);
let rows = select(&mut eng, "SELECT alarm FROM t");
let Value::Time(us) = &rows[0][0] else {
panic!()
};
assert_eq!(*us, 52_245_123_456);
}
#[test]
fn time_zero_midnight_round_trips() {
let mut eng = engine_with(&[
"CREATE TABLE t (id INT NOT NULL, alarm TIME)",
"INSERT INTO t VALUES (1, '00:00:00')",
]);
let rows = select(&mut eng, "SELECT alarm FROM t");
let Value::Time(us) = &rows[0][0] else {
panic!()
};
assert_eq!(*us, 0);
}
#[test]
fn time_end_of_day_round_trips() {
let mut eng = engine_with(&[
"CREATE TABLE t (id INT NOT NULL, alarm TIME)",
"INSERT INTO t VALUES (1, '23:59:59.999999')",
]);
let rows = select(&mut eng, "SELECT alarm FROM t");
let Value::Time(us) = &rows[0][0] else {
panic!()
};
assert_eq!(*us, 86_399_999_999);
}
#[test]
fn time_column_survives_catalog_round_trip() {
let mut eng = engine_with(&[
"CREATE TABLE schedule (id INT NOT NULL, alarm TIME)",
"INSERT INTO schedule VALUES (1, '08:30:00'), (2, '17:45:30')",
]);
let bytes = eng.snapshot();
let cat = spg_storage::Catalog::deserialize(&bytes).unwrap();
let mut eng2 = Engine::restore(cat);
let rows = select(&mut eng2, "SELECT id, alarm FROM schedule ORDER BY id");
assert_eq!(rows.len(), 2);
let Value::Time(a) = &rows[0][1] else {
panic!()
};
let Value::Time(b) = &rows[1][1] else {
panic!()
};
assert_eq!(*a, 30_600_000_000); assert_eq!(*b, 63_930_000_000); }
#[test]
fn time_null_column() {
let mut eng = engine_with(&[
"CREATE TABLE t (id INT NOT NULL, alarm TIME)",
"INSERT INTO t VALUES (1, NULL)",
]);
let rows = select(&mut eng, "SELECT alarm FROM t");
assert!(matches!(rows[0][0], Value::Null));
}
#[test]
fn time_malformed_input_is_error() {
let mut eng = engine_with(&["CREATE TABLE t (id INT NOT NULL, alarm TIME)"]);
let r = eng.execute("INSERT INTO t VALUES (1, 'not a time')");
assert!(
r.is_err(),
"garbage TIME literal must error, not silently store"
);
}
#[test]
fn time_out_of_range_hour_is_error() {
let mut eng = engine_with(&["CREATE TABLE t (id INT NOT NULL, alarm TIME)"]);
let r = eng.execute("INSERT INTO t VALUES (1, '25:00:00')");
assert!(r.is_err(), "hour > 23 must error");
}