From 68561f0f6b7f2170a5e37888ea1fd6bdc5dbe4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 29 Jan 2021 19:32:33 +0100 Subject: [PATCH] Add more block store tests --- server/services/store/storetests/blocks.go | 164 +++++++++++++++++++-- 1 file changed, 148 insertions(+), 16 deletions(-) diff --git a/server/services/store/storetests/blocks.go b/server/services/store/storetests/blocks.go index 04805b920..ba3bd1006 100644 --- a/server/services/store/storetests/blocks.go +++ b/server/services/store/storetests/blocks.go @@ -2,6 +2,7 @@ package storetests import ( "testing" + "time" "github.com/mattermost/focalboard/server/model" "github.com/mattermost/focalboard/server/services/store" @@ -11,25 +12,37 @@ import ( 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) + defer tearDown() + testInsertBlock(t, store) + }) + t.Run("DeleteBlock", func(t *testing.T) { + store, tearDown := setup(t) + defer tearDown() + testDeleteBlock(t, store) }) t.Run("GetSubTree2", func(t *testing.T) { store, tearDown := setup(t) - testGetSubTree2(t, store, tearDown) + defer tearDown() + testGetSubTree2(t, store) }) t.Run("GetSubTree3", func(t *testing.T) { store, tearDown := setup(t) - testGetSubTree3(t, store, tearDown) + defer tearDown() + testGetSubTree3(t, store) + }) + t.Run("GetParentID", func(t *testing.T) { + store, tearDown := setup(t) + defer tearDown() + testGetParentID(t, store) }) t.Run("GetRootID", func(t *testing.T) { store, tearDown := setup(t) - testGetRootID(t, store, tearDown) + defer tearDown() + testGetRootID(t, store) }) } -func testInsertBlock(t *testing.T, store store.Store, tearDown func()) { - defer tearDown() - +func testInsertBlock(t *testing.T, store store.Store) { userID := "user-id" blocks, err := store.GetAllBlocks() @@ -83,9 +96,7 @@ func testInsertBlock(t *testing.T, store store.Store, tearDown func()) { }) } -func testGetSubTree2(t *testing.T, store store.Store, tearDown func()) { - defer tearDown() - +func testGetSubTree2(t *testing.T, store store.Store) { userID := "user-id" blocks, err := store.GetAllBlocks() @@ -160,9 +171,7 @@ func testGetSubTree2(t *testing.T, store store.Store, tearDown func()) { }) } -func testGetSubTree3(t *testing.T, store store.Store, tearDown func()) { - defer tearDown() - +func testGetSubTree3(t *testing.T, store store.Store) { userID := "user-id" blocks, err := store.GetAllBlocks() @@ -240,9 +249,7 @@ func testGetSubTree3(t *testing.T, store store.Store, tearDown func()) { }) } -func testGetRootID(t *testing.T, store store.Store, tearDown func()) { - defer tearDown() - +func testGetRootID(t *testing.T, store store.Store) { userID := "user-id" blocks, err := store.GetAllBlocks() @@ -310,3 +317,128 @@ func testGetRootID(t *testing.T, store store.Store, tearDown func()) { require.Error(t, err) }) } + +func testGetParentID(t *testing.T, store store.Store) { + 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+6) + + t.Run("from root id", func(t *testing.T) { + parentID, err := store.GetParentID("parent") + require.NoError(t, err) + require.Equal(t, "", parentID) + }) + + t.Run("from child id", func(t *testing.T) { + parentID, err := store.GetParentID("grandchild1") + require.NoError(t, err) + require.Equal(t, "child1", parentID) + }) + + t.Run("from not existing id", func(t *testing.T) { + _, err := store.GetParentID("not-exists") + require.Error(t, err) + }) +} + +func testDeleteBlock(t *testing.T, store store.Store) { + userID := "user-id" + + blocks, err := store.GetAllBlocks() + require.NoError(t, err) + initialCount := len(blocks) + + blocksToInsert := []model.Block{ + { + ID: "block1", + RootID: "block1", + ModifiedBy: userID, + }, + { + ID: "block2", + RootID: "block2", + ModifiedBy: userID, + }, + { + ID: "block3", + RootID: "block3", + ModifiedBy: userID, + }, + } + InsertBlocks(t, store, blocksToInsert) + + blocks, err = store.GetAllBlocks() + require.NoError(t, err) + require.Len(t, blocks, initialCount+3) + + t.Run("exiting id", func(t *testing.T) { + // Wait for not colliding the ID+insert_at key + time.Sleep(1 * time.Millisecond) + err := store.DeleteBlock("block1", userID) + require.NoError(t, err) + }) + + t.Run("exiting id multiple times", func(t *testing.T) { + // Wait for not colliding the ID+insert_at key + time.Sleep(1 * time.Millisecond) + err := store.DeleteBlock("block1", userID) + require.NoError(t, err) + // Wait for not colliding the ID+insert_at key + time.Sleep(1 * time.Millisecond) + err = store.DeleteBlock("block1", userID) + require.NoError(t, err) + }) + + t.Run("from not existing id", func(t *testing.T) { + // Wait for not colliding the ID+insert_at key + time.Sleep(1 * time.Millisecond) + err := store.DeleteBlock("not-exists", userID) + require.NoError(t, err) + }) +}