From cfac3fe81b4931cd90a9d6a65ad0360caa048101 Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 29 Jul 2021 17:56:57 +0800 Subject: [PATCH] [GH-431] Add unit tests for ./store/sqlstore/workspaces.go (#768) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Jesús Espino --- .../services/store/sqlstore/sqlstore_test.go | 1 + server/services/store/sqlstore/workspaces.go | 4 + .../services/store/storetests/workspaces.go | 122 ++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 server/services/store/storetests/workspaces.go diff --git a/server/services/store/sqlstore/sqlstore_test.go b/server/services/store/sqlstore/sqlstore_test.go index 1bc9685ad..162d04d20 100644 --- a/server/services/store/sqlstore/sqlstore_test.go +++ b/server/services/store/sqlstore/sqlstore_test.go @@ -46,4 +46,5 @@ func TestBlocksStore(t *testing.T) { t.Run("SystemStore", func(t *testing.T) { storetests.StoreTestSystemStore(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("WorkspaceStore", func(t *testing.T) { storetests.StoreTestWorkspaceStore(t, SetupTests) }) } diff --git a/server/services/store/sqlstore/workspaces.go b/server/services/store/sqlstore/workspaces.go index d0a4534e0..3a07a3955 100644 --- a/server/services/store/sqlstore/workspaces.go +++ b/server/services/store/sqlstore/workspaces.go @@ -6,6 +6,7 @@ import ( "github.com/mattermost/focalboard/server/model" "github.com/mattermost/focalboard/server/services/mlog" + "github.com/mattermost/focalboard/server/utils" 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 { now := time.Now().Unix() + signupToken := utils.CreateGUID() settingsJSON, err := json.Marshal(workspace.Settings) if err != nil { @@ -53,12 +55,14 @@ func (s *SQLStore) UpsertWorkspaceSettings(workspace model.Workspace) error { Insert(s.tablePrefix+"workspaces"). Columns( "id", + "signup_token", "settings", "modified_by", "update_at", ). Values( workspace.ID, + signupToken, settingsJSON, workspace.ModifiedBy, now, diff --git a/server/services/store/storetests/workspaces.go b/server/services/store/storetests/workspaces.go new file mode 100644 index 000000000..8356f8d2f --- /dev/null +++ b/server/services/store/storetests/workspaces.go @@ -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) + }) +}