Add tests for simple alter

next
Sayan Nandan 2 years ago
parent c5d7f5f6f6
commit 0e67872e69
No known key found for this signature in database
GPG Key ID: 42EEDF4AE9D96B54

@ -24,39 +24,61 @@
* *
*/ */
mod plan { use crate::engine::{
use crate::{
engine::{
core::{ core::{
model::{ model::{alt::AlterPlan, ModelView},
self, tests::model::{create, exec_create},
alt::{AlterAction, AlterPlan}, GlobalNS,
Field, Layer,
},
tests::model::create,
},
error::{DatabaseError, DatabaseResult},
ql::{ast::parse_ast_node_full, tests::lex_insecure},
}, },
vecfuse, error::DatabaseResult,
}; idx::STIndex,
fn with_plan(model: &str, plan: &str, f: impl Fn(AlterPlan)) -> DatabaseResult<()> { ql::{ast::parse_ast_node_full, ddl::alt::AlterModel, tests::lex_insecure},
};
fn with_plan(model: &str, plan: &str, f: impl Fn(AlterPlan)) -> DatabaseResult<()> {
let model = create(model)?; let model = create(model)?;
let tok = lex_insecure(plan.as_bytes()).unwrap(); let tok = lex_insecure(plan.as_bytes()).unwrap();
let alter = parse_ast_node_full(&tok[2..]).unwrap(); let alter = parse_ast_node_full(&tok[2..]).unwrap();
let model_write = model.intent_write_model(); let model_write = model.intent_write_model();
let mv = AlterPlan::fdeltas(&model, &model_write, alter)?; let mv = AlterPlan::fdeltas(&model, &model_write, alter)?;
Ok(f(mv)) Ok(f(mv))
} }
fn plan(model: &str, plan: &str, f: impl Fn(AlterPlan)) { fn plan(model: &str, plan: &str, f: impl Fn(AlterPlan)) {
with_plan(model, plan, f).unwrap() with_plan(model, plan, f).unwrap()
} }
fn exec_plan(
gns: &GlobalNS,
new_space: bool,
model: &str,
plan: &str,
f: impl Fn(&ModelView),
) -> DatabaseResult<()> {
exec_create(gns, model, "myspace", new_space)?;
let tok = lex_insecure(plan.as_bytes()).unwrap();
let alter = parse_ast_node_full::<AlterModel>(&tok[2..]).unwrap();
let model_name = alter.model;
ModelView::exec_alter(gns, "myspace".as_bytes(), alter)?;
let gns_read = gns.spaces().read();
let space = gns_read.st_get("myspace".as_bytes()).unwrap();
let model = space.models().read();
f(model.st_get(model_name.as_bytes()).unwrap());
Ok(())
}
mod plan {
use crate::{
engine::{
core::model::{self, alt::AlterAction, Field, Layer},
error::DatabaseError,
},
vecfuse,
};
/* /*
Simple Simple
*/ */
#[test] #[test]
fn simple_add() { fn simple_add() {
plan( super::plan(
"create model mymodel(username: string, password: binary)", "create model mymodel(username: string, password: binary)",
"alter model mymodel add myfield { type: string, nullable: true }", "alter model mymodel add myfield { type: string, nullable: true }",
|plan| { |plan| {
@ -73,7 +95,7 @@ mod plan {
} }
#[test] #[test]
fn simple_remove() { fn simple_remove() {
plan( super::plan(
"create model mymodel(username: string, password: binary, useless_field: uint8)", "create model mymodel(username: string, password: binary, useless_field: uint8)",
"alter model mymodel remove useless_field", "alter model mymodel remove useless_field",
|plan| { |plan| {
@ -89,7 +111,7 @@ mod plan {
#[test] #[test]
fn simple_update() { fn simple_update() {
// FREEDOM! DAMN THE PASSWORD! // FREEDOM! DAMN THE PASSWORD!
plan( super::plan(
"create model mymodel(username: string, password: binary)", "create model mymodel(username: string, password: binary)",
"alter model mymodel update password { nullable: true }", "alter model mymodel update password { nullable: true }",
|plan| { |plan| {
@ -104,13 +126,31 @@ mod plan {
}, },
); );
} }
#[test]
fn update_need_lock() {
// FIGHT THE NULL
super::plan(
"create model mymodel(username: string, null password: binary)",
"alter model mymodel update password { nullable: false }",
|plan| {
assert_eq!(plan.model.as_str(), "mymodel");
assert!(!plan.no_lock);
assert_eq!(
plan.action,
AlterAction::Update(into_dict! {
"password" => Field::new([Layer::bin()].into(), false)
})
);
},
);
}
/* /*
Illegal Illegal
*/ */
#[test] #[test]
fn illegal_remove_nx() { fn illegal_remove_nx() {
assert_eq!( assert_eq!(
with_plan( super::with_plan(
"create model mymodel(username: string, password: binary)", "create model mymodel(username: string, password: binary)",
"alter model mymodel remove password_e2e", "alter model mymodel remove password_e2e",
|_| {} |_| {}
@ -122,7 +162,7 @@ mod plan {
#[test] #[test]
fn illegal_remove_pk() { fn illegal_remove_pk() {
assert_eq!( assert_eq!(
with_plan( super::with_plan(
"create model mymodel(username: string, password: binary)", "create model mymodel(username: string, password: binary)",
"alter model mymodel remove username", "alter model mymodel remove username",
|_| {} |_| {}
@ -134,7 +174,7 @@ mod plan {
#[test] #[test]
fn illegal_add_pk() { fn illegal_add_pk() {
assert_eq!( assert_eq!(
with_plan( super::with_plan(
"create model mymodel(username: string, password: binary)", "create model mymodel(username: string, password: binary)",
"alter model mymodel add username { type: string }", "alter model mymodel add username { type: string }",
|_| {} |_| {}
@ -146,7 +186,7 @@ mod plan {
#[test] #[test]
fn illegal_add_ex() { fn illegal_add_ex() {
assert_eq!( assert_eq!(
with_plan( super::with_plan(
"create model mymodel(username: string, password: binary)", "create model mymodel(username: string, password: binary)",
"alter model mymodel add password { type: string }", "alter model mymodel add password { type: string }",
|_| {} |_| {}
@ -158,7 +198,7 @@ mod plan {
#[test] #[test]
fn illegal_update_pk() { fn illegal_update_pk() {
assert_eq!( assert_eq!(
with_plan( super::with_plan(
"create model mymodel(username: string, password: binary)", "create model mymodel(username: string, password: binary)",
"alter model mymodel update username { type: string }", "alter model mymodel update username { type: string }",
|_| {} |_| {}
@ -170,7 +210,7 @@ mod plan {
#[test] #[test]
fn illegal_update_nx() { fn illegal_update_nx() {
assert_eq!( assert_eq!(
with_plan( super::with_plan(
"create model mymodel(username: string, password: binary)", "create model mymodel(username: string, password: binary)",
"alter model mymodel update username_secret { type: string }", "alter model mymodel update username_secret { type: string }",
|_| {} |_| {}
@ -183,7 +223,7 @@ mod plan {
let create = format!("create model mymodel(username: string, silly_field: {orig_ty})"); let create = format!("create model mymodel(username: string, silly_field: {orig_ty})");
let alter = format!("alter model mymodel update silly_field {{ type: {new_ty} }}"); let alter = format!("alter model mymodel update silly_field {{ type: {new_ty} }}");
assert_eq!( assert_eq!(
with_plan(&create, &alter, |_| {}).expect_err(&format!( super::with_plan(&create, &alter, |_| {}).expect_err(&format!(
"found no error in transformation: {orig_ty} -> {new_ty}" "found no error in transformation: {orig_ty} -> {new_ty}"
)), )),
DatabaseError::DdlModelAlterBadTypedef, DatabaseError::DdlModelAlterBadTypedef,
@ -300,3 +340,22 @@ mod plan {
); );
} }
} }
mod exec {
use crate::engine::{core::GlobalNS, idx::STIndex};
#[test]
fn exec_simple_alter() {
let gns = GlobalNS::empty();
super::exec_plan(
&gns,
true,
"create model mymodel(username: string, password: binary)",
"alter model mymodel update password { nullable: true }",
|model| {
let schema = model.intent_read_model();
assert!(schema.fields().st_get("password").unwrap().is_nullable());
},
)
.unwrap();
}
}

Loading…
Cancel
Save