Finish pogreb tests and fix some issues
This commit is contained in:
parent
6497e461bd
commit
9c1c1e609d
@ -62,5 +62,17 @@ func (s spaceKeys) HeaderKey() []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isRootIdKey(key string) bool {
|
func isRootIdKey(key string) bool {
|
||||||
return strings.HasPrefix(key, "t/") && strings.HasSuffix(key, "heads")
|
return strings.HasPrefix(key, "t/") && strings.HasSuffix(key, "/heads")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRootId(key string) string {
|
||||||
|
prefixLen := 2 // len("t/")
|
||||||
|
suffixLen := 6 // len("/heads")
|
||||||
|
rootLen := len(key) - suffixLen - prefixLen
|
||||||
|
sBuf := strings.Builder{}
|
||||||
|
sBuf.Grow(rootLen)
|
||||||
|
for i := prefixLen; i < prefixLen+rootLen; i++ {
|
||||||
|
sBuf.WriteByte(key[i])
|
||||||
|
}
|
||||||
|
return sBuf.String()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (fx *fixture) testListInDB(t *testing.T, store storage.ListStorage, root *aclrecordproto.RawACLRecordWithId, head string) {
|
func testListInDB(t *testing.T, store storage.ListStorage, root *aclrecordproto.RawACLRecordWithId, head string) {
|
||||||
require.Equal(t, store.ID(), root.Id)
|
require.Equal(t, store.ID(), root.Id)
|
||||||
|
|
||||||
aclRoot, err := store.Root()
|
aclRoot, err := store.Root()
|
||||||
@ -28,7 +28,7 @@ func TestListStorage_Create(t *testing.T) {
|
|||||||
aclRoot := &aclrecordproto.RawACLRecordWithId{Payload: []byte("root"), Id: "someRootId"}
|
aclRoot := &aclrecordproto.RawACLRecordWithId{Payload: []byte("root"), Id: "someRootId"}
|
||||||
listStore, err := createListStorage(fx.db, aclRoot)
|
listStore, err := createListStorage(fx.db, aclRoot)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
fx.testListInDB(t, listStore, aclRoot, aclRoot.Id)
|
testListInDB(t, listStore, aclRoot, aclRoot.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestListStorage_Methods(t *testing.T) {
|
func TestListStorage_Methods(t *testing.T) {
|
||||||
@ -43,7 +43,7 @@ func TestListStorage_Methods(t *testing.T) {
|
|||||||
defer fx.stop(t)
|
defer fx.stop(t)
|
||||||
listStore, err := newListStorage(fx.db)
|
listStore, err := newListStorage(fx.db)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
fx.testListInDB(t, listStore, aclRoot, aclRoot.Id)
|
testListInDB(t, listStore, aclRoot, aclRoot.Id)
|
||||||
|
|
||||||
t.Run("set head", func(t *testing.T) {
|
t.Run("set head", func(t *testing.T) {
|
||||||
head := "newHead"
|
head := "newHead"
|
||||||
|
|||||||
@ -88,8 +88,8 @@ func createSpaceStorage(rootPath string, payload spacestorage.SpaceStorageCreate
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
log.With(zap.String("id", payload.SpaceHeaderWithId.Id), zap.Error(err)).Warn("failed to create storage")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.With(zap.String("id", payload.SpaceHeaderWithId.Id), zap.Error(err)).Warn("failed to create storage")
|
||||||
db.Close()
|
db.Close()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@ -156,13 +156,13 @@ func (s *spaceStorage) SpaceHeader() (header *spacesyncproto.RawSpaceHeaderWithI
|
|||||||
func (s *spaceStorage) StoredIds() (ids []string, err error) {
|
func (s *spaceStorage) StoredIds() (ids []string, err error) {
|
||||||
index := s.objDb.Items()
|
index := s.objDb.Items()
|
||||||
|
|
||||||
key, val, err := index.Next()
|
key, _, err := index.Next()
|
||||||
for err == nil {
|
for err == nil {
|
||||||
strKey := string(key)
|
strKey := string(key)
|
||||||
if isRootIdKey(strKey) {
|
if isRootIdKey(strKey) {
|
||||||
ids = append(ids, string(val))
|
ids = append(ids, getRootId(strKey))
|
||||||
}
|
}
|
||||||
key, val, err = index.Next()
|
key, _, err = index.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != pogreb.ErrIterationDone {
|
if err != pogreb.ErrIterationDone {
|
||||||
|
|||||||
104
node/storage/spacestorage_test.go
Normal file
104
node/storage/spacestorage_test.go
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto"
|
||||||
|
spacestorage "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/storage"
|
||||||
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func spaceTestPayload() spacestorage.SpaceStorageCreatePayload {
|
||||||
|
header := &spacesyncproto.RawSpaceHeaderWithId{
|
||||||
|
RawHeader: []byte("header"),
|
||||||
|
Id: "headerId",
|
||||||
|
}
|
||||||
|
aclRoot := &aclrecordproto.RawACLRecordWithId{
|
||||||
|
Payload: []byte("aclRoot"),
|
||||||
|
Id: "aclRootId",
|
||||||
|
}
|
||||||
|
return spacestorage.SpaceStorageCreatePayload{
|
||||||
|
RecWithId: aclRoot,
|
||||||
|
SpaceHeaderWithId: header,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testSpaceInDB(t *testing.T, store spacestorage.SpaceStorage, payload spacestorage.SpaceStorageCreatePayload) {
|
||||||
|
header, err := store.SpaceHeader()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, payload.SpaceHeaderWithId, header)
|
||||||
|
|
||||||
|
aclStorage, err := store.ACLStorage()
|
||||||
|
require.NoError(t, err)
|
||||||
|
testListInDB(t, aclStorage, payload.RecWithId, payload.RecWithId.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSpaceStorage_Create(t *testing.T) {
|
||||||
|
dir, err := os.MkdirTemp("", "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
payload := spaceTestPayload()
|
||||||
|
store, err := createSpaceStorage(dir, payload)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, store.Close())
|
||||||
|
}()
|
||||||
|
|
||||||
|
testSpaceInDB(t, store, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSpaceStorage_NewAndCreateTree(t *testing.T) {
|
||||||
|
dir, err := os.MkdirTemp("", "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
payload := spaceTestPayload()
|
||||||
|
store, err := createSpaceStorage(dir, payload)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, store.Close())
|
||||||
|
|
||||||
|
store, err = newSpaceStorage(dir, payload.SpaceHeaderWithId.Id)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, store.Close())
|
||||||
|
}()
|
||||||
|
testSpaceInDB(t, store, payload)
|
||||||
|
|
||||||
|
t.Run("create tree and get tree", func(t *testing.T) {
|
||||||
|
payload := treeTestPayload()
|
||||||
|
treeStore, err := store.CreateTreeStorage(payload)
|
||||||
|
require.NoError(t, err)
|
||||||
|
testTreePayloadInDB(t, treeStore, payload)
|
||||||
|
|
||||||
|
otherStore, err := store.TreeStorage(payload.RootRawChange.Id)
|
||||||
|
require.NoError(t, err)
|
||||||
|
testTreePayloadInDB(t, otherStore, payload)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSpaceStorage_StoredIds(t *testing.T) {
|
||||||
|
dir, err := os.MkdirTemp("", "")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
payload := spaceTestPayload()
|
||||||
|
store, err := createSpaceStorage(dir, payload)
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, store.Close())
|
||||||
|
}()
|
||||||
|
|
||||||
|
n := 5
|
||||||
|
var ids []string
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
treePayload := treeTestPayload()
|
||||||
|
treePayload.RootRawChange.Id += strconv.Itoa(i)
|
||||||
|
ids = append(ids, treePayload.RootRawChange.Id)
|
||||||
|
_, err := store.CreateTreeStorage(treePayload)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
storedIds, err := store.StoredIds()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, ids, storedIds)
|
||||||
|
}
|
||||||
@ -38,7 +38,11 @@ func (fx *fixture) open(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fx *fixture) testTreePayloadInDB(t *testing.T, store storage.TreeStorage, payload storage.TreeStorageCreatePayload) {
|
func (fx *fixture) stop(t *testing.T) {
|
||||||
|
require.NoError(t, fx.db.Close())
|
||||||
|
}
|
||||||
|
|
||||||
|
func testTreePayloadInDB(t *testing.T, store storage.TreeStorage, payload storage.TreeStorageCreatePayload) {
|
||||||
require.Equal(t, payload.RootRawChange.Id, store.ID())
|
require.Equal(t, payload.RootRawChange.Id, store.ID())
|
||||||
|
|
||||||
root, err := store.Root()
|
root, err := store.Root()
|
||||||
@ -57,10 +61,6 @@ func (fx *fixture) testTreePayloadInDB(t *testing.T, store storage.TreeStorage,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fx *fixture) stop(t *testing.T) {
|
|
||||||
require.NoError(t, fx.db.Close())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTreeStorage_Create(t *testing.T) {
|
func TestTreeStorage_Create(t *testing.T) {
|
||||||
fx := newFixture(t)
|
fx := newFixture(t)
|
||||||
fx.open(t)
|
fx.open(t)
|
||||||
@ -69,7 +69,7 @@ func TestTreeStorage_Create(t *testing.T) {
|
|||||||
payload := treeTestPayload()
|
payload := treeTestPayload()
|
||||||
store, err := createTreeStorage(fx.db, payload)
|
store, err := createTreeStorage(fx.db, payload)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
fx.testTreePayloadInDB(t, store, payload)
|
testTreePayloadInDB(t, store, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTreeStorage_Methods(t *testing.T) {
|
func TestTreeStorage_Methods(t *testing.T) {
|
||||||
@ -84,7 +84,7 @@ func TestTreeStorage_Methods(t *testing.T) {
|
|||||||
defer fx.stop(t)
|
defer fx.stop(t)
|
||||||
store, err := newTreeStorage(fx.db, payload.RootRawChange.Id)
|
store, err := newTreeStorage(fx.db, payload.RootRawChange.Id)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
fx.testTreePayloadInDB(t, store, payload)
|
testTreePayloadInDB(t, store, payload)
|
||||||
|
|
||||||
t.Run("update heads", func(t *testing.T) {
|
t.Run("update heads", func(t *testing.T) {
|
||||||
newHeads := []string{"a", "b"}
|
newHeads := []string{"a", "b"}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user