Improving blocks tests

This commit is contained in:
Jesús Espino 2021-01-29 19:21:55 +01:00
parent baad055cc9
commit 7ee3213641

View file

@ -2,7 +2,6 @@ package storetests
import (
"testing"
"time"
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/services/store"
@ -22,6 +21,10 @@ func StoreTestBlocksStore(t *testing.T, setup func(t *testing.T) (store.Store, f
store, tearDown := setup(t)
testGetSubTree3(t, store, tearDown)
})
t.Run("GetRootID", func(t *testing.T) {
store, tearDown := setup(t)
testGetRootID(t, store, tearDown)
})
}
func testInsertBlock(t *testing.T, store store.Store, tearDown func()) {
@ -33,27 +36,51 @@ func testInsertBlock(t *testing.T, store store.Store, tearDown func()) {
require.NoError(t, err)
initialCount := len(blocks)
block := model.Block{
ID: "id-test",
RootID: "id-test",
ModifiedBy: userID,
}
t.Run("valid block", func(t *testing.T) {
block := model.Block{
ID: "id-test",
RootID: "id-test",
ModifiedBy: userID,
}
err = store.InsertBlock(block)
require.NoError(t, err)
err := store.InsertBlock(block)
require.NoError(t, err)
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, initialCount+1)
blocks, err := store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, initialCount+1)
})
// Wait for not colliding the ID+insert_at key
time.Sleep(1 * time.Millisecond)
err = store.DeleteBlock(block.ID, userID)
require.NoError(t, err)
t.Run("invalid rootid", func(t *testing.T) {
block := model.Block{
ID: "id-test",
RootID: "",
ModifiedBy: userID,
}
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, initialCount)
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)
})
}
func testGetSubTree2(t *testing.T, store store.Store, tearDown func()) {
@ -109,20 +136,28 @@ func testGetSubTree2(t *testing.T, store store.Store, tearDown func()) {
require.NoError(t, err)
require.Len(t, blocks, initialCount+6)
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"))
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"))
})
// Wait for not colliding the ID+insert_at key
time.Sleep(1 * time.Millisecond)
DeleteBlocks(t, store, blocksToInsert, userID)
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"))
})
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, initialCount)
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)
})
}
func testGetSubTree3(t *testing.T, store store.Store, tearDown func()) {
@ -178,20 +213,100 @@ func testGetSubTree3(t *testing.T, store store.Store, tearDown func()) {
require.NoError(t, err)
require.Len(t, blocks, initialCount+6)
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 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"))
})
// Wait for not colliding the ID+insert_at key
time.Sleep(1 * time.Millisecond)
DeleteBlocks(t, store, blocksToInsert, userID)
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()
require.NoError(t, err)
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,
},
}
InsertBlocks(t, store, blocksToInsert)
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, initialCount)
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)
})
}