From ac6344d03b9913186f2f358cb11aa6928bb3bbdf Mon Sep 17 00:00:00 2001 From: Issac Kelly Date: Wed, 30 Nov 2022 10:14:07 -0800 Subject: [PATCH] Test is failing on previously fixed change in main --- autosurgeon/src/reconcile.rs | 173 ++++++++++++++++++++++++++++++----- 1 file changed, 148 insertions(+), 25 deletions(-) diff --git a/autosurgeon/src/reconcile.rs b/autosurgeon/src/reconcile.rs index c382f48..0e6cc70 100644 --- a/autosurgeon/src/reconcile.rs +++ b/autosurgeon/src/reconcile.rs @@ -956,6 +956,11 @@ pub fn hydrate_key<'a, D: ReadDoc, H: crate::Hydrate + Clone>( mod tests { use super::*; use automerge_test::{assert_doc, list, map}; + use autosurgeon_derive::Reconcile; + + struct ContactBook { + contacts: Vec + } struct Contact { name: String, @@ -989,6 +994,15 @@ mod tests { } } + impl Reconcile for ContactBook { + type Key<'a> = NoKey; + fn reconcile(&self, mut reconciler: R) -> Result<(), R::Error> { + let mut map = reconciler.map()?; + map.put("contacts", &self.contacts)?; + Ok(()) + } + } + #[test] fn basic_reconciliation() { let mut bob = Contact { @@ -1073,10 +1087,11 @@ mod tests { ); } - fn test_prop_with_transaction() { + #[test] + fn test_with_transaction_commit_log() { let mut doc = automerge::Automerge::new(); - let mut tx = doc.transaction(); + // Create Bob, this should result in one change on the document. let bob = Contact { name: "bob".to_string(), id: 1, @@ -1085,38 +1100,146 @@ mod tests { line_two: "line two".to_string(), }], }; - reconcile(&mut tx, &bob).unwrap(); - tx.commit(); + let _tx_res = doc + .transact_with::<_, _, automerge::AutomergeError, _>( + |_| automerge::transaction::CommitOptions::default().with_message("Reconcile Bob".to_owned()), + |tx| { + reconcile(tx, &bob).unwrap(); + Ok(()) + }); + let changes = doc.get_changes(&[]).unwrap(); + assert_eq!(changes.len(), 1); + + + // Take two should create no changes + let bob = Contact { + name: "bob".to_string(), + id: 1, + addresses: vec![Address { + line_one: "line one".to_string(), + line_two: "line two".to_string(), + }], + }; + let _tx_res = doc + .transact_with::<_, _, automerge::AutomergeError, _>( + |_| automerge::transaction::CommitOptions::default().with_message("Reconcile Bob Again, Equal".to_owned()), + |tx| { + reconcile(tx, &bob).unwrap(); + Ok(()) + }); + let changes = doc.get_changes(&[]).unwrap(); + assert_eq!(changes.len(), 1); + + } + + #[test] + fn test_reconcile_prop_behavior() { + let mut doc = automerge::Automerge::new(); + + // Take two should create no changes + let bob = Contact { + name: "bob".to_string(), + id: 1, + addresses: vec![Address { + line_one: "line one".to_string(), + line_two: "line two".to_string(), + }], + }; + let alice = Contact { + name: "alice".to_string(), + id: 2, + addresses: vec![Address { + line_one: "line one".to_string(), + line_two: "line two".to_string(), + }], + }; + + let contacts = ContactBook { + contacts: vec![bob, alice] + }; + + let _tx_res = doc + .transact_with::<_, _, automerge::AutomergeError, _>( + |_| automerge::transaction::CommitOptions::default().with_message("Set Contact Book".to_owned()), + |tx| { + reconcile(tx, &contacts).unwrap(); + Ok(()) + }); + let changes = doc.get_changes(&[]).unwrap(); + assert_eq!(changes.len(), 1); assert_doc!( &doc, - map! { - "name" => { "bob" }, - "id" => { 1_u64 }, - "addresses" => { list!{ - { map! { - "line_one" => { "line one" }, - "line_two" => { "line two" }, - }} - } + {map! { + "contacts" => {list! { + {map! { + "name" => { "bob" }, + "id" => { 1_u64 }, + "addresses" => { list!{ + { map! { + "line_one" => { "line one" }, + "line_two" => { "line two" }, + }} + }} + }}, { map! { + "name" => { "alice" }, + "id" => { 2_u64 }, + "addresses" => { list!{ + { map! { + "line_one" => { "line one" }, + "line_two" => { "line two" }, + }} + }} + }} + }} }} ); - let mut tx = doc.transaction(); - reconcile_prop(&mut tx, automerge::ROOT, "name", "Alice".to_owned()).unwrap(); - tx.commit(); + // Let's change alice and reconcile_prop + let alice = Contact { + name: "alice".to_string(), + id: 2, + addresses: vec![Address { + line_one: "33 Rockefeller Center".to_string(), + line_two: "New York".to_string(), + }], + }; + let _tx_res = doc + .transact_with::<_, _, automerge::AutomergeError, _>( + |_| automerge::transaction::CommitOptions::default().with_message("Set Contact Book".to_owned()), + |tx| { + + reconcile_prop(tx, automerge::ROOT, "contacts", alice).unwrap(); + Ok(()) + }); + let changes = doc.get_changes(&[]).unwrap(); + assert_eq!(changes.len(), 2); + assert_doc!( &doc, - map! { - "name" => { "alice" }, - "id" => { 1_u64 }, - "addresses" => { list!{ - { map! { - "line_one" => { "line one" }, - "line_two" => { "line two" }, - }} - } + {map! { + "contacts" => {list! { + {map! { + "name" => { "bob" }, + "id" => { 1_u64 }, + "addresses" => { list!{ + { map! { + "line_one" => { "line one" }, + "line_two" => { "line two" }, + }} + }} + }}, { map! { + "name" => { "alice" }, + "id" => { 2_u64 }, + "addresses" => { list!{ + { map! { + "line_one" => { "33 Rockefeller Center" }, + "line_two" => { "New York" }, + }} + }} + }} + }} }} );