From 9b85cf2373a3f55fa41752f6ed2c83ab9f7a3918 Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Wed, 26 Oct 2022 21:26:36 +0200 Subject: [PATCH] Add badger list storage and tree storage test --- client/storage/liststorage.go | 4 +- client/storage/liststorage_test.go | 61 +++++++++++++++ client/storage/treestorage_test.go | 118 +++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+), 2 deletions(-) create mode 100644 client/storage/liststorage_test.go create mode 100644 client/storage/treestorage_test.go diff --git a/client/storage/liststorage.go b/client/storage/liststorage.go index 6fad213d..40d46e38 100644 --- a/client/storage/liststorage.go +++ b/client/storage/liststorage.go @@ -37,7 +37,7 @@ func newListStorage(spaceId string, db *badger.DB, txn *badger.Txn) (ls storage. ls = &listStorage{ db: db, - keys: newACLKeys(spaceId), + keys: keys, id: stringId, root: rootWithId, } @@ -70,7 +70,7 @@ func createListStorage(spaceId string, db *badger.DB, txn *badger.Txn, root *acl ls = &listStorage{ db: db, - keys: newACLKeys(spaceId), + keys: keys, id: root.Id, root: root, } diff --git a/client/storage/liststorage_test.go b/client/storage/liststorage_test.go new file mode 100644 index 00000000..4d8cf9b5 --- /dev/null +++ b/client/storage/liststorage_test.go @@ -0,0 +1,61 @@ +package storage + +import ( + "context" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/aclrecordproto" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/dgraph-io/badger/v3" + "github.com/stretchr/testify/require" + "testing" +) + +func testListInDB(t *testing.T, store storage.ListStorage, root *aclrecordproto.RawACLRecordWithId, head string) { + require.Equal(t, store.ID(), root.Id) + + aclRoot, err := store.Root() + require.NoError(t, err) + require.Equal(t, root, aclRoot) + + aclHead, err := store.Head() + require.NoError(t, err) + require.Equal(t, head, aclHead) +} + +func TestListStorage(t *testing.T) { + fx := newFixture(t) + fx.open(t) + defer fx.stop(t) + spaceId := "spaceId" + aclRoot := &aclrecordproto.RawACLRecordWithId{Payload: []byte("root"), Id: "someRootId"} + + fx.db.Update(func(txn *badger.Txn) error { + _, err := createListStorage(spaceId, fx.db, txn, aclRoot) + require.NoError(t, err) + return nil + }) + + var listStore storage.ListStorage + fx.db.View(func(txn *badger.Txn) (err error) { + listStore, err = newListStorage(spaceId, fx.db, txn) + require.NoError(t, err) + testListInDB(t, listStore, aclRoot, aclRoot.Id) + + return nil + }) + + t.Run("set head", func(t *testing.T) { + head := "newHead" + require.NoError(t, listStore.SetHead(head)) + aclHead, err := listStore.Head() + require.NoError(t, err) + require.Equal(t, head, aclHead) + }) + + t.Run("add raw record and get raw record", func(t *testing.T) { + newRec := &aclrecordproto.RawACLRecordWithId{Payload: []byte("rec"), Id: "someRecId"} + require.NoError(t, listStore.AddRawRecord(context.Background(), newRec)) + aclRec, err := listStore.GetRawRecord(context.Background(), newRec.Id) + require.NoError(t, err) + require.Equal(t, newRec, aclRec) + }) +} diff --git a/client/storage/treestorage_test.go b/client/storage/treestorage_test.go new file mode 100644 index 00000000..fd6062cd --- /dev/null +++ b/client/storage/treestorage_test.go @@ -0,0 +1,118 @@ +package storage + +import ( + "context" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/storage" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/pkg/acl/treechangeproto" + "github.com/dgraph-io/badger/v3" + "github.com/stretchr/testify/require" + "os" + "testing" +) + +func treeTestPayload() storage.TreeStorageCreatePayload { + rootRawChange := &treechangeproto.RawTreeChangeWithId{RawChange: []byte("some"), Id: "someRootId"} + otherChange := &treechangeproto.RawTreeChangeWithId{RawChange: []byte("some other"), Id: "otherId"} + changes := []*treechangeproto.RawTreeChangeWithId{rootRawChange, otherChange} + return storage.TreeStorageCreatePayload{ + RootRawChange: rootRawChange, + Changes: changes, + Heads: []string{rootRawChange.Id}, + } +} + +type fixture struct { + dir string + db *badger.DB +} + +func testTreePayloadInDB(t *testing.T, store storage.TreeStorage, payload storage.TreeStorageCreatePayload) { + require.Equal(t, payload.RootRawChange.Id, store.ID()) + + root, err := store.Root() + require.NoError(t, err) + require.Equal(t, root, payload.RootRawChange) + + heads, err := store.Heads() + require.NoError(t, err) + require.Equal(t, payload.Heads, heads) + + for _, ch := range payload.Changes { + dbCh, err := store.GetRawChange(context.Background(), ch.Id) + require.NoError(t, err) + require.Equal(t, ch, dbCh) + } + return +} + +func newFixture(t *testing.T) *fixture { + dir, err := os.MkdirTemp("", "") + require.NoError(t, err) + return &fixture{dir: dir} +} + +func (fx *fixture) open(t *testing.T) { + var err error + fx.db, err = badger.Open(badger.DefaultOptions(fx.dir)) + require.NoError(t, err) +} + +func (fx *fixture) stop(t *testing.T) { + require.NoError(t, fx.db.Close()) +} + +func TestTreeStorage_Create(t *testing.T) { + fx := newFixture(t) + fx.open(t) + defer fx.stop(t) + + spaceId := "spaceId" + payload := treeTestPayload() + store, err := createTreeStorage(fx.db, spaceId, payload) + require.NoError(t, err) + testTreePayloadInDB(t, store, payload) +} + +func TestTreeStorage_Methods(t *testing.T) { + fx := newFixture(t) + fx.open(t) + payload := treeTestPayload() + spaceId := "spaceId" + _, err := createTreeStorage(fx.db, spaceId, payload) + require.NoError(t, err) + fx.stop(t) + + fx.open(t) + defer fx.stop(t) + store, err := newTreeStorage(fx.db, spaceId, payload.RootRawChange.Id) + require.NoError(t, err) + testTreePayloadInDB(t, store, payload) + + t.Run("update heads", func(t *testing.T) { + newHeads := []string{"a", "b"} + require.NoError(t, store.SetHeads(newHeads)) + heads, err := store.Heads() + require.NoError(t, err) + require.Equal(t, newHeads, heads) + }) + + t.Run("add raw change, get change and has change", func(t *testing.T) { + newChange := &treechangeproto.RawTreeChangeWithId{RawChange: []byte("ab"), Id: "newId"} + require.NoError(t, store.AddRawChange(newChange)) + rawCh, err := store.GetRawChange(context.Background(), newChange.Id) + require.NoError(t, err) + require.Equal(t, newChange, rawCh) + has, err := store.HasChange(context.Background(), newChange.Id) + require.NoError(t, err) + require.True(t, has) + }) + + t.Run("get and has for unknown change", func(t *testing.T) { + incorrectId := "incorrectId" + _, err := store.GetRawChange(context.Background(), incorrectId) + require.Error(t, err) + has, err := store.HasChange(context.Background(), incorrectId) + require.NoError(t, err) + require.False(t, has) + }) +}