Fix Go integration tests

This commit is contained in:
Chen-I Lim 2021-01-05 19:47:18 -08:00
parent 4350378ce6
commit 593e077fb9
3 changed files with 99 additions and 23 deletions

15
.vscode/launch.json vendored
View file

@ -11,7 +11,20 @@
"mode": "debug",
"program": "${workspaceFolder}/server/main",
"cwd": "${workspaceFolder}"
},
},
{
"name": "Go: Test Current File",
"type": "go",
"request": "launch",
"mode": "test",
"remotePath": "",
"port": 8888,
"host": "127.0.0.1",
"program": "${file}",
"env": {},
"args": [],
"showLog": true
},
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",

View file

@ -13,28 +13,34 @@ func TestGetBlocks(t *testing.T) {
th := SetupTestHelper().InitBasic()
defer th.TearDown()
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
initialCount := len(blocks)
blockID1 := utils.CreateGUID()
blockID2 := utils.CreateGUID()
newBlocks := []model.Block{
{
ID: blockID1,
RootID: blockID1,
CreateAt: 1,
UpdateAt: 1,
Type: "board",
},
{
ID: blockID2,
RootID: blockID2,
CreateAt: 1,
UpdateAt: 1,
Type: "board",
},
}
_, resp := th.Client.InsertBlocks(newBlocks)
_, resp = th.Client.InsertBlocks(newBlocks)
require.NoError(t, resp.Error)
blocks, resp := th.Client.GetBlocks()
blocks, resp = th.Client.GetBlocks()
require.NoError(t, resp.Error)
require.Len(t, blocks, 2)
require.Len(t, blocks, initialCount+2)
blockIDs := make([]string, len(blocks))
for i, b := range blocks {
@ -48,6 +54,10 @@ func TestPostBlock(t *testing.T) {
th := SetupTestHelper().InitBasic()
defer th.TearDown()
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
initialCount := len(blocks)
blockID1 := utils.CreateGUID()
blockID2 := utils.CreateGUID()
blockID3 := utils.CreateGUID()
@ -55,6 +65,7 @@ func TestPostBlock(t *testing.T) {
t.Run("Create a single block", func(t *testing.T) {
block := model.Block{
ID: blockID1,
RootID: blockID1,
CreateAt: 1,
UpdateAt: 1,
Type: "board",
@ -66,20 +77,27 @@ func TestPostBlock(t *testing.T) {
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
require.Len(t, blocks, 1)
require.Equal(t, blockID1, blocks[0].ID)
require.Len(t, blocks, initialCount+1)
blockIDs := make([]string, len(blocks))
for i, b := range blocks {
blockIDs[i] = b.ID
}
require.Contains(t, blockIDs, blockID1)
})
t.Run("Create a couple of blocks in the same call", func(t *testing.T) {
newBlocks := []model.Block{
{
ID: blockID2,
RootID: blockID2,
CreateAt: 1,
UpdateAt: 1,
Type: "board",
},
{
ID: blockID3,
RootID: blockID3,
CreateAt: 1,
UpdateAt: 1,
Type: "board",
@ -91,7 +109,7 @@ func TestPostBlock(t *testing.T) {
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
require.Len(t, blocks, 3)
require.Len(t, blocks, initialCount+3)
blockIDs := make([]string, len(blocks))
for i, b := range blocks {
@ -105,6 +123,7 @@ func TestPostBlock(t *testing.T) {
t.Run("Update a block", func(t *testing.T) {
block := model.Block{
ID: blockID1,
RootID: blockID1,
CreateAt: 1,
UpdateAt: 20,
Type: "board",
@ -116,7 +135,7 @@ func TestPostBlock(t *testing.T) {
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
require.Len(t, blocks, 3)
require.Len(t, blocks, initialCount+3)
var updatedBlock model.Block
for _, b := range blocks {
@ -133,10 +152,15 @@ func TestDeleteBlock(t *testing.T) {
th := SetupTestHelper().InitBasic()
defer th.TearDown()
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
initialCount := len(blocks)
blockID := utils.CreateGUID()
t.Run("Create a block", func(t *testing.T) {
block := model.Block{
ID: blockID,
RootID: blockID,
CreateAt: 1,
UpdateAt: 1,
Type: "board",
@ -148,8 +172,14 @@ func TestDeleteBlock(t *testing.T) {
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
require.Len(t, blocks, 1)
require.Equal(t, blockID, blocks[0].ID)
require.Len(t, blocks, initialCount+1)
blockIDs := make([]string, len(blocks))
for i, b := range blocks {
blockIDs[i] = b.ID
}
require.Contains(t, blockIDs, blockID)
})
t.Run("Delete a block", func(t *testing.T) {
@ -158,7 +188,7 @@ func TestDeleteBlock(t *testing.T) {
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
require.Len(t, blocks, 0)
require.Len(t, blocks, initialCount)
})
}
@ -166,6 +196,10 @@ func TestGetSubtree(t *testing.T) {
th := SetupTestHelper().InitBasic()
defer th.TearDown()
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
initialCount := len(blocks)
parentBlockID := utils.CreateGUID()
childBlockID1 := utils.CreateGUID()
childBlockID2 := utils.CreateGUID()
@ -173,12 +207,14 @@ func TestGetSubtree(t *testing.T) {
newBlocks := []model.Block{
{
ID: parentBlockID,
RootID: parentBlockID,
CreateAt: 1,
UpdateAt: 1,
Type: "board",
},
{
ID: childBlockID1,
RootID: parentBlockID,
ParentID: parentBlockID,
CreateAt: 2,
UpdateAt: 2,
@ -186,6 +222,7 @@ func TestGetSubtree(t *testing.T) {
},
{
ID: childBlockID2,
RootID: parentBlockID,
ParentID: parentBlockID,
CreateAt: 2,
UpdateAt: 2,
@ -198,8 +235,13 @@ func TestGetSubtree(t *testing.T) {
blocks, resp := th.Client.GetBlocks()
require.NoError(t, resp.Error)
require.Len(t, blocks, 1)
require.Equal(t, parentBlockID, blocks[0].ID)
require.Len(t, blocks, initialCount+1) // GetBlocks returns root blocks (null ParentID)
blockIDs := make([]string, len(blocks))
for i, b := range blocks {
blockIDs[i] = b.ID
}
require.Contains(t, blockIDs, parentBlockID)
})
t.Run("Get subtree for parent ID", func(t *testing.T) {

View file

@ -14,10 +14,11 @@ func TestInsertBlock(t *testing.T) {
blocks, err := store.GetAllBlocks()
require.NoError(t, err)
require.Empty(t, blocks)
initialCount := len(blocks)
block := model.Block{
ID: "id-test",
ID: "id-test",
RootID: "id-test",
}
err = store.InsertBlock(block)
@ -25,7 +26,7 @@ func TestInsertBlock(t *testing.T) {
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, 1)
require.Len(t, blocks, initialCount+1)
// Wait for not colliding the ID+insert_at key
time.Sleep(1 * time.Millisecond)
@ -34,7 +35,7 @@ func TestInsertBlock(t *testing.T) {
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Empty(t, blocks)
require.Len(t, blocks, initialCount)
}
func TestGetSubTree2(t *testing.T) {
@ -43,36 +44,46 @@ func TestGetSubTree2(t *testing.T) {
blocks, err := store.GetAllBlocks()
require.NoError(t, err)
require.Empty(t, blocks)
initialCount := len(blocks)
blocksToInsert := []model.Block{
model.Block{
ID: "parent",
ID: "parent",
RootID: "parent",
},
model.Block{
ID: "child1",
RootID: "parent",
ParentID: "parent",
},
model.Block{
ID: "child2",
RootID: "parent",
ParentID: "parent",
},
model.Block{
ID: "grandchild1",
RootID: "parent",
ParentID: "child1",
},
model.Block{
ID: "grandchild2",
RootID: "parent",
ParentID: "child2",
},
model.Block{
ID: "greatgrandchild1",
RootID: "parent",
ParentID: "grandchild1",
},
}
InsertBlocks(t, store, blocksToInsert)
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, initialCount+6)
blocks, err = store.GetSubTree2("parent")
require.NoError(t, err)
require.Len(t, blocks, 3)
@ -86,7 +97,7 @@ func TestGetSubTree2(t *testing.T) {
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Empty(t, blocks)
require.Len(t, blocks, initialCount)
}
func TestGetSubTree3(t *testing.T) {
@ -95,36 +106,46 @@ func TestGetSubTree3(t *testing.T) {
blocks, err := store.GetAllBlocks()
require.NoError(t, err)
require.Empty(t, blocks)
initialCount := len(blocks)
blocksToInsert := []model.Block{
model.Block{
ID: "parent",
ID: "parent",
RootID: "parent",
},
model.Block{
ID: "child1",
RootID: "parent",
ParentID: "parent",
},
model.Block{
ID: "child2",
RootID: "parent",
ParentID: "parent",
},
model.Block{
ID: "grandchild1",
RootID: "parent",
ParentID: "child1",
},
model.Block{
ID: "grandchild2",
RootID: "parent",
ParentID: "child2",
},
model.Block{
ID: "greatgrandchild1",
RootID: "parent",
ParentID: "grandchild1",
},
}
InsertBlocks(t, store, blocksToInsert)
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Len(t, blocks, initialCount+6)
blocks, err = store.GetSubTree3("parent")
require.NoError(t, err)
require.Len(t, blocks, 5)
@ -140,5 +161,5 @@ func TestGetSubTree3(t *testing.T) {
blocks, err = store.GetAllBlocks()
require.NoError(t, err)
require.Empty(t, blocks)
require.Len(t, blocks, initialCount)
}