use serde_json::{Map, json};
use super::Write;
use super::plan::plan_dog_table as set_plan_dog_table;
use super::unset::{plan_dog_table, plan_section};
fn config() -> serde_json::Value {
json!({
"name": "api",
"dogs": {
"kelpie": {
"git": { "checkout": "/srv/api", "merging": "ff" },
"options": {}
}
}
})
}
fn keys(parts: &[&str]) -> Vec<String> {
parts.iter().map(|key| (*key).to_string()).collect()
}
#[test]
fn a_removal_proposes_the_table_that_survives_it() {
let plan = plan_dog_table("api", "kelpie", &keys(&["git", "merging"]), &config()).unwrap();
assert!(matches!(plan.write, Write::SheepDogTable { .. }));
assert_eq!(
serde_json::to_value(&plan.table).unwrap(),
json!({ "git": { "checkout": "/srv/api" }, "options": {} })
);
}
#[test]
fn a_table_removed_whole_proposes_no_table() {
let plan = plan_dog_table("api", "kelpie", &keys(&[]), &config()).unwrap();
assert!(matches!(plan.write, Write::DropSheepDogTable { .. }));
assert_eq!(
plan.table,
Map::new(),
"the table is gone, and proposing it as still there would ask the wrong question"
);
}
#[test]
fn a_section_removal_proposes_the_section_that_is_left() {
let doc =
"repo = \"/srv/project\"\n\n[worker]\nallowed_domains = [\"github.com\"]\ntimeout = 30\n"
.parse::<toml_edit::DocumentMut>()
.unwrap();
let plan = plan_section(doc, "kelpie", &keys(&["worker", "timeout"])).unwrap();
assert!(matches!(plan.write, Write::DogSection { .. }));
assert_eq!(
serde_json::to_value(&plan.table).unwrap(),
json!({
"repo": "/srv/project",
"worker": { "allowed_domains": ["github.com"] },
})
);
}
#[test]
fn set_and_unset_propose_the_same_holder() {
let written = set_plan_dog_table(
"api",
"kelpie",
&keys(&["git", "merging"]),
&config(),
&json!("rebase"),
)
.unwrap();
let removed = plan_dog_table("api", "kelpie", &keys(&["git", "merging"]), &config()).unwrap();
assert_eq!(
serde_json::to_value(&written.table).unwrap(),
json!({
"git": { "checkout": "/srv/api", "merging": "rebase" },
"options": {},
})
);
assert_eq!(
serde_json::to_value(&removed.table).unwrap(),
json!({ "git": { "checkout": "/srv/api" }, "options": {} })
);
}