Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,244 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn seeds_insert_missing_keys_without_overwriting_live_records() {
|
||||
let root = project(
|
||||
"model-seed-native",
|
||||
r#"
|
||||
type Entry {code:Text, amount:Decimal, title:Text, token:Uuid}
|
||||
loop entries(rows:List<data.Article>) (out:List<Entry>)
|
||||
| each row in rows | result |
|
||||
| out | list Entry {code=row.code,amount=row.amount,title=row.title,token=row.token} |
|
||||
flow all() (total:Int, items:List<Entry>)
|
||||
| step | kind | do |
|
||||
| page | data | data.page(order = id.asc) |
|
||||
| total | calc | page.total |
|
||||
| items | loop | entries(page.items) |
|
||||
flow change(article_id: data.id, title: Text) ()
|
||||
| step | kind | do |
|
||||
| - | data | data.update(article_id, {title = title}) |
|
||||
"#,
|
||||
);
|
||||
write_source(
|
||||
root.join("module/demo/user/data/main.dever"),
|
||||
"type User { name: Text unique }\nseed { {name = \"作者\"} }",
|
||||
)
|
||||
.unwrap();
|
||||
let model = root.join("module/demo/article/data/main.dever");
|
||||
let base = "type Article { user_id: demo.user.data.id, code: Text lower, title: Text(1,30), token: Uuid generated, amount: Decimal(8,2) default 0.50 }\nunique(user_id,code)\n";
|
||||
write_source(
|
||||
&model,
|
||||
format!("{base}seed {{ {{user_id = 1, code = \" FIRST \", title = \"first\"}} }}"),
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(
|
||||
root.join("module/demo/article/api.dever"),
|
||||
"cmd all = app.all\ncmd change = app.change",
|
||||
)
|
||||
.unwrap();
|
||||
fs::create_dir_all(root.join("config")).unwrap();
|
||||
fs::create_dir_all(root.join("data")).unwrap();
|
||||
let config = root.join("config/setting.json");
|
||||
fs::write(
|
||||
&config,
|
||||
r#"{"database":{"default":{"type":"sqlite","path":"data/model.db"}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let build = || {
|
||||
let (loaded, program) = analyze(&root);
|
||||
build::build(&loaded, &program, "main", Profile::Dev, None).unwrap()
|
||||
};
|
||||
let migrate = |binary: &Path| {
|
||||
success(
|
||||
Command::new(binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.arg("migrate")
|
||||
.output()
|
||||
.unwrap(),
|
||||
)
|
||||
};
|
||||
let call = |binary: &Path, name: &str, input: &str| {
|
||||
success(
|
||||
Command::new(binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.args([name, input])
|
||||
.output()
|
||||
.unwrap(),
|
||||
)
|
||||
};
|
||||
let page = |binary: &Path| {
|
||||
serde_json::from_str::<serde_json::Value>(&call(binary, "demo.article.all", "{}")).unwrap()
|
||||
["data"]
|
||||
.clone()
|
||||
};
|
||||
let artifact = build();
|
||||
migrate(&artifact.binary);
|
||||
let first = page(&artifact.binary);
|
||||
assert_eq!(first["total"], 1);
|
||||
assert_eq!(first["items"][0]["code"], "first");
|
||||
assert_eq!(first["items"][0]["amount"], "0.5");
|
||||
call(
|
||||
&artifact.binary,
|
||||
"demo.article.change",
|
||||
r#"{"article_id":1,"title":"edited"}"#,
|
||||
);
|
||||
migrate(&artifact.binary);
|
||||
assert_eq!(page(&artifact.binary)["items"][0]["title"], "edited");
|
||||
drop(artifact);
|
||||
write_source(&model, format!("{base}seed {{ {{user_id = 1, code = \"FIRST\", title = \"must not replace\"}}\n{{user_id = 1,code = \"second\",title = \"new\",amount = 12.50}} }}")).unwrap();
|
||||
let artifact = build();
|
||||
migrate(&artifact.binary);
|
||||
migrate(&artifact.binary);
|
||||
let after = page(&artifact.binary);
|
||||
assert_eq!(after["total"], 2);
|
||||
assert_eq!(after["items"][0]["title"], "edited");
|
||||
assert_eq!(after["items"][0]["token"], first["items"][0]["token"]);
|
||||
assert_eq!(after["items"][1]["amount"], "12.5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn model_rename_addition_and_failed_migration_preserve_real_data() {
|
||||
let root = project(
|
||||
"model-migration-native",
|
||||
r#"
|
||||
flow create(title: Text) (out: data.Article)
|
||||
| step | kind | do |
|
||||
| out | data | data.create({title = title}) |
|
||||
flow get(article_id: data.id) (out: data.Article)
|
||||
| step | kind | do |
|
||||
| out | data | data.get(article_id) |
|
||||
"#,
|
||||
);
|
||||
let model = root.join("module/demo/article/data/main.dever");
|
||||
write_source(&model, "type Article { title: Text(2,20) }").unwrap();
|
||||
fs::write(
|
||||
root.join("module/demo/article/api.dever"),
|
||||
"cmd create = app.create\ncmd get = app.get",
|
||||
)
|
||||
.unwrap();
|
||||
fs::create_dir_all(root.join("config")).unwrap();
|
||||
fs::create_dir_all(root.join("data")).unwrap();
|
||||
let config = root.join("config/setting.json");
|
||||
fs::write(
|
||||
&config,
|
||||
r#"{"database":{"default":{"type":"sqlite","path":"data/model.db"}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let build = || {
|
||||
let (loaded, program) = analyze(&root);
|
||||
build::build(&loaded, &program, "main", Profile::Dev, None).unwrap()
|
||||
};
|
||||
let migrate = |binary: &Path| {
|
||||
Command::new(binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.arg("migrate")
|
||||
.output()
|
||||
.unwrap()
|
||||
};
|
||||
let call = |binary: &Path, name: &str, input: &str| {
|
||||
Command::new(binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.args([name, input])
|
||||
.output()
|
||||
.unwrap()
|
||||
};
|
||||
let artifact = build();
|
||||
success(migrate(&artifact.binary));
|
||||
// NUL 也按 Unicode 码点计算,不能使用 SQLite 内建 length 的截断语义。
|
||||
let original: serde_json::Value = serde_json::from_str(&success(call(
|
||||
&artifact.binary,
|
||||
"demo.article.create",
|
||||
r#"{"title":"文\u0000章"}"#,
|
||||
)))
|
||||
.unwrap();
|
||||
drop(artifact);
|
||||
write_source(root.join("module/demo/article/app/main.dever"), "flow get(article_id: data.id) (out: data.Article)\n| step | kind | do |\n| out | data | data.get(article_id) |").unwrap();
|
||||
fs::write(
|
||||
root.join("module/demo/article/api.dever"),
|
||||
"cmd get = app.get",
|
||||
)
|
||||
.unwrap();
|
||||
let renamed = "type Article { heading: Text(2,20) from title, token: Uuid generated unique, amount: Decimal(8,2) default 12.50, label: Text lower default \" NEWS \", note: Text(1,10)? }";
|
||||
write_source(&model, renamed).unwrap();
|
||||
let artifact = build();
|
||||
assert!(
|
||||
!call(&artifact.binary, "demo.article.get", r#"{"article_id":1}"#)
|
||||
.status
|
||||
.success()
|
||||
);
|
||||
success(migrate(&artifact.binary));
|
||||
success(migrate(&artifact.binary));
|
||||
let read = |binary: &Path| {
|
||||
serde_json::from_str::<serde_json::Value>(&success(call(
|
||||
binary,
|
||||
"demo.article.get",
|
||||
r#"{"article_id":1}"#,
|
||||
)))
|
||||
.unwrap()["data"]
|
||||
.clone()
|
||||
};
|
||||
let after = read(&artifact.binary);
|
||||
assert_eq!(after["id"], original["data"]["id"]);
|
||||
assert_eq!(after["created_at"], original["data"]["created_at"]);
|
||||
assert_eq!(after["heading"], original["data"]["title"]);
|
||||
assert_eq!(after["label"], "news");
|
||||
assert_eq!(after["amount"], "12.5");
|
||||
assert!(after["note"].is_null());
|
||||
assert_eq!(after["token"].as_str().unwrap().chars().nth(14), Some('7'));
|
||||
let valid_binary = root.join("renamed");
|
||||
fs::copy(&artifact.binary, &valid_binary).unwrap();
|
||||
drop(artifact);
|
||||
for invalid in [
|
||||
renamed.replace("Text(2,20)", "Text(1,2)"),
|
||||
renamed.replace("heading: Text(2,20) from title, ", ""),
|
||||
renamed.replace(
|
||||
"Decimal(8,2) default 12.50",
|
||||
"Decimal(8,2) <= 10 default 1.00",
|
||||
),
|
||||
] {
|
||||
write_source(&model, invalid).unwrap();
|
||||
let artifact = build();
|
||||
assert!(!migrate(&artifact.binary).status.success());
|
||||
assert_eq!(read(&valid_binary), after);
|
||||
}
|
||||
write_source(
|
||||
&model,
|
||||
renamed.replace(
|
||||
"Decimal(8,2) default 12.50",
|
||||
"Decimal(8,2) > 12.491 and < 12.509 default 12.50",
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
let artifact = build();
|
||||
success(migrate(&artifact.binary));
|
||||
assert_eq!(read(&artifact.binary), after);
|
||||
drop(artifact);
|
||||
let dropped = format!(
|
||||
"{}\nmigrate remove_heading {{ drop heading }}",
|
||||
renamed.replace("heading: Text(2,20) from title, ", "")
|
||||
);
|
||||
write_source(&model, &dropped).unwrap();
|
||||
let artifact = build();
|
||||
success(migrate(&artifact.binary));
|
||||
success(migrate(&artifact.binary));
|
||||
let mut expected = after.clone();
|
||||
expected.as_object_mut().unwrap().remove("heading");
|
||||
assert_eq!(read(&artifact.binary), expected);
|
||||
drop(artifact);
|
||||
write_source(
|
||||
&model,
|
||||
dropped.replace("drop heading", "drop different_column"),
|
||||
)
|
||||
.unwrap();
|
||||
let artifact = build();
|
||||
let failure = migrate(&artifact.binary);
|
||||
assert!(!failure.status.success());
|
||||
assert!(
|
||||
String::from_utf8_lossy(&failure.stderr)
|
||||
.contains("applied migration content or order changed")
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user