focalboard/server/services/store/storetests/blocks.go

198 lines
4.4 KiB
Go
Raw Normal View History

package storetests
import (
"testing"
"time"
2021-01-26 23:13:46 +01:00
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/services/store"
"github.com/stretchr/testify/require"
)
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)
})
}
func testInsertBlock(t *testing.T, store store.Store, tearDown func()) {
defer tearDown()
2021-01-12 20:16:25 +01:00
userID := "user-id"
blocks, err := store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 04:47:18 +01:00
initialCount := len(blocks)
block := model.Block{
2021-01-12 20:16:25 +01:00
ID: "id-test",
RootID: "id-test",
ModifiedBy: userID,
}
err = store.InsertBlock(block)
require.NoError(t, err)
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 04:47:18 +01:00
require.Len(t, blocks, initialCount+1)
// Wait for not colliding the ID+insert_at key
time.Sleep(1 * time.Millisecond)
2021-01-12 20:16:25 +01:00
err = store.DeleteBlock(block.ID, userID)
require.NoError(t, err)
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 04:47:18 +01:00
require.Len(t, blocks, initialCount)
}
2020-11-12 19:48:08 +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-12 20:16:25 +01:00
ID: "parent",
RootID: "parent",
ModifiedBy: userID,
2020-11-12 19:48:08 +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-12 20:16:25 +01:00
ID: "child2",
RootID: "parent",
ParentID: "parent",
ModifiedBy: userID,
2020-11-12 19:48:08 +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-12 20:16:25 +01:00
ID: "grandchild2",
RootID: "parent",
ParentID: "child2",
ModifiedBy: userID,
2020-11-12 19:48:08 +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)
2020-11-12 19:48:08 +01:00
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"))
// Wait for not colliding the ID+insert_at key
time.Sleep(1 * time.Millisecond)
2021-01-12 20:16:25 +01:00
DeleteBlocks(t, store, blocksToInsert, userID)
2020-11-12 19:48:08 +01:00
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 04:47:18 +01:00
require.Len(t, blocks, initialCount)
2020-11-12 19:48:08 +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-12 20:16:25 +01:00
ID: "parent",
RootID: "parent",
ModifiedBy: userID,
2020-11-12 19:48:08 +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-12 20:16:25 +01:00
ID: "child2",
RootID: "parent",
ParentID: "parent",
ModifiedBy: userID,
2020-11-12 19:48:08 +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-12 20:16:25 +01:00
ID: "grandchild2",
RootID: "parent",
ParentID: "child2",
ModifiedBy: userID,
2020-11-12 19:48:08 +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)
2020-11-12 19:48:08 +01:00
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"))
// Wait for not colliding the ID+insert_at key
time.Sleep(1 * time.Millisecond)
2021-01-12 20:16:25 +01:00
DeleteBlocks(t, store, blocksToInsert, userID)
2020-11-12 19:48:08 +01:00
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 04:47:18 +01:00
require.Len(t, blocks, initialCount)
2020-11-12 19:48:08 +01:00
}