329 lines
12 KiB
Rust
329 lines
12 KiB
Rust
use super::*;
|
|
|
|
fn executable(name: &str, app: &str, api: &str) -> (PathBuf, PathBuf) {
|
|
let root = project(name, app);
|
|
fs::write(root.join("module/demo/article/api.dever"), api).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","max_connections":4}}}"#,
|
|
)
|
|
.unwrap();
|
|
let (loaded, program) = analyze(&root);
|
|
let artifact = build::build(&loaded, &program, "main", Profile::Dev, None).unwrap();
|
|
success(
|
|
Command::new(&artifact.binary)
|
|
.arg("--config")
|
|
.arg(&config)
|
|
.arg("migrate")
|
|
.output()
|
|
.unwrap(),
|
|
);
|
|
(artifact.binary, config)
|
|
}
|
|
fn call(binary: &std::path::Path, config: &std::path::Path, name: &str, input: &str) -> Output {
|
|
Command::new(binary)
|
|
.arg("--config")
|
|
.arg(config)
|
|
.args([name, input])
|
|
.output()
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn nested_failure_cannot_be_captured_and_committed() {
|
|
let (binary, config) = executable(
|
|
"model-nested-rollback",
|
|
r#"
|
|
type Problem { error Denied }
|
|
type Attempt { Saved, error Rejected(value: Problem), error Storage(value: dever.database.Error) }
|
|
transaction flow inner(title: Text, allowed: Bool) ()
|
|
| step | kind | do |
|
|
| saved | data | data.create({title = title}) |
|
|
| - | check | allowed else Problem.Denied |
|
|
rule handled(value: Attempt) (out: Bool) recover("报告被拒绝的请求")
|
|
| value | out |
|
|
| Saved | true |
|
|
| Rejected(error) | false |
|
|
| Storage(error) | fail(error) |
|
|
transaction flow outer(title: Text, allowed: Bool) (out: Bool)
|
|
| step | kind | do |
|
|
| attempt: Attempt | flow | result(inner(title, allowed)) |
|
|
| out | rule | handled(attempt) |
|
|
flow count() (out: Int)
|
|
| step | kind | do |
|
|
| out | data | data.count() |
|
|
"#,
|
|
"cmd outer = app.outer\ncmd count = app.count",
|
|
);
|
|
let output = call(
|
|
&binary,
|
|
&config,
|
|
"demo.article.outer",
|
|
r#"{"title":"不得提交","allowed":false}"#,
|
|
);
|
|
assert!(!output.status.success());
|
|
assert!(String::from_utf8_lossy(&output.stderr).contains("Database"));
|
|
let output: serde_json::Value =
|
|
serde_json::from_str(&success(call(&binary, &config, "demo.article.count", "{}"))).unwrap();
|
|
assert_eq!(output["data"], 0);
|
|
success(call(
|
|
&binary,
|
|
&config,
|
|
"demo.article.outer",
|
|
r#"{"title":"正常提交","allowed":true}"#,
|
|
));
|
|
let output: serde_json::Value =
|
|
serde_json::from_str(&success(call(&binary, &config, "demo.article.count", "{}"))).unwrap();
|
|
assert_eq!(output["data"], 1);
|
|
}
|
|
|
|
#[test]
|
|
fn long_entry_commits_explicit_stages_and_tasks_use_separate_contexts() {
|
|
let (binary, config) = executable(
|
|
"model-long-stages",
|
|
r#"
|
|
transaction flow save(title: Text) ()
|
|
| step | kind | do |
|
|
| saved | data | data.create({title = title}) |
|
|
flow stages(title: Text, allowed: Bool) ()
|
|
| step | kind | do |
|
|
| - | flow | save(title) |
|
|
| - | flow | dever.time.sleep(1) |
|
|
| - | check | allowed else Conflict |
|
|
flow count() (out: Int)
|
|
| step | kind | do |
|
|
| out | data | data.count() |
|
|
flow concurrent() (out: Int)
|
|
| step | kind | do |
|
|
| task | calc | run(count()) |
|
|
| out | calc | wait(task) |
|
|
flow counts() (left: Int, right: Int)
|
|
| step | kind | parallel | do |
|
|
| left | data | reads | data.count() |
|
|
| right | flow | reads | count() |
|
|
"#,
|
|
"long cmd stages = app.stages\ncmd count = app.concurrent\ncmd counts = app.counts",
|
|
);
|
|
assert!(
|
|
!call(
|
|
&binary,
|
|
&config,
|
|
"demo.article.stages",
|
|
r#"{"title":"已提交阶段","allowed":false}"#
|
|
)
|
|
.status
|
|
.success()
|
|
);
|
|
let output: serde_json::Value =
|
|
serde_json::from_str(&success(call(&binary, &config, "demo.article.count", "{}"))).unwrap();
|
|
assert_eq!(output["data"], 1);
|
|
let output: serde_json::Value = serde_json::from_str(&success(call(
|
|
&binary,
|
|
&config,
|
|
"demo.article.counts",
|
|
"{}",
|
|
)))
|
|
.unwrap();
|
|
assert_eq!(output["data"], serde_json::json!({"left":1,"right":1}));
|
|
}
|
|
|
|
#[test]
|
|
fn command_transaction_and_model_access_constraints_are_static() {
|
|
let root = project(
|
|
"model-entry-constraints",
|
|
r#"
|
|
flow create(title: Text) ()
|
|
| step | kind | do |
|
|
| saved | data | data.create({title = title}) |
|
|
"#,
|
|
);
|
|
fs::write(
|
|
root.join("module/demo/article/api.dever"),
|
|
"long cmd create = app.create",
|
|
)
|
|
.unwrap();
|
|
let loaded = Project::load(&root).unwrap();
|
|
let error = program::check(&loaded.declarations, &loaded.modules)
|
|
.err()
|
|
.unwrap();
|
|
assert!(error.message.contains("transaction flow"), "{error:?}");
|
|
fs::write(
|
|
root.join("module/demo/article/api.dever"),
|
|
"cmd create = app.create",
|
|
)
|
|
.unwrap();
|
|
for app in [
|
|
"flow create(title: Text) ()\n| step | kind | do |\n| saved | data | data.create({title = title}) |\n| - | flow | dever.time.sleep(1) |",
|
|
"flow create(title: Text) ()\n| step | kind | do |\n| changed | data | data.update({}, {title = title}) |",
|
|
"flow create(title: Text) ()\n| step | kind | do |\n| saved | data | demo.user.data.create({name = title}) |",
|
|
"transaction flow create(title: Text) ()\n| step | kind | parallel | do |\n| left | data | reads | data.count() |\n| right | data | reads | data.count() |",
|
|
] {
|
|
write_source(root.join("module/demo/article/app/main.dever"), app).unwrap();
|
|
let loaded = Project::load(&root).unwrap();
|
|
assert!(loaded.diagnostics.is_empty(), "{:?}", loaded.diagnostics);
|
|
assert!(
|
|
program::check(&loaded.declarations, &loaded.modules).is_err(),
|
|
"{app}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn sqlite_services_migrate_when_configured() {
|
|
services_migrate_when_configured(
|
|
"sqlite-startup-migration",
|
|
serde_json::json!({"type":"sqlite","path":"data/model.db"}),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "requires an isolated PostgreSQL database; run test/postgres.py"]
|
|
fn postgres_services_migrate_when_configured() {
|
|
let url = std::env::var("DEVER_TEST_POSTGRES").expect("isolated PostgreSQL test connection");
|
|
services_migrate_when_configured(
|
|
"postgres-startup-migration",
|
|
serde_json::json!({"type":"postgres","url":url,"tls":"disabled"}),
|
|
);
|
|
}
|
|
|
|
fn services_migrate_when_configured(name: &str, database: serde_json::Value) {
|
|
use crate::api::{Server, config};
|
|
let root = project(
|
|
name,
|
|
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");
|
|
let api = root.join("module/demo/article/api/front");
|
|
fs::create_dir_all(&api).unwrap();
|
|
fs::write(
|
|
api.join("main.dever"),
|
|
"public post create = app.create\npublic get get = app.get",
|
|
)
|
|
.unwrap();
|
|
fs::create_dir_all(root.join("config")).unwrap();
|
|
fs::create_dir_all(root.join("data")).unwrap();
|
|
let (path, port, mut settings) = config(&root);
|
|
settings["database"] = serde_json::json!({"create":true,"default":database});
|
|
fs::write(&path, settings.to_string()).unwrap();
|
|
let build = || {
|
|
let (loaded, program) = analyze(&root);
|
|
build::build(&loaded, &program, "main", Profile::Dev, None).unwrap()
|
|
};
|
|
let artifact = build();
|
|
let first = Server::start(&artifact.binary, &path, port);
|
|
let (status, original) = first.call(
|
|
"POST",
|
|
"/demo/article/front/main/create",
|
|
r#"{"title":"original"}"#,
|
|
"Content-Type: application/json\r\n",
|
|
);
|
|
assert_eq!(status, 200, "{original}");
|
|
let (_, second_port, mut second_settings) = config(&root);
|
|
let second_path = path.with_file_name("second.json");
|
|
second_settings["database"] = settings["database"].clone();
|
|
fs::write(&second_path, second_settings.to_string()).unwrap();
|
|
first.stop();
|
|
drop(artifact);
|
|
|
|
write_source(
|
|
&model,
|
|
"type Article { title: Text, state: Text default \"draft\" }",
|
|
)
|
|
.unwrap();
|
|
let artifact = build();
|
|
settings["database"]["create"] = false.into();
|
|
fs::write(&path, settings.to_string()).unwrap();
|
|
let reserved = std::net::TcpListener::bind(("127.0.0.1", port)).unwrap();
|
|
let rejected = Command::new(&artifact.binary)
|
|
.arg("--config")
|
|
.arg(&path)
|
|
.arg("api")
|
|
.output()
|
|
.unwrap();
|
|
drop(reserved);
|
|
assert!(!rejected.status.success());
|
|
let error = String::from_utf8_lossy(&rejected.stderr);
|
|
assert!(error.contains("schema is outdated or changed"), "{error}");
|
|
|
|
settings["database"]["create"] = true.into();
|
|
fs::write(&path, settings.to_string()).unwrap();
|
|
let (first, second) = std::thread::scope(|scope| {
|
|
let first = scope.spawn(|| Server::start(&artifact.binary, &path, port));
|
|
let second = scope.spawn(|| Server::start(&artifact.binary, &second_path, second_port));
|
|
(first.join().unwrap(), second.join().unwrap())
|
|
});
|
|
for server in [first, second] {
|
|
let (status, updated) =
|
|
server.call("GET", "/demo/article/front/main/get?article_id=1", "", "");
|
|
assert_eq!(status, 200, "{updated}");
|
|
assert_eq!(updated["data"]["id"], original["data"]["id"]);
|
|
assert_eq!(
|
|
updated["data"]["created_at"],
|
|
original["data"]["created_at"]
|
|
);
|
|
assert_eq!(updated["data"]["title"], "original");
|
|
assert_eq!(updated["data"]["state"], "draft");
|
|
server.stop();
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn http_startup_prepares_declared_models_without_database_calls() {
|
|
use crate::api::{Server, config};
|
|
let root = project("declared-model-startup", "hello() (out: Int) { out = 42 }");
|
|
let api = root.join("module/demo/article/api/front");
|
|
fs::create_dir_all(&api).unwrap();
|
|
fs::write(api.join("main.dever"), "public get hello = app.hello").unwrap();
|
|
fs::create_dir_all(root.join("config")).unwrap();
|
|
fs::create_dir_all(root.join("data")).unwrap();
|
|
let (path, port, mut settings) = config(&root);
|
|
settings["database"] =
|
|
serde_json::json!({"create":true,"default":{"type":"sqlite","path":"data/model.db"}});
|
|
fs::write(&path, settings.to_string()).unwrap();
|
|
let (loaded, program) = analyze(&root);
|
|
let artifact = build::build(&loaded, &program, "main", Profile::Dev, None).unwrap();
|
|
let server = Server::start(&artifact.binary, &path, port);
|
|
assert!(root.join("data/model.db").is_file());
|
|
let (status, value) = server.call("GET", "/demo/article/front/main/hello", "", "");
|
|
assert_eq!(status, 200, "{value}");
|
|
assert_eq!(value["data"], 42);
|
|
server.stop();
|
|
}
|
|
|
|
#[test]
|
|
fn front_startup_prepares_declared_models() {
|
|
use crate::api::{Server, config};
|
|
let root = project("front-model-startup", "hello() (out: Int) { out = 42 }");
|
|
let front = root.join("module/demo/front/src");
|
|
for directory in [&front, &root.join("config"), &root.join("data")] {
|
|
fs::create_dir_all(directory).unwrap();
|
|
}
|
|
fs::write(front.join("index.html"), "<h1>Dever</h1>").unwrap();
|
|
let (path, port, mut settings) = config(&root);
|
|
settings["sites"]["front"]["front"] = serde_json::json!({"component":"demo","path":"/"});
|
|
settings["database"] =
|
|
serde_json::json!({"create":true,"default":{"type":"sqlite","path":"data/model.db"}});
|
|
fs::write(&path, settings.to_string()).unwrap();
|
|
let (loaded, program) = analyze(&root);
|
|
let artifact = build::build(&loaded, &program, "main", Profile::Dev, None).unwrap();
|
|
Server::start(&artifact.binary, &path, port).stop();
|
|
|
|
settings["database"]["create"] = false.into();
|
|
fs::write(&path, settings.to_string()).unwrap();
|
|
let server = Server::start(&artifact.binary, &path, port);
|
|
let (status, body, _) = server.raw_response("GET", "/", "", "");
|
|
assert_eq!(status, 200, "{body}");
|
|
assert_eq!(body, "<h1>Dever</h1>");
|
|
server.stop();
|
|
}
|