1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-18 09:35:25 +02:00
focalboard/server/services/store/sqlstore/blocks_test.go

185 lines
4.1 KiB
Go
Raw Normal View History

package sqlstore
import (
"testing"
"time"
"github.com/mattermost/mattermost-octo-tasks/server/model"
"github.com/stretchr/testify/require"
)
func TestInsertBlock(t *testing.T) {
store, tearDown := SetupTests(t)
defer tearDown()
2021-01-12 21:16:25 +02:00
userID := "user-id"
blocks, err := store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 05:47:18 +02:00
initialCount := len(blocks)
block := model.Block{
2021-01-12 21:16:25 +02: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 05:47:18 +02:00
require.Len(t, blocks, initialCount+1)
// Wait for not colliding the ID+insert_at key
time.Sleep(1 * time.Millisecond)
2021-01-12 21:16:25 +02:00
err = store.DeleteBlock(block.ID, userID)
require.NoError(t, err)
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 05:47:18 +02:00
require.Len(t, blocks, initialCount)
}
2020-11-12 20:48:08 +02:00
func TestGetSubTree2(t *testing.T) {
store, tearDown := SetupTests(t)
defer tearDown()
2021-01-12 21:16:25 +02:00
userID := "user-id"
2020-11-12 20:48:08 +02:00
blocks, err := store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 05:47:18 +02:00
initialCount := len(blocks)
2020-11-12 20:48:08 +02:00
blocksToInsert := []model.Block{
model.Block{
2021-01-12 21:16:25 +02:00
ID: "parent",
RootID: "parent",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "child1",
RootID: "parent",
ParentID: "parent",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "child2",
RootID: "parent",
ParentID: "parent",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "grandchild1",
RootID: "parent",
ParentID: "child1",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "grandchild2",
RootID: "parent",
ParentID: "child2",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "greatgrandchild1",
RootID: "parent",
ParentID: "grandchild1",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
}
InsertBlocks(t, store, blocksToInsert)
2021-01-06 05:47:18 +02:00
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, initialCount+6)
2020-11-12 20:48:08 +02: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 21:16:25 +02:00
DeleteBlocks(t, store, blocksToInsert, userID)
2020-11-12 20:48:08 +02:00
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 05:47:18 +02:00
require.Len(t, blocks, initialCount)
2020-11-12 20:48:08 +02:00
}
func TestGetSubTree3(t *testing.T) {
store, tearDown := SetupTests(t)
defer tearDown()
2021-01-12 21:16:25 +02:00
userID := "user-id"
2020-11-12 20:48:08 +02:00
blocks, err := store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 05:47:18 +02:00
initialCount := len(blocks)
2020-11-12 20:48:08 +02:00
blocksToInsert := []model.Block{
model.Block{
2021-01-12 21:16:25 +02:00
ID: "parent",
RootID: "parent",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "child1",
RootID: "parent",
ParentID: "parent",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "child2",
RootID: "parent",
ParentID: "parent",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "grandchild1",
RootID: "parent",
ParentID: "child1",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "grandchild2",
RootID: "parent",
ParentID: "child2",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
model.Block{
2021-01-12 21:16:25 +02:00
ID: "greatgrandchild1",
RootID: "parent",
ParentID: "grandchild1",
ModifiedBy: userID,
2020-11-12 20:48:08 +02:00
},
}
InsertBlocks(t, store, blocksToInsert)
2021-01-06 05:47:18 +02:00
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, initialCount+6)
2020-11-12 20:48:08 +02: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 21:16:25 +02:00
DeleteBlocks(t, store, blocksToInsert, userID)
2020-11-12 20:48:08 +02:00
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
2021-01-06 05:47:18 +02:00
require.Len(t, blocks, initialCount)
2020-11-12 20:48:08 +02:00
}