Skip to content

Commit

Permalink
Test is failing on previously fixed change in main
Browse files Browse the repository at this point in the history
  • Loading branch information
issackelly committed Nov 30, 2022
1 parent 183b8db commit ac6344d
Showing 1 changed file with 148 additions and 25 deletions.
173 changes: 148 additions & 25 deletions autosurgeon/src/reconcile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Contact>
}

struct Contact {
name: String,
Expand Down Expand Up @@ -989,6 +994,15 @@ mod tests {
}
}

impl Reconcile for ContactBook {
type Key<'a> = NoKey;
fn reconcile<R: Reconciler>(&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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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" },
}}
}}
}}
}}
}}
);

Expand Down

0 comments on commit ac6344d

Please sign in to comment.