Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,194 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn batch_upsert_generated_values_and_captured_database_errors_execute() {
|
||||
let root = project(
|
||||
"model-writes-native",
|
||||
r#"
|
||||
flow seed() (out: Int)
|
||||
| step | kind | do |
|
||||
| out | data | data.createMany([{ title = " First% ", code = "first" }, { code = "second", title = "Second_" }]) |
|
||||
flow empty() (out: Int)
|
||||
| step | kind | do |
|
||||
| out | data | data.createMany([]) |
|
||||
flow batchConflict() (out: Int)
|
||||
| step | kind | do |
|
||||
| out | data | data.createMany([{ title = "must roll back", code = "third" }, { title = "duplicate", code = "first" }]) |
|
||||
flow save(code: Text?, title: Text) (out: data.Article)
|
||||
| step | kind | do |
|
||||
| out | data | data.upsert({code = code}, {title = title}, {title = title}) |
|
||||
flow keep(code: Text?) (out: data.Article)
|
||||
| step | kind | do |
|
||||
| out | data | data.upsert({code = code}, {title = "created"}, {}) |
|
||||
flow find(needle: Text) (out: data.Page)
|
||||
| step | kind | do |
|
||||
| out | data | data.page(title.contains(needle), order = id.asc) |
|
||||
flow count() (out: Int)
|
||||
| step | kind | do |
|
||||
| out | data | data.count() |
|
||||
type Read { Found(value: data.Article), error Storage(value: dever.database.Error) }
|
||||
rule present(value: Read) (out: Bool) recover("记录不存在时报告查询失败")
|
||||
| value | out |
|
||||
| Found(article) | true |
|
||||
| Storage(error) | false |
|
||||
flow captured(article_id: data.id) (out: Bool)
|
||||
| step | kind | do |
|
||||
| read: Read | data | result(data.get(article_id)) |
|
||||
| out | rule | present(read) |
|
||||
checkedTitle(value:Text) (out:Text) {
|
||||
check length(value) > 0 else Invalid
|
||||
out = value
|
||||
}
|
||||
flow caughtTitle(title: Text) (out: Bool)
|
||||
| step | kind | do |
|
||||
| saved: Read | data | result(data.create({title = checkedTitle(title)})) |
|
||||
| out | rule | present(saved) |
|
||||
flow caughtSave(code: Text?, title: Text) (out: Bool)
|
||||
| step | kind | do |
|
||||
| saved: Read | data | result(data.upsert({code = code},{title = title},{})) |
|
||||
| out | rule | present(saved) |
|
||||
flow calendar(code: Text, day: Date, clock: Time, moment: DateTime, tag: Uuid?) (out: data.Article)
|
||||
| step | kind | do |
|
||||
| out | data | data.create({title = code, code = code, day = day, clock = clock, moment = moment, tag = tag}) |
|
||||
"#,
|
||||
);
|
||||
write_source(
|
||||
root.join("module/demo/article/data/main.dever"),
|
||||
r#"
|
||||
type Article {
|
||||
title: Text(1, 120)
|
||||
code: Text? unique
|
||||
token: Uuid generated unique
|
||||
day: Date?
|
||||
clock: Time?
|
||||
moment: DateTime?
|
||||
tag: Uuid?
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(root.join("module/demo/article/api.dever"), "cmd seed = app.seed\ncmd empty = app.empty\ncmd batchConflict = app.batchConflict\ncmd save = app.save\ncmd keep = app.keep\ncmd find = app.find\ncmd count = app.count\ncmd captured = app.captured\ncmd calendar = app.calendar\ncmd caughtSave = app.caughtSave\ncmd caughtTitle = app.caughtTitle").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 (loaded, program) = analyze(&root);
|
||||
let artifact = build::build(&loaded, &program, "main", Profile::Dev, None).unwrap();
|
||||
let call = |name: &str, input: serde_json::Value| {
|
||||
Command::new(&artifact.binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.args([&format!("demo.article.{name}"), &input.to_string()])
|
||||
.output()
|
||||
.unwrap()
|
||||
};
|
||||
let data = |name: &str, input: serde_json::Value| {
|
||||
let output = success(call(name, input));
|
||||
serde_json::from_str::<serde_json::Value>(&output).unwrap()["data"].clone()
|
||||
};
|
||||
success(
|
||||
Command::new(&artifact.binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.arg("migrate")
|
||||
.output()
|
||||
.unwrap(),
|
||||
);
|
||||
assert_eq!(data("seed", serde_json::json!({})), 2);
|
||||
assert_eq!(data("empty", serde_json::json!({})), 0);
|
||||
assert!(
|
||||
!call("batchConflict", serde_json::json!({}))
|
||||
.status
|
||||
.success()
|
||||
);
|
||||
assert_eq!(data("count", serde_json::json!({})), 2);
|
||||
let first = data("find", serde_json::json!({"needle":"%"}));
|
||||
assert_eq!(first["total"], 1);
|
||||
assert_eq!(first["items"][0]["title"], "First%");
|
||||
let first = &first["items"][0];
|
||||
assert_eq!(first["token"].as_str().unwrap().chars().nth(14), Some('7'));
|
||||
let updated = data(
|
||||
"save",
|
||||
serde_json::json!({"code":"first","title":" changed "}),
|
||||
);
|
||||
assert_eq!(updated["id"], first["id"]);
|
||||
assert_eq!(updated["token"], first["token"]);
|
||||
assert_eq!(updated["created_at"], first["created_at"]);
|
||||
assert_eq!(updated["title"], "changed");
|
||||
assert_eq!(data("keep", serde_json::json!({"code":"first"})), updated);
|
||||
let created = data("keep", serde_json::json!({"code":"new"}));
|
||||
assert_eq!(created["title"], "created");
|
||||
assert_ne!(created["token"], updated["token"]);
|
||||
assert!(
|
||||
!call("save", serde_json::json!({"code":null,"title":"invalid"}))
|
||||
.status
|
||||
.success()
|
||||
);
|
||||
assert_eq!(data("count", serde_json::json!({})), 3);
|
||||
assert_eq!(
|
||||
data("captured", serde_json::json!({"article_id":9999})),
|
||||
false
|
||||
);
|
||||
assert_eq!(
|
||||
data("captured", serde_json::json!({"article_id":updated["id"]})),
|
||||
true
|
||||
);
|
||||
assert_eq!(
|
||||
data(
|
||||
"caughtSave",
|
||||
serde_json::json!({"code":null,"title":"handled failure"})
|
||||
),
|
||||
false
|
||||
);
|
||||
assert_eq!(
|
||||
data(
|
||||
"caughtSave",
|
||||
serde_json::json!({"code":"first","title":"unchanged"})
|
||||
),
|
||||
true
|
||||
);
|
||||
let invalid = call("caughtTitle", serde_json::json!({"title":""}));
|
||||
assert!(!invalid.status.success());
|
||||
assert!(String::from_utf8_lossy(&invalid.stderr).contains("Invalid"));
|
||||
assert!(!String::from_utf8_lossy(&invalid.stderr).contains("InvalidData"));
|
||||
assert_eq!(
|
||||
data("caughtTitle", serde_json::json!({"title":"正常实参"})),
|
||||
true
|
||||
);
|
||||
let calendar = data(
|
||||
"calendar",
|
||||
serde_json::json!({"code":"calendar","day":"2026-11-12","clock":"12:34:56","moment":"2026-11-12T12:34:56Z","tag":updated["token"]}),
|
||||
);
|
||||
assert_eq!(calendar["day"], "2026-11-12");
|
||||
assert_eq!(calendar["clock"], "12:34:56");
|
||||
assert_eq!(calendar["moment"], "2026-11-12T12:34:56Z");
|
||||
assert_eq!(calendar["tag"], updated["token"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn model_rejects_invalid_storage_and_defaults_before_native_compilation() {
|
||||
let root = project(
|
||||
"model-invalid-storage",
|
||||
"copy(value: data.Article) (out: data.Article) { out = value }",
|
||||
);
|
||||
for model in [
|
||||
"type Article { title: Text default -1 }",
|
||||
"type Article { title: Text(2,10) default \" x \" }",
|
||||
"type Article { title: Text(2,10) upper default \"ß\" }", // expands to SS and is valid below
|
||||
"type Article { values: List<Int> }",
|
||||
"type Article { key: Uuid? generated }",
|
||||
] {
|
||||
write_source(root.join("module/demo/article/data/main.dever"), model).unwrap();
|
||||
let loaded = Project::load(&root).unwrap();
|
||||
let result = program::check(&loaded.declarations, &loaded.modules);
|
||||
if model.contains("ß") {
|
||||
assert!(result.is_ok());
|
||||
} else {
|
||||
assert!(result.is_err(), "{model}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user