2021-01-29 17:50:20 +01:00
|
|
|
package storetests
|
2020-10-18 01:09:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-01-29 17:50:20 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
2020-10-18 01:09:12 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-01-29 17:50:20 +01:00
|
|
|
func StoreTestBlocksStore(t *testing.T, setup func(t *testing.T) (store.Store, func())) {
|
|
|
|
t.Run("InsertBlock", func(t *testing.T) {
|
|
|
|
store, tearDown := setup(t)
|
|
|
|
testInsertBlock(t, store, tearDown)
|
|
|
|
})
|
|
|
|
t.Run("GetSubTree2", func(t *testing.T) {
|
|
|
|
store, tearDown := setup(t)
|
|
|
|
testGetSubTree2(t, store, tearDown)
|
|
|
|
})
|
|
|
|
t.Run("GetSubTree3", func(t *testing.T) {
|
|
|
|
store, tearDown := setup(t)
|
|
|
|
testGetSubTree3(t, store, tearDown)
|
|
|
|
})
|
2021-01-29 19:21:55 +01:00
|
|
|
t.Run("GetRootID", func(t *testing.T) {
|
|
|
|
store, tearDown := setup(t)
|
|
|
|
testGetRootID(t, store, tearDown)
|
|
|
|
})
|
2021-01-29 17:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func testInsertBlock(t *testing.T, store store.Store, tearDown func()) {
|
2020-10-18 01:09:12 +02:00
|
|
|
defer tearDown()
|
|
|
|
|
2021-01-12 20:16:25 +01:00
|
|
|
userID := "user-id"
|
|
|
|
|
2020-10-18 01:09:12 +02:00
|
|
|
blocks, err := store.GetAllBlocks()
|
|
|
|
require.NoError(t, err)
|
2021-01-06 04:47:18 +01:00
|
|
|
initialCount := len(blocks)
|
2020-10-18 01:09:12 +02:00
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
t.Run("valid block", func(t *testing.T) {
|
|
|
|
block := model.Block{
|
|
|
|
ID: "id-test",
|
|
|
|
RootID: "id-test",
|
|
|
|
ModifiedBy: userID,
|
|
|
|
}
|
2020-10-18 01:09:12 +02:00
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
err := store.InsertBlock(block)
|
|
|
|
require.NoError(t, err)
|
2020-10-18 01:09:12 +02:00
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
blocks, err := store.GetAllBlocks()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, initialCount+1)
|
|
|
|
})
|
2020-10-18 01:09:12 +02:00
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
t.Run("invalid rootid", func(t *testing.T) {
|
|
|
|
block := model.Block{
|
|
|
|
ID: "id-test",
|
|
|
|
RootID: "",
|
|
|
|
ModifiedBy: userID,
|
|
|
|
}
|
2020-10-18 01:09:12 +02:00
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
err := store.InsertBlock(block)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
blocks, err := store.GetAllBlocks()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, initialCount+1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid fields data", func(t *testing.T) {
|
|
|
|
block := model.Block{
|
|
|
|
ID: "id-test",
|
|
|
|
RootID: "id-test",
|
|
|
|
ModifiedBy: userID,
|
|
|
|
Fields: map[string]interface{}{"no-serialiable-value": t.Run},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := store.InsertBlock(block)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
blocks, err := store.GetAllBlocks()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, initialCount+1)
|
|
|
|
})
|
2020-10-18 01:09:12 +02:00
|
|
|
}
|
2020-11-12 19:48:08 +01:00
|
|
|
|
2021-01-29 17:50:20 +01:00
|
|
|
func testGetSubTree2(t *testing.T, store store.Store, tearDown func()) {
|
2020-11-12 19:48:08 +01:00
|
|
|
defer tearDown()
|
|
|
|
|
2021-01-12 20:16:25 +01:00
|
|
|
userID := "user-id"
|
|
|
|
|
2020-11-12 19:48:08 +01:00
|
|
|
blocks, err := store.GetAllBlocks()
|
|
|
|
require.NoError(t, err)
|
2021-01-06 04:47:18 +01:00
|
|
|
initialCount := len(blocks)
|
2020-11-12 19:48:08 +01:00
|
|
|
|
|
|
|
blocksToInsert := []model.Block{
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "parent",
|
|
|
|
RootID: "parent",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "child1",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "parent",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "child2",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "parent",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "grandchild1",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "child1",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "grandchild2",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "child2",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "greatgrandchild1",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "grandchild1",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertBlocks(t, store, blocksToInsert)
|
|
|
|
|
2021-01-06 04:47:18 +01:00
|
|
|
blocks, err = store.GetAllBlocks()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, initialCount+6)
|
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
t.Run("from root id", func(t *testing.T) {
|
|
|
|
blocks, err = store.GetSubTree2("parent")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, 3)
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "parent"))
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "child1"))
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "child2"))
|
|
|
|
})
|
2020-11-12 19:48:08 +01:00
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
t.Run("from child id", func(t *testing.T) {
|
|
|
|
blocks, err = store.GetSubTree2("child1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, 2)
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "child1"))
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "grandchild1"))
|
|
|
|
})
|
2020-11-12 19:48:08 +01:00
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
t.Run("from not existing id", func(t *testing.T) {
|
|
|
|
blocks, err = store.GetSubTree2("not-exists")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, 0)
|
|
|
|
})
|
2020-11-12 19:48:08 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 17:50:20 +01:00
|
|
|
func testGetSubTree3(t *testing.T, store store.Store, tearDown func()) {
|
2020-11-12 19:48:08 +01:00
|
|
|
defer tearDown()
|
|
|
|
|
2021-01-12 20:16:25 +01:00
|
|
|
userID := "user-id"
|
|
|
|
|
2020-11-12 19:48:08 +01:00
|
|
|
blocks, err := store.GetAllBlocks()
|
|
|
|
require.NoError(t, err)
|
2021-01-06 04:47:18 +01:00
|
|
|
initialCount := len(blocks)
|
2020-11-12 19:48:08 +01:00
|
|
|
|
|
|
|
blocksToInsert := []model.Block{
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "parent",
|
|
|
|
RootID: "parent",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "child1",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "parent",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "child2",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "parent",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "grandchild1",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "child1",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "grandchild2",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "child2",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
2021-01-29 17:50:20 +01:00
|
|
|
{
|
2021-01-12 20:16:25 +01:00
|
|
|
ID: "greatgrandchild1",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "grandchild1",
|
|
|
|
ModifiedBy: userID,
|
2020-11-12 19:48:08 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertBlocks(t, store, blocksToInsert)
|
|
|
|
|
2021-01-06 04:47:18 +01:00
|
|
|
blocks, err = store.GetAllBlocks()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, initialCount+6)
|
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
t.Run("from root id", func(t *testing.T) {
|
|
|
|
blocks, err = store.GetSubTree3("parent")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, 5)
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "parent"))
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "child1"))
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "child2"))
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "grandchild1"))
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "grandchild2"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("from child id", func(t *testing.T) {
|
|
|
|
blocks, err = store.GetSubTree3("child1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, 3)
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "child1"))
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "grandchild1"))
|
|
|
|
require.True(t, ContainsBlockWithID(blocks, "greatgrandchild1"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("from not existing id", func(t *testing.T) {
|
|
|
|
blocks, err = store.GetSubTree3("not-exists")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, blocks, 0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetRootID(t *testing.T, store store.Store, tearDown func()) {
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
userID := "user-id"
|
|
|
|
|
|
|
|
blocks, err := store.GetAllBlocks()
|
2020-11-12 19:48:08 +01:00
|
|
|
require.NoError(t, err)
|
2021-01-29 19:21:55 +01:00
|
|
|
initialCount := len(blocks)
|
|
|
|
|
|
|
|
blocksToInsert := []model.Block{
|
|
|
|
{
|
|
|
|
ID: "parent",
|
|
|
|
RootID: "parent",
|
|
|
|
ModifiedBy: userID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "child1",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "parent",
|
|
|
|
ModifiedBy: userID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "child2",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "parent",
|
|
|
|
ModifiedBy: userID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "grandchild1",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "child1",
|
|
|
|
ModifiedBy: userID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "grandchild2",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "child2",
|
|
|
|
ModifiedBy: userID,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "greatgrandchild1",
|
|
|
|
RootID: "parent",
|
|
|
|
ParentID: "grandchild1",
|
|
|
|
ModifiedBy: userID,
|
|
|
|
},
|
|
|
|
}
|
2020-11-12 19:48:08 +01:00
|
|
|
|
2021-01-29 19:21:55 +01:00
|
|
|
InsertBlocks(t, store, blocksToInsert)
|
2020-11-12 19:48:08 +01:00
|
|
|
|
|
|
|
blocks, err = store.GetAllBlocks()
|
|
|
|
require.NoError(t, err)
|
2021-01-29 19:21:55 +01:00
|
|
|
require.Len(t, blocks, initialCount+6)
|
|
|
|
|
|
|
|
t.Run("from root id", func(t *testing.T) {
|
|
|
|
rootID, err := store.GetRootID("parent")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "parent", rootID)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("from child id", func(t *testing.T) {
|
|
|
|
rootID, err := store.GetRootID("child1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "parent", rootID)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("from not existing id", func(t *testing.T) {
|
|
|
|
_, err := store.GetRootID("not-exists")
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2020-11-12 19:48:08 +01:00
|
|
|
}
|