diff --git a/node/storage/liststorage_test.go b/node/storage/liststorage_test.go new file mode 100644 index 00000000..f0a35471 --- /dev/null +++ b/node/storage/liststorage_test.go @@ -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) + }) +} diff --git a/node/storage/treestorage_test.go b/node/storage/treestorage_test.go index 98bc5ecd..c5e6feca 100644 --- a/node/storage/treestorage_test.go +++ b/node/storage/treestorage_test.go @@ -38,7 +38,7 @@ func (fx *fixture) open(t *testing.T) { 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()) root, err := store.Root() @@ -61,7 +61,7 @@ func (fx *fixture) stop(t *testing.T) { require.NoError(t, fx.db.Close()) } -func TestTreeStorage(t *testing.T) { +func TestTreeStorage_Create(t *testing.T) { fx := newFixture(t) fx.open(t) defer fx.stop(t) @@ -69,7 +69,7 @@ func TestTreeStorage(t *testing.T) { payload := treeTestPayload() store, err := createTreeStorage(fx.db, payload) require.NoError(t, err) - fx.testPayloadInDB(t, store, payload) + fx.testTreePayloadInDB(t, store, payload) } func TestTreeStorage_Methods(t *testing.T) { @@ -84,7 +84,7 @@ func TestTreeStorage_Methods(t *testing.T) { defer fx.stop(t) store, err := newTreeStorage(fx.db, payload.RootRawChange.Id) require.NoError(t, err) - fx.testPayloadInDB(t, store, payload) + fx.testTreePayloadInDB(t, store, payload) t.Run("update heads", func(t *testing.T) { newHeads := []string{"a", "b"}