[MM-28810] channel_store_categories: check if object is nil after the query (#15635)

* check if onj is nil

* channel_store_categories: add tests

* reflect review comments

* remove store declaraation
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2020-10-07 09:33:18 +03:00
коммит произвёл GitHub
родитель 6222e182ca
Коммит c67c46a684
3 изменённых файлов: 49 добавлений и 2 удалений

Просмотреть файл

@@ -6,10 +6,9 @@ package sqlstore
import (
"fmt"
"github.com/mattermost/mattermost-server/v5/store"
"github.com/mattermost/gorp"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
sq "github.com/Masterminds/squirrel"
"github.com/pkg/errors"
@@ -843,6 +842,8 @@ func (s SqlChannelStore) addChannelToFavoritesCategoryT(transaction *gorp.Transa
var channel *model.Channel
if obj, err := transaction.Get(&model.Channel{}, preference.Name); err != nil {
return errors.Wrapf(err, "Failed to get favorited channel with id=%s", preference.Name)
} else if obj == nil {
return store.NewErrNotFound("Channel", preference.Name)
} else {
channel = obj.(*model.Channel)
}

Просмотреть файл

@@ -107,6 +107,7 @@ func TestChannelStore(t *testing.T, ss store.Store, s SqlSupplier) {
t.Run("GetSidebarCategories", func(t *testing.T) { testGetSidebarCategories(t, ss) })
t.Run("UpdateSidebarCategories", func(t *testing.T) { testUpdateSidebarCategories(t, ss, s) })
t.Run("DeleteSidebarCategory", func(t *testing.T) { testDeleteSidebarCategory(t, ss, s) })
t.Run("UpdateSidebarChannelsByPreferences", func(t *testing.T) { testUpdateSidebarChannelsByPreferences(t, ss) })
}
func testChannelStoreSave(t *testing.T, ss store.Store) {

Просмотреть файл

@@ -21,6 +21,7 @@ func TestChannelStoreCategories(t *testing.T, ss store.Store, s SqlSupplier) {
t.Run("GetSidebarCategories", func(t *testing.T) { testGetSidebarCategories(t, ss) })
t.Run("UpdateSidebarCategories", func(t *testing.T) { testUpdateSidebarCategories(t, ss, s) })
t.Run("DeleteSidebarCategory", func(t *testing.T) { testDeleteSidebarCategory(t, ss, s) })
t.Run("UpdateSidebarChannelsByPreferences", func(t *testing.T) { testUpdateSidebarChannelsByPreferences(t, ss) })
}
func testCreateInitialSidebarCategories(t *testing.T, ss store.Store) {
@@ -1676,3 +1677,47 @@ func testDeleteSidebarCategory(t *testing.T, ss store.Store, s SqlSupplier) {
assert.NotNil(t, err)
})
}
func testUpdateSidebarChannelsByPreferences(t *testing.T, ss store.Store) {
t.Run("Should be able to update sidebar channels", func(t *testing.T) {
userId := model.NewId()
teamId := model.NewId()
nErr := ss.Channel().CreateInitialSidebarCategories(userId, teamId)
require.Nil(t, nErr)
channel, nErr := ss.Channel().Save(&model.Channel{
Name: "channel",
Type: model.CHANNEL_OPEN,
TeamId: teamId,
}, 10)
require.Nil(t, nErr)
err := ss.Channel().UpdateSidebarChannelsByPreferences(&model.Preferences{
model.Preference{
Name: channel.Id,
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Value: "true",
},
})
assert.NoError(t, err)
})
t.Run("Should not panic if channel is not found", func(t *testing.T) {
userId := model.NewId()
teamId := model.NewId()
nErr := ss.Channel().CreateInitialSidebarCategories(userId, teamId)
assert.Nil(t, nErr)
require.NotPanics(t, func() {
_ = ss.Channel().UpdateSidebarChannelsByPreferences(&model.Preferences{
model.Preference{
Name: "fakeid",
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
Value: "true",
},
})
})
})
}