some tests
parent
bcbcd6fbb7
commit
3c64bb27de
|
|
@ -159,15 +159,18 @@ impl File {
|
|||
|
||||
pub fn persist_stage(&self) -> Result<(), String> {
|
||||
let persisted = self.events()?.snapshot()?;
|
||||
let snapshot = serde_json::to_string(&persisted).unwrap();
|
||||
let snapshot = snapshot.as_str();
|
||||
let snapshot: serde_json::Value = serde_json::from_str(&snapshot).unwrap();
|
||||
let persisted = serde_json::to_string(&persisted).unwrap();
|
||||
let persisted = persisted.as_str();
|
||||
let persisted: serde_json::Value = serde_json::from_str(&persisted).unwrap();
|
||||
eprintln!("PERSISTED | {:?}", &persisted);
|
||||
|
||||
let stage = self.stage()?;
|
||||
let stage = serde_json::to_string(&stage).unwrap();
|
||||
let stage: serde_json::Value = serde_json::from_str(stage.as_str()).unwrap();
|
||||
eprintln!(" STAGE | {:?}", &stage);
|
||||
|
||||
let patches = json_patch::diff(&snapshot, &stage);
|
||||
let patches = json_patch::diff(&persisted, &stage);
|
||||
eprintln!(" PATCHES | {:?}", &patches);
|
||||
let deltas: Vec<Delta> = patches
|
||||
.iter()
|
||||
.map(|patch| patch.clone())
|
||||
|
|
@ -234,7 +237,7 @@ mod test_file {
|
|||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_file_empty() {
|
||||
fn test_file_empty_empty() {
|
||||
tests::with_dir(|d| {
|
||||
tests::write_file(&d, "plain", "[]");
|
||||
|
||||
|
|
@ -244,17 +247,38 @@ mod test_file {
|
|||
assert_eq!(0, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "[]");
|
||||
|
||||
f.stage_persisted().unwrap();
|
||||
assert_eq!(0, f.events().unwrap().0.len());
|
||||
assert_eq!(0, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "[]");
|
||||
|
||||
f.persist_stage().unwrap();
|
||||
assert_eq!(0, f.events().unwrap().0.len());
|
||||
assert_eq!(0, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "[]");
|
||||
|
||||
assert!(false, "not impl");
|
||||
f.stage_persisted().unwrap();
|
||||
assert_eq!(0, f.events().unwrap().0.len());
|
||||
assert_eq!(0, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "[]");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_file_empty_stage() {
|
||||
tests::with_dir(|d| {
|
||||
tests::write_file(&d, "plain", "[hello, world]");
|
||||
|
||||
let f = File::new(&d.path().join("plain").to_str().unwrap().to_string());
|
||||
|
||||
assert_eq!(0, f.events().unwrap().0.len());
|
||||
assert_eq!(2, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "[hello, world]");
|
||||
|
||||
f.persist_stage().unwrap();
|
||||
assert_eq!(2, f.events().unwrap().0.len());
|
||||
assert_eq!(2, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "[hello, world]");
|
||||
|
||||
f.stage_persisted().unwrap();
|
||||
assert_eq!(2, f.events().unwrap().0.len());
|
||||
assert_eq!(2, f.stage().unwrap().len());
|
||||
tests::file_contains(&d, "plain", "- hello\n- world");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -463,8 +487,8 @@ mod tests {
|
|||
}
|
||||
|
||||
pub fn file_contains(d: &tempdir::TempDir, fname: &str, content: &str) {
|
||||
let mut f = std::fs::File::create(d.path().join(&fname)).unwrap();
|
||||
writeln!(f, "{}", &content).unwrap();
|
||||
f.sync_all().unwrap();
|
||||
let p = d.path().join(&fname);
|
||||
let file_content = std::fs::read_to_string(p).unwrap();
|
||||
assert!(file_content.contains(content));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue