package storetests import ( "testing" "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) }) 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()) { defer tearDown() userID := "user-id" blocks, err := store.GetAllBlocks() require.NoError(t, err) initialCount := len(blocks) 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) blocks, err := store.GetAllBlocks() require.NoError(t, err) require.Len(t, blocks, initialCount+1) }) t.Run("invalid rootid", func(t *testing.T) { block := model.Block{ ID: "id-test", RootID: "", ModifiedBy: userID, } 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()) { 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+6) 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")) }) 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")) }) 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()) { 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+6) 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")) }) 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+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) }) }