Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,173 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn named_databases_keep_schemas_separate_and_reject_cross_database_transactions() {
|
||||
let root = project(
|
||||
"model-bindings-native",
|
||||
r#"
|
||||
flow save(title: Text) ()
|
||||
| step | kind | do |
|
||||
| - | data | data.create({title = title}) |
|
||||
transaction flow article(title: Text) ()
|
||||
| step | kind | do |
|
||||
| - | flow | save(title) |
|
||||
flow both(title: Text) ()
|
||||
| step | kind | do |
|
||||
| - | flow | save(title) |
|
||||
| - | flow | demo.user.app.save(title) |
|
||||
flow stages(title: Text) ()
|
||||
| step | kind | do |
|
||||
| - | flow | article(title) |
|
||||
| - | flow | demo.user.app.transactional(title) |
|
||||
flow link(user_id: demo.user.data.id) (out: data.Article) {
|
||||
out = data.create({title = "linked", author_id = user_id})
|
||||
}
|
||||
flow linked(user_id: demo.user.data.id) (out: data.Article) {
|
||||
out = data.get(author_id == user_id)
|
||||
}
|
||||
flow counts() (articles: Int, users: Int)
|
||||
| step | kind | do |
|
||||
| articles | data | data.count() |
|
||||
| users | flow | demo.user.app.count() |
|
||||
"#,
|
||||
);
|
||||
write_source(
|
||||
root.join("module/demo/article/data/main.dever"),
|
||||
"type Article { title: Text, author_id: demo.user.data.id? external }",
|
||||
)
|
||||
.unwrap();
|
||||
write_source(
|
||||
root.join("module/demo/user/data/main.dever"),
|
||||
"database archive\nglobal type User { name: Text }",
|
||||
)
|
||||
.unwrap();
|
||||
write_source(
|
||||
root.join("module/demo/user/app/main.dever"),
|
||||
r#"
|
||||
flow save(name: Text) ()
|
||||
| step | kind | do |
|
||||
| - | data | data.create({name = name}) |
|
||||
transaction flow transactional(name: Text) ()
|
||||
| step | kind | do |
|
||||
| - | flow | save(name) |
|
||||
flow count() (out: Int)
|
||||
| step | kind | do |
|
||||
| out | data | data.count() |
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(
|
||||
root.join("module/demo/article/api.dever"),
|
||||
"cmd both = app.both\nlong cmd stages = app.stages\ncmd counts = app.counts\ncmd link = app.link\ncmd linked = app.linked",
|
||||
)
|
||||
.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/main.db"},"archive":{"type":"sqlite","path":"data/archive.db"}}}"#).unwrap();
|
||||
let (loaded, program) = analyze(&root);
|
||||
let user = program
|
||||
.definitions
|
||||
.iter()
|
||||
.find(|d| d.name == "demo.user.data.User")
|
||||
.unwrap();
|
||||
assert!(user.model.as_ref().unwrap().global);
|
||||
let artifact = build::build(&loaded, &program, "main", Profile::Dev, None).unwrap();
|
||||
let call = |name: &str, input: &str| {
|
||||
Command::new(&artifact.binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.args([&format!("demo.article.{name}"), input])
|
||||
.output()
|
||||
.unwrap()
|
||||
};
|
||||
success(
|
||||
Command::new(&artifact.binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.arg("migrate")
|
||||
.output()
|
||||
.unwrap(),
|
||||
);
|
||||
assert!(
|
||||
!call("both", r#"{"title":"must roll back"}"#)
|
||||
.status
|
||||
.success()
|
||||
);
|
||||
let counts: serde_json::Value = serde_json::from_str(&success(call("counts", "{}"))).unwrap();
|
||||
assert_eq!(counts["data"], serde_json::json!({"articles":0,"users":0}));
|
||||
success(call("stages", r#"{"title":"committed stages"}"#));
|
||||
let counts: serde_json::Value = serde_json::from_str(&success(call("counts", "{}"))).unwrap();
|
||||
assert_eq!(counts["data"], serde_json::json!({"articles":1,"users":1}));
|
||||
let user = r#"{"user_id":1}"#;
|
||||
let saved: serde_json::Value = serde_json::from_str(&success(call("link", user))).unwrap();
|
||||
let found: serde_json::Value = serde_json::from_str(&success(call("linked", user))).unwrap();
|
||||
assert_eq!(saved["data"]["author_id"], 1);
|
||||
assert_eq!(found["data"], saved["data"]);
|
||||
// 读取本测试生成的两个真实文件,确认各库没有另一库的业务表。
|
||||
for (file, expected, absent) in [
|
||||
("main.db", "demo_article", "demo_user"),
|
||||
("archive.db", "demo_user", "demo_article"),
|
||||
] {
|
||||
let path = root.join("data").join(file);
|
||||
let db = dever_runtime::database::Database::sqlite(
|
||||
&path,
|
||||
false,
|
||||
dever_runtime::database::Options {
|
||||
max_page_size: 100,
|
||||
max_connections: 1,
|
||||
wait_timeout: std::time::Duration::from_secs(5),
|
||||
io_timeout: std::time::Duration::from_secs(30),
|
||||
},
|
||||
None,
|
||||
);
|
||||
let path_check = dever_runtime::task::run(true, 8, async |scope| {
|
||||
let mut executor = db.await.unwrap().acquire().await.unwrap();
|
||||
executor
|
||||
.query(
|
||||
scope,
|
||||
"SELECT name FROM sqlite_schema WHERE type='table'".into(),
|
||||
vec![],
|
||||
|row| row.get::<String>(0),
|
||||
)
|
||||
.await
|
||||
})
|
||||
.unwrap();
|
||||
let names = path_check.unwrap();
|
||||
assert!(names.iter().any(|n| n == expected));
|
||||
assert!(!names.iter().any(|n| n == absent));
|
||||
}
|
||||
fs::rename(
|
||||
root.join("data/archive.db"),
|
||||
root.join("data/archive.saved.db"),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!call("counts", "{}").status.success());
|
||||
assert!(!root.join("data/archive.db").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn physical_table_names_are_unique_per_database_binding() {
|
||||
let root = directory("physical-table-collision");
|
||||
let first = "module/sales/item_log/data/item_log.dv";
|
||||
let second = "module/sales_item/log/data/log.dv";
|
||||
write_source(root.join(first), "type ItemLog { title: Text }").unwrap();
|
||||
write_source(root.join(second), "type Log { title: Text }").unwrap();
|
||||
let check = || cli(&["check", root.to_str().unwrap()]);
|
||||
success(cli(&["fmt", root.to_str().unwrap()]));
|
||||
let output = check();
|
||||
let error = String::from_utf8_lossy(&output.stderr);
|
||||
assert!(
|
||||
!output.status.success()
|
||||
&& error.contains("duplicate physical table sales_item_log in database default"),
|
||||
"{error}"
|
||||
);
|
||||
assert!(error.contains(first) && error.contains(second), "{error}");
|
||||
write_source(
|
||||
root.join(second),
|
||||
"database archive\ntype Log { title: Text }",
|
||||
)
|
||||
.unwrap();
|
||||
success(cli(&["fmt", root.to_str().unwrap()]));
|
||||
success(check());
|
||||
}
|
||||
Reference in new issue
Block a user