use spg_engine::{Engine, QueryResult};
fn one(e: &mut Engine, sql: &str) -> Vec<spg_storage::Value<'static>> {
match e.execute(sql).unwrap() {
QueryResult::Rows { rows, .. } => rows[0].values.clone(),
_ => panic!("expected rows"),
}
}
#[test]
fn virtual_generated_column_computes_and_enforces_not_null() {
let mut e = Engine::new();
e.execute("CREATE TABLE g(id int, v int GENERATED ALWAYS AS (id * 2) VIRTUAL)")
.unwrap();
e.execute("INSERT INTO g(id) VALUES (5)").unwrap();
assert_eq!(
one(&mut e, "SELECT id, v FROM g"),
vec![spg_storage::Value::Int(5), spg_storage::Value::Int(10)]
);
e.execute("UPDATE g SET id = 10").unwrap();
assert_eq!(
one(&mut e, "SELECT id, v FROM g"),
vec![spg_storage::Value::Int(10), spg_storage::Value::Int(20)]
);
e.execute("CREATE TABLE gn(id int, v int GENERATED ALWAYS AS (id * 2) VIRTUAL NOT NULL)")
.unwrap();
let err = e
.execute("INSERT INTO gn(id) VALUES (NULL)")
.unwrap_err()
.to_string();
assert!(
err.contains("violates not-null constraint") && err.contains("relation \"gn\""),
"got: {err}"
);
}