Merge pull request from jeffa5/experiment-del-nothing

Add failing tests for deleting nothing
This commit is contained in:
Andrew Jeffery 2022-03-09 11:15:22 +00:00 committed by GitHub
commit 3cff67002a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions
automerge/src

View file

@ -1247,4 +1247,41 @@ mod tests {
assert_eq!(doc.value(&map1, "c").unwrap().unwrap().0, Value::int(3));
}
#[test]
fn delete_nothing_in_map_is_noop() {
let mut doc = Automerge::new();
let mut tx = doc.transaction();
// deleting a missing key in a map should just be a noop
assert!(tx.del(ROOT, "a").is_ok());
tx.commit();
let last_change = doc.get_last_local_change().unwrap();
assert_eq!(last_change.len(), 0);
let bytes = doc.save();
assert!(Automerge::load(&bytes).is_ok());
let mut tx = doc.transaction();
tx.set(ROOT, "a", 1).unwrap();
tx.commit();
let last_change = doc.get_last_local_change().unwrap();
assert_eq!(last_change.len(), 1);
let mut tx = doc.transaction();
// a real op
tx.del(ROOT, "a").unwrap();
// a no-op
tx.del(ROOT, "a").unwrap();
tx.commit();
let last_change = doc.get_last_local_change().unwrap();
assert_eq!(last_change.len(), 1);
}
#[test]
fn delete_nothing_in_list_returns_error() {
let mut doc = Automerge::new();
let mut tx = doc.transaction();
// deleting an element in a list that does not exist is an error
assert!(tx.del(ROOT, 0).is_err());
}
}

View file

@ -225,6 +225,11 @@ impl TransactionInner {
let prop = doc.ops.m.props.cache(prop);
let query = doc.ops.search(obj, query::Prop::new(prop));
// no key present to delete
if query.ops.is_empty() && action == OpType::Del {
return Ok(None);
}
if query.ops.len() == 1 && query.ops[0].is_noop(&action) {
return Ok(None);
}