Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,151 @@
|
||||
use super::*;
|
||||
mod filters;
|
||||
mod native;
|
||||
mod postgres;
|
||||
mod relation;
|
||||
mod stream;
|
||||
|
||||
#[test]
|
||||
fn cursor_reads_stable_pages_without_count_and_preserves_exact_decimal_order() {
|
||||
let root = project(
|
||||
"model-cursor-native",
|
||||
r#"
|
||||
flow create(title: Text, rank: Int, cost: Decimal) (out: data.Article)
|
||||
| step | kind | do |
|
||||
| out | data | data.create({title = title, rank = rank, cost = cost}) |
|
||||
flow page(after: data.Article?, size: Int, keyword: Text) (out: data.Cursor)
|
||||
| step | kind | do |
|
||||
| out | data | data.cursor(title.contains(keyword), order = [rank.asc, cost.desc], after = after, size = size) |
|
||||
flow newest(after: data.Article?, size: Int) (out: data.Cursor)
|
||||
| step | kind | do |
|
||||
| out | data | data.cursor(after = after, size = size) |
|
||||
flow remove(article_id: data.id) (out: Int)
|
||||
| step | kind | do |
|
||||
| out | data | data.delete(article_id) |
|
||||
"#,
|
||||
);
|
||||
write_source(
|
||||
root.join("module/demo/article/data/main.dever"),
|
||||
"type Article { title: Text, rank: Int, cost: Decimal(20,2), note: Text? }",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(root.join("module/demo/article/api.dever"), "cmd create = app.create\ncmd page = app.page\ncmd newest = app.newest\ncmd remove = app.remove").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/cursor.db"}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let (loaded, program) = analyze(&root);
|
||||
let generated = emit::emit(&program, &ownership::analyze(&program), &loaded.sources);
|
||||
let app = &generated["module/d_4_demo_7_article_3_app.rs"].text;
|
||||
assert!(!app.contains("COUNT("));
|
||||
assert!(!app.contains("OFFSET"));
|
||||
assert!(
|
||||
!app.contains("(NOT "),
|
||||
"cursor SQL must seek without an optional-cursor OR flag"
|
||||
);
|
||||
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([name, &input.to_string()])
|
||||
.output()
|
||||
.unwrap()
|
||||
};
|
||||
success(
|
||||
Command::new(&artifact.binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.arg("migrate")
|
||||
.output()
|
||||
.unwrap(),
|
||||
);
|
||||
let data = |name: &str, input| {
|
||||
serde_json::from_str::<serde_json::Value>(&success(call(name, input))).unwrap()["data"]
|
||||
.clone()
|
||||
};
|
||||
let empty = data(
|
||||
"demo.article.page",
|
||||
serde_json::json!({"after":null,"size":2,"keyword":"keep"}),
|
||||
);
|
||||
assert_eq!(
|
||||
empty,
|
||||
serde_json::json!({"items":[],"next":null,"has_more":false})
|
||||
);
|
||||
let mut rows = Vec::new();
|
||||
for (title, rank, cost) in [
|
||||
("keep A", 1, "9007199254740992.01"),
|
||||
("keep B", 1, "9007199254740992.02"),
|
||||
("keep C", 1, "9007199254740992.02"),
|
||||
("keep D", 2, "1.00"),
|
||||
("skip", 0, "2.00"),
|
||||
] {
|
||||
rows.push(data(
|
||||
"demo.article.create",
|
||||
serde_json::json!({"title":title,"rank":rank,"cost":cost}),
|
||||
));
|
||||
}
|
||||
let first = data(
|
||||
"demo.article.page",
|
||||
serde_json::json!({"after":null,"size":2,"keyword":"keep"}),
|
||||
);
|
||||
assert_eq!(first["items"], serde_json::json!([rows[2], rows[1]]));
|
||||
assert_eq!(first["has_more"], true);
|
||||
assert_eq!(first["next"], rows[1]);
|
||||
// 游标值携带排序键,上一页末记录删除后仍能继续,不重新查询该记录。
|
||||
data(
|
||||
"demo.article.remove",
|
||||
serde_json::json!({"article_id":rows[1]["id"]}),
|
||||
);
|
||||
let second = data(
|
||||
"demo.article.page",
|
||||
serde_json::json!({"after":first["next"],"size":2,"keyword":"keep"}),
|
||||
);
|
||||
assert_eq!(second["items"], serde_json::json!([rows[0], rows[3]]));
|
||||
assert_eq!(second["has_more"], false);
|
||||
assert!(second["next"].is_null());
|
||||
let newest = data(
|
||||
"demo.article.newest",
|
||||
serde_json::json!({"after":null,"size":1}),
|
||||
);
|
||||
assert_eq!(newest["items"], serde_json::json!([rows[4]]));
|
||||
for size in [0, -1, i64::MAX] {
|
||||
let output = call(
|
||||
"demo.article.newest",
|
||||
serde_json::json!({"after":null,"size":size}),
|
||||
);
|
||||
assert!(!output.status.success());
|
||||
assert!(String::from_utf8_lossy(&output.stderr).contains("InvalidData"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cursor_rejects_nullable_keys_wrong_identity_and_unknown_options() {
|
||||
let root = project("model-cursor-errors", "");
|
||||
write_source(
|
||||
root.join("module/demo/article/data/main.dever"),
|
||||
"type Article { title: Text?, rank: Int }",
|
||||
)
|
||||
.unwrap();
|
||||
for query in [
|
||||
"order = [title.asc]",
|
||||
"order = [rank.asc, rank.desc]",
|
||||
"order = []",
|
||||
"order = [missing.asc]",
|
||||
"page = 1",
|
||||
"after = 1",
|
||||
"size = null",
|
||||
] {
|
||||
write_source(root.join("module/demo/article/app/main.dever"), format!("flow read() (out: data.Cursor)\n| step | kind | do |\n| out | data | data.cursor({query}) |\n")).unwrap();
|
||||
let loaded = Project::load(&root).unwrap();
|
||||
assert!(loaded.diagnostics.is_empty(), "{query}");
|
||||
assert!(
|
||||
program::check(&loaded.declarations, &loaded.modules).is_err(),
|
||||
"{query}"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user