Skip to content

Commit

Permalink
fix(diff): handle edge case diffing empty against non-empty (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
frrist authored Jun 22, 2021
1 parent 506754a commit 0b588ae
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
7 changes: 7 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ func Diff(ctx context.Context, prevBs, curBs cbor.IpldStore, prev, cur cid.Cid,
height: curAmt.height,
}

// edge case of diffing an empty AMT against non-empty
if prevAmt.count == 0 && curAmt.count != 0 {
return addAll(ctx, curCtx, curAmt.node, 0)
}
if prevAmt.count != 0 && curAmt.count == 0 {
return removeAll(ctx, prevCtx, prevAmt.node, 0)
}
return diffNode(ctx, prevCtx, curCtx, prevAmt.node, curAmt.node, 0)
}

Expand Down
62 changes: 60 additions & 2 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"strconv"
"testing"

"github.com/stretchr/testify/assert"

cbor "github.com/ipfs/go-ipld-cbor"
"github.com/stretchr/testify/assert"
)

type expectedChange struct {
Expand Down Expand Up @@ -102,6 +101,65 @@ func TestSimpleAdd(t *testing.T) {
ec.assertExpectation(t, cs[0])
}

func TestDiffEmptyStateWithNonEmptyState(t *testing.T) {
t.Run("Removed values", func(t *testing.T) {
prevBs := cbor.NewCborStore(newMockBlocks())
curBs := cbor.NewCborStore(newMockBlocks())
ctx := context.Background()

prev, err := NewAMT(prevBs)
assert.NoError(t, err)

cur, err := NewAMT(curBs)
assert.NoError(t, err)
assertCount(t, cur, 0)

assertSet(t, prev, 2, "foo")
assertGet(ctx, t, prev, 2, "foo")
assertCount(t, prev, 1)

cs := diffAndAssertLength(ctx, t, prevBs, curBs, prev, cur, 1)

ec := expectedChange{
Type: Remove,
Key: 2,
Before: "foo",
After: "",
}

ec.assertExpectation(t, cs[0])
})

t.Run("Added values", func(t *testing.T) {
prevBs := cbor.NewCborStore(newMockBlocks())
curBs := cbor.NewCborStore(newMockBlocks())
ctx := context.Background()

prev, err := NewAMT(prevBs)
assert.NoError(t, err)
assertCount(t, prev, 0)

cur, err := NewAMT(curBs)
assert.NoError(t, err)

assertSet(t, cur, 2, "foo")
assertGet(ctx, t, cur, 2, "foo")
assertCount(t, cur, 1)

cs := diffAndAssertLength(ctx, t, prevBs, curBs, prev, cur, 1)

ec := expectedChange{
Type: Add,
Key: 2,
Before: "",
After: "foo",
}

ec.assertExpectation(t, cs[0])

})
}

func TestSimpleRemove(t *testing.T) {
prevBs := cbor.NewCborStore(newMockBlocks())
curBs := cbor.NewCborStore(newMockBlocks())
Expand Down

0 comments on commit 0b588ae

Please sign in to comment.