Add first test for object tree
This commit is contained in:
parent
b294d13254
commit
e4686d93e5
@ -299,9 +299,14 @@ func (ot *objectTree) addRawChanges(ctx context.Context, rawChanges ...*aclpb.Ra
|
|||||||
ot.difSnapshotBuf = ot.difSnapshotBuf[:0]
|
ot.difSnapshotBuf = ot.difSnapshotBuf[:0]
|
||||||
ot.newSnapshotsBuf = ot.newSnapshotsBuf[: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
|
// this will be returned to client, so we shouldn't use buffer here
|
||||||
prevHeadsCopy := make([]string, 0, len(ot.tree.Heads()))
|
prevHeadsCopy := headsCopy()
|
||||||
copy(prevHeadsCopy, ot.tree.Heads())
|
|
||||||
|
|
||||||
// filtering changes, verifying and unmarshalling them
|
// filtering changes, verifying and unmarshalling them
|
||||||
for idx, ch := range rawChanges {
|
for idx, ch := range rawChanges {
|
||||||
@ -332,12 +337,6 @@ func (ot *objectTree) addRawChanges(ctx context.Context, rawChanges ...*aclpb.Ra
|
|||||||
return
|
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
|
// returns changes that we added to the tree
|
||||||
getAddedChanges := func() []*aclpb.RawChange {
|
getAddedChanges := func() []*aclpb.RawChange {
|
||||||
var added []*aclpb.RawChange
|
var added []*aclpb.RawChange
|
||||||
|
|||||||
@ -1,12 +1,14 @@
|
|||||||
package tree
|
package tree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/aclchanges/aclpb"
|
"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/list"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/acl/storage"
|
"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/pkg/acl/testutils/acllistbuilder"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/util/keys/asymmetric/signingkey"
|
||||||
"github.com/gogo/protobuf/proto"
|
"github.com/gogo/protobuf/proto"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -77,7 +79,7 @@ func prepareACLList(t *testing.T) list.ACLList {
|
|||||||
return aclList
|
return aclList
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestObjectTree_Build(t *testing.T) {
|
func TestObjectTree(t *testing.T) {
|
||||||
aclList := prepareACLList(t)
|
aclList := prepareACLList(t)
|
||||||
changeCreator := &mockChangeCreator{}
|
changeCreator := &mockChangeCreator{}
|
||||||
treeStorage := changeCreator.createNewTreeStorage("treeId", aclList.ID(), aclList.Head().Id, "0")
|
treeStorage := changeCreator.createNewTreeStorage("treeId", aclList.ID(), aclList.Head().Id, "0")
|
||||||
@ -91,6 +93,52 @@ func TestObjectTree_Build(t *testing.T) {
|
|||||||
aclList: aclList,
|
aclList: aclList,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := buildObjectTree(deps)
|
// check build
|
||||||
|
objTree, err := buildObjectTree(deps)
|
||||||
require.NoError(t, err, "building tree should be without error")
|
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…
x
Reference in New Issue
Block a user