Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,248 @@
|
||||
use super::*;
|
||||
use dever_runtime::{
|
||||
database::{Sql, config::Config},
|
||||
task,
|
||||
};
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires an isolated PostgreSQL database; run test/postgres.py"]
|
||||
fn postgres_generated_models_queries_transactions_and_migrations() {
|
||||
let url = std::env::var("DEVER_TEST_POSTGRES").expect("isolated PostgreSQL test connection");
|
||||
let root = project(
|
||||
"postgres-model-native",
|
||||
r#"
|
||||
type Title { title: Text }
|
||||
flow save(code: Text, title: Text, cost: Decimal) (out: data.Article)
|
||||
| step | kind | do |
|
||||
| out | data | data.upsert({code = code}, {title = title, cost = cost}, {title = title, cost = cost}) |
|
||||
flow page(after: data.Article?) (out: data.Cursor)
|
||||
| step | kind | do |
|
||||
| out | data | data.cursor(after = after, size = 1, order = cost.asc) |
|
||||
flow filtered(after: data.Article?, minimum: Decimal) (out: data.Cursor)
|
||||
| step | kind | do |
|
||||
| out | data | data.cursor(cost >= minimum, after = after, size = 1, order = cost.asc) |
|
||||
flow total() (out: Int)
|
||||
| step | kind | do |
|
||||
| out | data | data.count() |
|
||||
flow exactPage(page: Int) (out: data.Page)
|
||||
| step | kind | do |
|
||||
| out | data | data.page(page = page, size = 1) |
|
||||
flow filteredTotal() (out: Int)
|
||||
| step | kind | do |
|
||||
| out | data | data.count(code == "two") |
|
||||
flow remove(id: data.id) (out: Int)
|
||||
| step | kind | do |
|
||||
| out | data | data.delete(id) |
|
||||
flow titles() (out: List<Title>)
|
||||
| step | kind | do |
|
||||
| out | data | data.titles() |
|
||||
flow rejected(accept: Bool) ()
|
||||
| step | kind | do |
|
||||
| - | data | data.create({code = "rollback", title = "new", cost = 2.50}) |
|
||||
| - | check | accept else Invalid |
|
||||
"#,
|
||||
);
|
||||
let model = root.join("module/demo/article/data/main.dever");
|
||||
let original = r#"
|
||||
type Article { code: Text unique, title: Text(2,50), cost: Decimal(20,2), live: Bool default true, obsolete: Text default "old" }
|
||||
sql titles() (rows: List<app.Title>) {
|
||||
sqlite = "SELECT title FROM demo_article ORDER BY id"
|
||||
postgres = "SELECT title FROM demo_article ORDER BY id"
|
||||
}
|
||||
"#;
|
||||
write_source(&model, original).unwrap();
|
||||
write_source(
|
||||
root.join("module/demo/user/data/main.dever"),
|
||||
"type User { name: Text unique }",
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(root.join("module/demo/article/api.dever"),"cmd save = app.save\ncmd page = app.page\ncmd filtered = app.filtered\ncmd titles = app.titles\ncmd rejected = app.rejected\ncmd total = app.total\ncmd exactPage = app.exactPage\ncmd filteredTotal = app.filteredTotal\ncmd remove = app.remove").unwrap();
|
||||
fs::create_dir_all(root.join("config")).unwrap();
|
||||
let config = root.join("config/setting.json");
|
||||
let connection = |url: &str| serde_json::json!({"type":"postgres","url":url,"tls":"disabled","max_connections":2});
|
||||
let setting =
|
||||
|url: &str| serde_json::json!({"database":{"default":connection(url)}}).to_string();
|
||||
fs::write(&config, setting(&url)).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: serde_json::Value| {
|
||||
Command::new(binary)
|
||||
.arg("--config")
|
||||
.arg(&config)
|
||||
.args([name, &input.to_string()])
|
||||
.output()
|
||||
.unwrap()
|
||||
};
|
||||
let value = |binary: &Path, name: &str, input: serde_json::Value| {
|
||||
serde_json::from_str::<serde_json::Value>(&success(call(binary,name,input))).unwrap()["data"].clone()
|
||||
};
|
||||
let artifact = build();
|
||||
success(migrate(&artifact.binary));
|
||||
success(migrate(&artifact.binary));
|
||||
let first = value(
|
||||
&artifact.binary,
|
||||
"demo.article.save",
|
||||
serde_json::json!({"code":"one","title":"first","cost":"9007199254740993.01"}),
|
||||
);
|
||||
assert_eq!(first["cost"], "9007199254740993.01");
|
||||
assert_eq!(first["live"], true);
|
||||
let updated = value(
|
||||
&artifact.binary,
|
||||
"demo.article.save",
|
||||
serde_json::json!({"code":"one","title":"updated","cost":"9007199254740993.01"}),
|
||||
);
|
||||
assert_eq!(updated["id"], first["id"]);
|
||||
value(
|
||||
&artifact.binary,
|
||||
"demo.article.save",
|
||||
serde_json::json!({"code":"two","title":"second","cost":"9007199254740993.02"}),
|
||||
);
|
||||
let page = value(
|
||||
&artifact.binary,
|
||||
"demo.article.page",
|
||||
serde_json::json!({"after":null}),
|
||||
);
|
||||
assert_eq!(page["items"][0]["title"], "updated");
|
||||
assert_eq!(page["has_more"], true);
|
||||
let last = value(
|
||||
&artifact.binary,
|
||||
"demo.article.page",
|
||||
serde_json::json!({"after":page["next"]}),
|
||||
);
|
||||
assert_eq!(last["items"][0]["title"], "second");
|
||||
assert_eq!(last["has_more"], false);
|
||||
for (after, minimum, expected) in [
|
||||
(serde_json::Value::Null, "9007199254740993.01", page.clone()),
|
||||
(page["next"].clone(), "9007199254740993.01", last.clone()),
|
||||
(serde_json::Value::Null, "9007199254740993.02", last.clone()),
|
||||
] {
|
||||
assert_eq!(
|
||||
value(
|
||||
&artifact.binary,
|
||||
"demo.article.filtered",
|
||||
serde_json::json!({"after":after,"minimum":minimum})
|
||||
),
|
||||
expected
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
!call(
|
||||
&artifact.binary,
|
||||
"demo.article.rejected",
|
||||
serde_json::json!({"accept":false})
|
||||
)
|
||||
.status
|
||||
.success()
|
||||
);
|
||||
assert_eq!(
|
||||
value(
|
||||
&artifact.binary,
|
||||
"demo.article.titles",
|
||||
serde_json::json!({})
|
||||
)
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.len(),
|
||||
2
|
||||
);
|
||||
let total = |binary: &Path| value(binary, "demo.article.total", serde_json::json!({}));
|
||||
assert_eq!(total(&artifact.binary), 2);
|
||||
assert_eq!(
|
||||
value(
|
||||
&artifact.binary,
|
||||
"demo.article.filteredTotal",
|
||||
serde_json::json!({})
|
||||
),
|
||||
1
|
||||
);
|
||||
let beyond = value(
|
||||
&artifact.binary,
|
||||
"demo.article.exactPage",
|
||||
serde_json::json!({"page":3}),
|
||||
);
|
||||
assert_eq!(
|
||||
beyond,
|
||||
serde_json::json!({"items":[],"page":3,"size":1,"total":2,"pages":2})
|
||||
);
|
||||
drop(artifact);
|
||||
let changed = format!(
|
||||
"{}\n{}",
|
||||
original.replace(", obsolete: Text default \"old\"", ""),
|
||||
r#"
|
||||
migrate revise {
|
||||
before { sqlite = "UPDATE demo_article SET title = title || ?1", postgres = "UPDATE demo_article SET title = title || $1", parameters = ["!"] }
|
||||
drop obsolete
|
||||
after { sqlite = "UPDATE demo_article SET live=?1", postgres = "UPDATE demo_article SET live=$1", parameters = [false] }
|
||||
}
|
||||
"#
|
||||
);
|
||||
write_source(&model, &changed).unwrap();
|
||||
let artifact = build();
|
||||
success(migrate(&artifact.binary));
|
||||
success(migrate(&artifact.binary));
|
||||
let page = value(
|
||||
&artifact.binary,
|
||||
"demo.article.page",
|
||||
serde_json::json!({"after":null}),
|
||||
);
|
||||
assert_eq!(page["items"][0]["title"], "updated!");
|
||||
assert_eq!(page["items"][0]["live"], false);
|
||||
assert!(page["items"][0].get("obsolete").is_none());
|
||||
assert_eq!(total(&artifact.binary), 2);
|
||||
assert_eq!(
|
||||
value(
|
||||
&artifact.binary,
|
||||
"demo.article.remove",
|
||||
serde_json::json!({"id":first["id"]})
|
||||
),
|
||||
1
|
||||
);
|
||||
assert_eq!(total(&artifact.binary), 1);
|
||||
let database: Config = serde_json::from_value(connection(&url)).unwrap();
|
||||
task::run(true, 16, async |scope| {
|
||||
let database = database.open(&root, false).await?;
|
||||
let mut executor = database.acquire().await?;
|
||||
for sql in [
|
||||
"CREATE ROLE dever_page_viewer",
|
||||
"GRANT SELECT ON demo_article, _dever_model_count, _dever_history TO dever_page_viewer",
|
||||
"ALTER TABLE demo_article ENABLE ROW LEVEL SECURITY",
|
||||
"CREATE POLICY published_article ON demo_article FOR SELECT TO dever_page_viewer USING (code='one')",
|
||||
] {
|
||||
executor.execute(scope, Sql::same(sql), vec![]).await?;
|
||||
}
|
||||
Ok::<_, dever_runtime::database::Error>(())
|
||||
}).unwrap().unwrap();
|
||||
fs::write(
|
||||
&config,
|
||||
setting(&format!("{url} options='-c role=dever_page_viewer'")),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(total(&artifact.binary), 0);
|
||||
assert_eq!(
|
||||
value(
|
||||
&artifact.binary,
|
||||
"demo.article.exactPage",
|
||||
serde_json::json!({"page":1})
|
||||
)["total"],
|
||||
0
|
||||
);
|
||||
fs::write(&config, setting(&url)).unwrap();
|
||||
drop(artifact);
|
||||
write_source(
|
||||
&model,
|
||||
changed.replace("parameters = [false]", "parameters = [true]"),
|
||||
)
|
||||
.unwrap();
|
||||
let artifact = build();
|
||||
assert!(!migrate(&artifact.binary).status.success());
|
||||
}
|
||||
Reference in new issue
Block a user