more test

master
Bel LaPointe 2025-11-11 16:40:38 -07:00
parent c34ad15ba1
commit b5b794817c
1 changed files with 53 additions and 5 deletions

View File

@ -294,6 +294,7 @@ impl Events {
Ok(f) => {
for line in std::io::BufReader::new(f).lines() {
let line = line.unwrap();
let line = line.trim();
if line.len() > 0 {
let delta = match serde_json::from_str(&line) {
Ok(v) => Ok(v),
@ -307,7 +308,9 @@ impl Events {
Err(msg) => Err(format!("failed to read {}: {}", &log, msg)),
}?;
}
result.sort_by(|a, b| a.ts.cmp(&b.ts));
Ok(Events(result))
}
@ -356,12 +359,12 @@ mod test_events {
use super::*;
#[test]
fn test_events() {
fn test_events_oplog_to_snapshot_one() {
let d = tempdir::TempDir::new("").unwrap();
test_file(&d, "plain", "- persisted\n- stage only");
test_file(
&d,
".plain.log",
".plain.some_host",
r#"
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
"#,
@ -370,12 +373,57 @@ mod test_events {
let events = Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
assert_eq!(1, events.0.len(), "events: {:?}", events);
assert!(false, "not impl");
let snapshot = events.snapshot().unwrap();
assert_eq!(1, snapshot.len());
assert_eq!(
serde_yaml::Value::String("persisted".to_string()),
snapshot[0].0
);
}
#[test]
fn test_events_oplog_to_snapshot_complex() {
let d = tempdir::TempDir::new("").unwrap();
test_file(&d, "plain", "- ignored");
test_file(
&d,
".plain.some_host",
r#"
{"ts":1, "patch":{"op":"replace", "path":"", "value":["persisted"]}}
{"ts":3, "patch":{"op":"add", "path":"/-", "value":"persisted 3"}}
{"ts":2, "patch":{"op":"add", "path":"/-", "value":"persisted 2"}}
{"ts":4, "patch":{"op":"add", "path":"/-", "value":"persisted 4"}}
{"ts":5, "patch":{"op":"add", "path":"/-", "value":"persisted 5"}}
{"ts":6, "patch":{"op":"replace", "path":"/4", "value":"persisted 5'"}}
{"ts":7, "patch":{"op":"remove", "path":"/3"}}
"#,
);
let events = Events::new(&d.path().join("plain").to_str().unwrap().to_string()).unwrap();
let snapshot = events.snapshot().unwrap();
assert_eq!(4, snapshot.len());
assert_eq!(
serde_yaml::Value::String("persisted".to_string()),
snapshot[0].0
);
assert_eq!(
serde_yaml::Value::String("persisted 2".to_string()),
snapshot[1].0
);
assert_eq!(
serde_yaml::Value::String("persisted 3".to_string()),
snapshot[2].0
);
assert_eq!(
serde_yaml::Value::String("persisted 5'".to_string()),
snapshot[3].0
);
}
}
fn test_file(d: &tempdir::TempDir, fname: &str, content: &str) {
let mut f = std::fs::File::create(d.path().join("plain")).unwrap();
writeln!(f, "- persisted\n- stage only").unwrap();
let mut f = std::fs::File::create(d.path().join(&fname)).unwrap();
writeln!(f, "{}", &content).unwrap();
f.sync_all().unwrap();
}