mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-11 10:18:08 +09:00
Add first test for object tree
This commit is contained in:
parent
b294d13254
commit
e4686d93e5
2 changed files with 57 additions and 10 deletions
|
@ -299,9 +299,14 @@ func (ot *objectTree) addRawChanges(ctx context.Context, rawChanges ...*aclpb.Ra
|
|||
ot.difSnapshotBuf = ot.difSnapshotBuf[:0]
|
||||
ot.newSnapshotsBuf = ot.newSnapshotsBuf[:0]
|
||||
|
||||
headsCopy := func() []string {
|
||||
newHeads := make([]string, 0, len(ot.tree.Heads()))
|
||||
newHeads = append(newHeads, ot.tree.Heads()...)
|
||||
return newHeads
|
||||
}
|
||||
|
||||
// this will be returned to client, so we shouldn't use buffer here
|
||||
prevHeadsCopy := make([]string, 0, len(ot.tree.Heads()))
|
||||
copy(prevHeadsCopy, ot.tree.Heads())
|
||||
prevHeadsCopy := headsCopy()
|
||||
|
||||
// filtering changes, verifying and unmarshalling them
|
||||
for idx, ch := range rawChanges {
|
||||
|
@ -332,12 +337,6 @@ func (ot *objectTree) addRawChanges(ctx context.Context, rawChanges ...*aclpb.Ra
|
|||
return
|
||||
}
|
||||
|
||||
headsCopy := func() []string {
|
||||
newHeads := make([]string, 0, len(ot.tree.Heads()))
|
||||
copy(newHeads, ot.tree.Heads())
|
||||
return newHeads
|
||||
}
|
||||
|
||||
// returns changes that we added to the tree
|
||||
getAddedChanges := func() []*aclpb.RawChange {
|
||||
var added []*aclpb.RawChange
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package tree
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/list"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/testutils/acllistbuilder"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
@ -77,7 +79,7 @@ func prepareACLList(t *testing.T) list.ACLList {
|
|||
return aclList
|
||||
}
|
||||
|
||||
func TestObjectTree_Build(t *testing.T) {
|
||||
func TestObjectTree(t *testing.T) {
|
||||
aclList := prepareACLList(t)
|
||||
changeCreator := &mockChangeCreator{}
|
||||
treeStorage := changeCreator.createNewTreeStorage("treeId", aclList.ID(), aclList.Head().Id, "0")
|
||||
|
@ -91,6 +93,52 @@ func TestObjectTree_Build(t *testing.T) {
|
|||
aclList: aclList,
|
||||
}
|
||||
|
||||
_, err := buildObjectTree(deps)
|
||||
// check build
|
||||
objTree, err := buildObjectTree(deps)
|
||||
require.NoError(t, err, "building tree should be without error")
|
||||
|
||||
// check tree iterate
|
||||
var iterChangesId []string
|
||||
err = objTree.Iterate(nil, func(change *Change) bool {
|
||||
iterChangesId = append(iterChangesId, change.Id)
|
||||
return true
|
||||
})
|
||||
require.NoError(t, err, "iterate should be without error")
|
||||
assert.Equal(t, []string{"0"}, iterChangesId)
|
||||
|
||||
t.Run("add simple", func(t *testing.T) {
|
||||
rawChanges := []*aclpb.RawChange{
|
||||
changeCreator.createRaw("1", aclList.Head().Id, "0", false, "0"),
|
||||
changeCreator.createRaw("2", aclList.Head().Id, "0", false, "1"),
|
||||
}
|
||||
res, err := objTree.AddRawChanges(context.Background(), rawChanges...)
|
||||
require.NoError(t, err, "adding changes should be without error")
|
||||
|
||||
// check result
|
||||
assert.Equal(t, []string{"0"}, res.OldHeads)
|
||||
assert.Equal(t, []string{"2"}, res.Heads)
|
||||
assert.Equal(t, len(rawChanges), len(res.Added))
|
||||
|
||||
// check tree heads
|
||||
assert.Equal(t, []string{"2"}, objTree.Heads())
|
||||
|
||||
// check tree iterate
|
||||
var iterChangesId []string
|
||||
err = objTree.Iterate(nil, func(change *Change) bool {
|
||||
iterChangesId = append(iterChangesId, change.Id)
|
||||
return true
|
||||
})
|
||||
require.NoError(t, err, "iterate should be without error")
|
||||
assert.Equal(t, []string{"0", "1", "2"}, iterChangesId)
|
||||
|
||||
// check storage
|
||||
heads, _ := treeStorage.Heads()
|
||||
assert.Equal(t, []string{"2"}, heads)
|
||||
|
||||
for _, ch := range rawChanges {
|
||||
raw, err := treeStorage.GetRawChange(context.Background(), ch.Id)
|
||||
assert.NoError(t, err, "storage should have all the changes")
|
||||
assert.Equal(t, ch, raw, "the changes in the storage should be the same")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue