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 1/2] 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) + }) +} From 52c38ff7c21e9e0625c6fbf0834a97ee19f953c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 29 Jan 2021 20:27:43 +0100 Subject: [PATCH 2/2] Add sharing unit tests --- .../services/store/sqlstore/sqlstore_test.go | 1 + server/services/store/storetests/sharing.go | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 server/services/store/storetests/sharing.go diff --git a/server/services/store/sqlstore/sqlstore_test.go b/server/services/store/sqlstore/sqlstore_test.go index edb55bcd5..bbb1b0c6b 100644 --- a/server/services/store/sqlstore/sqlstore_test.go +++ b/server/services/store/sqlstore/sqlstore_test.go @@ -33,4 +33,5 @@ func SetupTests(t *testing.T) (store.Store, func()) { func TestBlocksStore(t *testing.T) { t.Run("BlocksStore", func(t *testing.T) { storetests.StoreTestBlocksStore(t, SetupTests) }) + t.Run("SharingStore", func(t *testing.T) { storetests.StoreTestSharingStore(t, SetupTests) }) } diff --git a/server/services/store/storetests/sharing.go b/server/services/store/storetests/sharing.go new file mode 100644 index 000000000..888fe2f35 --- /dev/null +++ b/server/services/store/storetests/sharing.go @@ -0,0 +1,59 @@ +package storetests + +import ( + "testing" + + "github.com/mattermost/focalboard/server/model" + "github.com/mattermost/focalboard/server/services/store" + "github.com/stretchr/testify/require" +) + +func StoreTestSharingStore(t *testing.T, setup func(t *testing.T) (store.Store, func())) { + t.Run("UpsertSharingAndGetSharing", func(t *testing.T) { + store, tearDown := setup(t) + defer tearDown() + testUpsertSharingAndGetSharing(t, store) + }) +} + +func testUpsertSharingAndGetSharing(t *testing.T, store store.Store) { + t.Run("Insert first sharing and get it", func(t *testing.T) { + sharing := model.Sharing{ + ID: "sharing-id", + Enabled: true, + Token: "token", + ModifiedBy: "user-id", + } + + err := store.UpsertSharing(sharing) + require.NoError(t, err) + newSharing, err := store.GetSharing("sharing-id") + require.NoError(t, err) + newSharing.UpdateAt = 0 + require.Equal(t, sharing, *newSharing) + }) + t.Run("Upsert the inserted sharing and get it", func(t *testing.T) { + sharing := model.Sharing{ + ID: "sharing-id", + Enabled: true, + Token: "token2", + ModifiedBy: "user-id2", + } + + newSharing, err := store.GetSharing("sharing-id") + require.NoError(t, err) + newSharing.UpdateAt = 0 + require.NotEqual(t, sharing, *newSharing) + + err = store.UpsertSharing(sharing) + require.NoError(t, err) + newSharing, err = store.GetSharing("sharing-id") + require.NoError(t, err) + newSharing.UpdateAt = 0 + require.Equal(t, sharing, *newSharing) + }) + t.Run("Get not existing sharing", func(t *testing.T) { + _, err := store.GetSharing("not-existing") + require.Error(t, err) + }) +}