From e4686d93e5c49432dca1251c27ea627dba1c807e Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Tue, 6 Sep 2022 20:58:41 +0200 Subject: [PATCH] Add first test for object tree --- pkg/acl/tree/objecttree.go | 15 +++++----- pkg/acl/tree/objecttree_test.go | 52 +++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/pkg/acl/tree/objecttree.go b/pkg/acl/tree/objecttree.go index fcb57d3b..74f92b10 100644 --- a/pkg/acl/tree/objecttree.go +++ b/pkg/acl/tree/objecttree.go @@ -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 diff --git a/pkg/acl/tree/objecttree_test.go b/pkg/acl/tree/objecttree_test.go index 075d9d21..ea7bddfd 100644 --- a/pkg/acl/tree/objecttree_test.go +++ b/pkg/acl/tree/objecttree_test.go @@ -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") + } + }) }