use serial_test::serial;
use tempfile::tempdir;
use umadb_core::db::UmaDb;
use umadb_dcb::{DcbAppendCondition, DcbEvent, DcbEventStoreSync, TrackingInfo};
#[test]
#[serial]
fn append_batch_with_per_item_tracking_enforces_monotonicity() {
let temp_dir = tempdir().unwrap();
let db_path = temp_dir.path().join("tracking-batch.db");
let store = UmaDb::new(&db_path).unwrap();
let mk = |t: &str| DcbEvent {
event_type: t.to_string(),
data: b"x".to_vec(),
tags: vec!["t".into()],
uuid: None,
metadata: Vec::new(),
};
let items = vec![
(
vec![mk("A1")],
None::<DcbAppendCondition>,
Some(TrackingInfo {
source: "S".into(),
position: 5,
}),
),
(
vec![mk("A2")],
None,
Some(TrackingInfo {
source: "S".into(),
position: 5,
}),
),
(
vec![mk("A3")],
None,
Some(TrackingInfo {
source: "S".into(),
position: 6,
}),
),
];
let results = store.append_batch(items).unwrap();
assert_eq!(3, results.len());
match &results[0] {
Ok(p) => assert_eq!(1, *p),
other => panic!("unexpected: {:?}", other),
}
match &results[1] {
Err(e) => assert!(matches!(e, umadb_dcb::DcbError::IntegrityError(_))),
other => panic!("expected IntegrityError, got {:?}", other),
}
match &results[2] {
Ok(p) => assert_eq!(2, *p),
other => panic!("unexpected: {:?}", other),
}
assert_eq!(Some(6), store.get_tracking_info("S").unwrap());
assert_eq!(Some(2), store.head().unwrap());
}