Add list storage

This commit is contained in:
mcrakhman 2022-10-26 14:18:44 +02:00 committed by Mikhail Iudin
parent ca6964702f
commit 7ddd6825ba
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
2 changed files with 67 additions and 4 deletions

View File

@ -0,0 +1,63 @@
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/stretchr/testify/require"
"testing"
)
func (fx *fixture) 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_Create(t *testing.T) {
fx := newFixture(t)
fx.open(t)
defer fx.stop(t)
aclRoot := &aclrecordproto.RawACLRecordWithId{Payload: []byte("root"), Id: "someRootId"}
listStore, err := createListStorage(fx.db, aclRoot)
require.NoError(t, err)
fx.testListInDB(t, listStore, aclRoot, aclRoot.Id)
}
func TestListStorage_Methods(t *testing.T) {
fx := newFixture(t)
fx.open(t)
aclRoot := &aclrecordproto.RawACLRecordWithId{Payload: []byte("root"), Id: "someRootId"}
_, err := createListStorage(fx.db, aclRoot)
require.NoError(t, err)
fx.stop(t)
fx.open(t)
defer fx.stop(t)
listStore, err := newListStorage(fx.db)
require.NoError(t, err)
fx.testListInDB(t, listStore, aclRoot, aclRoot.Id)
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)
})
}

View File

@ -38,7 +38,7 @@ func (fx *fixture) open(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
} }
func (fx *fixture) testPayloadInDB(t *testing.T, store storage.TreeStorage, payload storage.TreeStorageCreatePayload) { func (fx *fixture) 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()
@ -61,7 +61,7 @@ func (fx *fixture) stop(t *testing.T) {
require.NoError(t, fx.db.Close()) require.NoError(t, fx.db.Close())
} }
func TestTreeStorage(t *testing.T) { func TestTreeStorage_Create(t *testing.T) {
fx := newFixture(t) fx := newFixture(t)
fx.open(t) fx.open(t)
defer fx.stop(t) defer fx.stop(t)
@ -69,7 +69,7 @@ func TestTreeStorage(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.testPayloadInDB(t, store, payload) fx.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.testPayloadInDB(t, store, payload) fx.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"}