Add support for Delete operations

This commit is contained in:
Dustin J. Mitchell 2019-12-29 13:16:42 -05:00
parent e83bdc28cd
commit 41acb1fa1e
5 changed files with 175 additions and 51 deletions

View file

@ -16,6 +16,7 @@ fn uuid_strategy() -> impl Strategy<Value = Uuid> {
fn operation_strategy() -> impl Strategy<Value = Operation> {
prop_oneof![
uuid_strategy().prop_map(|uuid| Operation::Create { uuid }),
uuid_strategy().prop_map(|uuid| Operation::Delete { uuid }),
(uuid_strategy(), "(title|project|status)").prop_map(|(uuid, property)| {
Operation::Update {
uuid,
@ -28,32 +29,50 @@ fn operation_strategy() -> impl Strategy<Value = Operation> {
}
proptest! {
#![proptest_config(ProptestConfig {
cases: 1024, .. ProptestConfig::default()
})]
#[test]
// check that the two operation sequences have the same effect, enforcing the invariant of
// the transform function.
fn transform_invariant_holds(o1 in operation_strategy(), o2 in operation_strategy()) {
let (o1p, o2p) = Operation::transform(o1.clone(), o2.clone());
// check that the two operation sequences have the same effect, enforcing the invariant of
// the transform function. This needs some care as if either of the operations is
// an Update then we must ensure the task already exists in the DB.
let mut db1 = DB::new();
// Ensure that any expected tasks already exist
if let Operation::Update{ ref uuid, .. } = o1 {
db1.apply(Operation::Create{uuid: uuid.clone()});
let _ = db1.apply(Operation::Create{uuid: uuid.clone()});
}
if let Operation::Update{ ref uuid, .. } = o2 {
db1.apply(Operation::Create{uuid: uuid.clone()});
let _ = db1.apply(Operation::Create{uuid: uuid.clone()});
}
if let Operation::Delete{ ref uuid } = o1 {
let _ = db1.apply(Operation::Create{uuid: uuid.clone()});
}
if let Operation::Delete{ ref uuid } = o2 {
let _ = db1.apply(Operation::Create{uuid: uuid.clone()});
}
let mut db2 = db1.clone();
db1.apply(o1);
if let Some(o) = o2p {
db1.apply(o);
// if applying the initial operations fail, that indicates the operation was invalid
// in the base state, so consider the case successful.
if let Err(_) = db1.apply(o1) {
return Ok(());
}
if let Err(_) = db2.apply(o2) {
return Ok(());
}
if let Some(o) = o2p {
db1.apply(o).map_err(|e| TestCaseError::Fail(format!("Applying to db1: {}", e).into()))?;
}
db2.apply(o2);
if let Some(o) = o1p {
db2.apply(o);
db2.apply(o).map_err(|e| TestCaseError::Fail(format!("Applying to db2: {}", e).into()))?;
}
assert_eq!(db1.tasks(), db2.tasks());
}

View file

@ -14,22 +14,24 @@ fn test_sync() {
// make some changes in parallel to db1 and db2..
let uuid1 = Uuid::new_v4();
db1.apply(Operation::Create { uuid: uuid1 });
db1.apply(Operation::Create { uuid: uuid1 }).unwrap();
db1.apply(Operation::Update {
uuid: uuid1,
property: "title".into(),
value: Some("my first task".into()),
timestamp: Utc::now(),
});
})
.unwrap();
let uuid2 = Uuid::new_v4();
db2.apply(Operation::Create { uuid: uuid2 });
db2.apply(Operation::Create { uuid: uuid2 }).unwrap();
db2.apply(Operation::Update {
uuid: uuid2,
property: "title".into(),
value: Some("my second task".into()),
timestamp: Utc::now(),
});
})
.unwrap();
// and synchronize those around
db1.sync("me", &mut server);
@ -43,13 +45,15 @@ fn test_sync() {
property: "priority".into(),
value: Some("H".into()),
timestamp: Utc::now(),
});
})
.unwrap();
db2.apply(Operation::Update {
uuid: uuid2,
property: "project".into(),
value: Some("personal".into()),
timestamp: Utc::now(),
});
})
.unwrap();
// and synchronize those around
db1.sync("me", &mut server);