[GH-431] Add unit tests for ./store/sqlstore/workspaces.go (#768)

* Add unit tests for ./store/sqlstore/workspaces.go

* replace require.Nil to require.NoError

* Fixing problem with workspace settings upsert

Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
Co-authored-by: Jesús Espino <jespinog@gmail.com>
This commit is contained in:
dave 2021-07-29 17:56:57 +08:00 committed by GitHub
parent 6a7d3f797b
commit cfac3fe81b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 127 additions and 0 deletions

View file

@ -46,4 +46,5 @@ func TestBlocksStore(t *testing.T) {
t.Run("SystemStore", func(t *testing.T) { storetests.StoreTestSystemStore(t, SetupTests) }) t.Run("SystemStore", func(t *testing.T) { storetests.StoreTestSystemStore(t, SetupTests) })
t.Run("UserStore", func(t *testing.T) { storetests.StoreTestUserStore(t, SetupTests) }) t.Run("UserStore", func(t *testing.T) { storetests.StoreTestUserStore(t, SetupTests) })
t.Run("SessionStore", func(t *testing.T) { storetests.StoreTestSessionStore(t, SetupTests) }) t.Run("SessionStore", func(t *testing.T) { storetests.StoreTestSessionStore(t, SetupTests) })
t.Run("WorkspaceStore", func(t *testing.T) { storetests.StoreTestWorkspaceStore(t, SetupTests) })
} }

View file

@ -6,6 +6,7 @@ import (
"github.com/mattermost/focalboard/server/model" "github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/services/mlog" "github.com/mattermost/focalboard/server/services/mlog"
"github.com/mattermost/focalboard/server/utils"
sq "github.com/Masterminds/squirrel" sq "github.com/Masterminds/squirrel"
) )
@ -43,6 +44,7 @@ func (s *SQLStore) UpsertWorkspaceSignupToken(workspace model.Workspace) error {
func (s *SQLStore) UpsertWorkspaceSettings(workspace model.Workspace) error { func (s *SQLStore) UpsertWorkspaceSettings(workspace model.Workspace) error {
now := time.Now().Unix() now := time.Now().Unix()
signupToken := utils.CreateGUID()
settingsJSON, err := json.Marshal(workspace.Settings) settingsJSON, err := json.Marshal(workspace.Settings)
if err != nil { if err != nil {
@ -53,12 +55,14 @@ func (s *SQLStore) UpsertWorkspaceSettings(workspace model.Workspace) error {
Insert(s.tablePrefix+"workspaces"). Insert(s.tablePrefix+"workspaces").
Columns( Columns(
"id", "id",
"signup_token",
"settings", "settings",
"modified_by", "modified_by",
"update_at", "update_at",
). ).
Values( Values(
workspace.ID, workspace.ID,
signupToken,
settingsJSON, settingsJSON,
workspace.ModifiedBy, workspace.ModifiedBy,
now, now,

View file

@ -0,0 +1,122 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package storetests
import (
"fmt"
"time"
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/utils"
"testing"
"github.com/stretchr/testify/require"
"github.com/mattermost/focalboard/server/services/store"
)
func StoreTestWorkspaceStore(t *testing.T, setup func(t *testing.T) (store.Store, func())) {
t.Run("UpsertWorkspaceSignupToken", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testUpsertWorkspaceSignupToken(t, store)
})
t.Run("UpsertWorkspaceSettings", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testUpsertWorkspaceSettings(t, store)
})
t.Run("GetWorkspaceCount", func(t *testing.T) {
store, tearDown := setup(t)
defer tearDown()
testGetWorkspaceCount(t, store)
})
}
func testUpsertWorkspaceSignupToken(t *testing.T, store store.Store) {
t.Run("Insert and update workspace with signup token", func(t *testing.T) {
workspaceID := "0"
workspace := &model.Workspace{
ID: workspaceID,
SignupToken: utils.CreateGUID(),
}
// insert
err := store.UpsertWorkspaceSignupToken(*workspace)
require.NoError(t, err)
got, err := store.GetWorkspace(workspaceID)
require.NoError(t, err)
require.Equal(t, workspace.ID, got.ID)
require.Equal(t, workspace.SignupToken, got.SignupToken)
// update signup token
workspace.SignupToken = utils.CreateGUID()
err = store.UpsertWorkspaceSignupToken(*workspace)
require.NoError(t, err)
got, err = store.GetWorkspace(workspaceID)
require.NoError(t, err)
require.Equal(t, workspace.ID, got.ID)
require.Equal(t, workspace.SignupToken, got.SignupToken)
})
}
func testUpsertWorkspaceSettings(t *testing.T, store store.Store) {
t.Run("Insert and update workspace with settings", func(t *testing.T) {
workspaceID := "0"
workspace := &model.Workspace{
ID: workspaceID,
Settings: map[string]interface{}{
"field1": "A",
},
}
// insert
err := store.UpsertWorkspaceSettings(*workspace)
require.NoError(t, err)
got, err := store.GetWorkspace(workspaceID)
require.NoError(t, err)
require.Equal(t, workspace.ID, got.ID)
require.Equal(t, workspace.Settings, got.Settings)
// update settings
workspace.Settings = map[string]interface{}{
"field1": "B",
}
err = store.UpsertWorkspaceSettings(*workspace)
require.NoError(t, err)
got2, err := store.GetWorkspace(workspaceID)
require.NoError(t, err)
require.Equal(t, workspace.ID, got2.ID)
require.Equal(t, workspace.Settings, got2.Settings)
require.Equal(t, got.SignupToken, got2.SignupToken)
})
}
func testGetWorkspaceCount(t *testing.T, store store.Store) {
t.Run("Insert multiple workspace and get workspace count", func(t *testing.T) {
// insert
n := time.Now().Unix() % 10
for i := 0; i < int(n); i++ {
workspaceID := fmt.Sprintf("%d", i)
workspace := &model.Workspace{
ID: workspaceID,
SignupToken: utils.CreateGUID(),
}
err := store.UpsertWorkspaceSignupToken(*workspace)
require.NoError(t, err)
}
got, err := store.GetWorkspaceCount()
require.NoError(t, err)
require.Equal(t, n, got)
})
}