Change the implementation of GetAll() in preference_store.go to ret… (#10926)

* Change the implementation of GetAll() in `preference_store.go` to return an
object from `model` and a *model.AppError.
Change the Interface in `store.go` to accomodate for the change
Change the test that called the GetAll() function

* Rename the result variable to preferences so it makes more sense.
Use assertions to keep the test consistent

* Generate the correct mocks after the code changes

* Remove redundant conditions

* Address govet errors

* Resolve conflicts with master's new changes

* Fix Save() function according to the new changes in master (Got
overwritten with the previous commit)

* Change the assertions to have the same format as commit 2d97f01
This commit is contained in:
Giorgos Christos Dimitriou
2019-06-03 12:25:04 +01:00
committed by Dean Whillier
parent d1f81842a5
commit 2e79ae9636
5 changed files with 58 additions and 53 deletions

View File

@@ -10,12 +10,12 @@ import (
)
func (a *App) GetPreferencesForUser(userId string) (model.Preferences, *model.AppError) {
result := <-a.Srv.Store.Preference().GetAll(userId)
if result.Err != nil {
result.Err.StatusCode = http.StatusBadRequest
return nil, result.Err
preferences, err := a.Srv.Store.Preference().GetAll(userId)
if err != nil {
err.StatusCode = http.StatusBadRequest
return nil, err
}
return result.Data.(model.Preferences), nil
return preferences, nil
}
func (a *App) GetPreferenceByCategoryForUser(userId string, category string) (model.Preferences, *model.AppError) {

View File

@@ -192,22 +192,19 @@ func (s SqlPreferenceStore) GetCategory(userId string, category string) (model.P
}
func (s SqlPreferenceStore) GetAll(userId string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
var preferences model.Preferences
func (s SqlPreferenceStore) GetAll(userId string) (model.Preferences, *model.AppError) {
var preferences model.Preferences
if _, err := s.GetReplica().Select(&preferences,
`SELECT
if _, err := s.GetReplica().Select(&preferences,
`SELECT
*
FROM
Preferences
WHERE
UserId = :UserId`, map[string]interface{}{"UserId": userId}); err != nil {
result.Err = model.NewAppError("SqlPreferenceStore.GetAll", "store.sql_preference.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
result.Data = preferences
}
})
return nil, model.NewAppError("SqlPreferenceStore.GetAll", "store.sql_preference.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return preferences, nil
}
func (s SqlPreferenceStore) PermanentDeleteByUser(userId string) *model.AppError {

View File

@@ -431,7 +431,7 @@ type PreferenceStore interface {
Save(preferences *model.Preferences) *model.AppError
GetCategory(userId string, category string) (model.Preferences, *model.AppError)
Get(userId string, category string, name string) (*model.Preference, *model.AppError)
GetAll(userId string) StoreChannel
GetAll(userId string) (model.Preferences, *model.AppError)
Delete(userId, category, name string) StoreChannel
DeleteCategory(userId string, category string) *model.AppError
DeleteCategoryAndName(category string, name string) *model.AppError

View File

@@ -110,19 +110,28 @@ func (_m *PreferenceStore) Get(userId string, category string, name string) (*mo
}
// GetAll provides a mock function with given fields: userId
func (_m *PreferenceStore) GetAll(userId string) store.StoreChannel {
func (_m *PreferenceStore) GetAll(userId string) (model.Preferences, *model.AppError) {
ret := _m.Called(userId)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok {
var r0 model.Preferences
if rf, ok := ret.Get(0).(func(string) model.Preferences); ok {
r0 = rf(userId)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(model.Preferences)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
r1 = rf(userId)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// GetCategory provides a mock function with given fields: userId, category

View File

@@ -6,9 +6,8 @@ package storetest
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
@@ -194,13 +193,13 @@ func testPreferenceGetAll(t *testing.T, ss store.Store) {
err := ss.Preference().Save(&preferences)
require.Nil(t, err)
if result := <-ss.Preference().GetAll(userId); result.Err != nil {
t.Fatal(result.Err)
} else if data := result.Data.(model.Preferences); len(data) != 3 {
if result, err := ss.Preference().GetAll(userId); err != nil {
t.Fatal(err)
} else if len(result) != 3 {
t.Fatal("got the wrong number of preferences")
} else {
for i := 0; i < 3; i++ {
if data[0] != preferences[i] && data[1] != preferences[i] && data[2] != preferences[i] {
if result[0] != preferences[i] && result[1] != preferences[i] && result[2] != preferences[i] {
t.Fatal("got incorrect preferences")
}
}
@@ -328,17 +327,17 @@ func testPreferenceDelete(t *testing.T, ss store.Store) {
err := ss.Preference().Save(&model.Preferences{preference})
require.Nil(t, err)
if prefs := store.Must(ss.Preference().GetAll(preference.UserId)).(model.Preferences); len([]model.Preference(prefs)) != 1 {
t.Fatal("should've returned 1 preference")
}
preferences, err := ss.Preference().GetAll(preference.UserId)
require.Nil(t, err)
assert.Len(t, preferences, 1, "should've returned 1 preference")
if result := <-ss.Preference().Delete(preference.UserId, preference.Category, preference.Name); result.Err != nil {
t.Fatal(result.Err)
}
preferences, err = ss.Preference().GetAll(preference.UserId)
require.Nil(t, err)
assert.Len(t, preferences, 0, "should've returned no preferences")
if prefs := store.Must(ss.Preference().GetAll(preference.UserId)).(model.Preferences); len([]model.Preference(prefs)) != 0 {
t.Fatal("should've returned no preferences")
}
}
func testPreferenceDeleteCategory(t *testing.T, ss store.Store) {
@@ -362,17 +361,17 @@ func testPreferenceDeleteCategory(t *testing.T, ss store.Store) {
err := ss.Preference().Save(&model.Preferences{preference1, preference2})
require.Nil(t, err)
if prefs := store.Must(ss.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 2 {
t.Fatal("should've returned 2 preferences")
}
preferences, err := ss.Preference().GetAll(userId)
require.Nil(t, err)
assert.Len(t, preferences, 2, "should've returned 2 preferences")
if err := ss.Preference().DeleteCategory(userId, category); err != nil {
if err = ss.Preference().DeleteCategory(userId, category); err != nil {
t.Fatal(err)
}
if prefs := store.Must(ss.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 0 {
t.Fatal("should've returned no preferences")
}
preferences, err = ss.Preference().GetAll(userId)
require.Nil(t, err)
assert.Len(t, preferences, 0, "should've returned no preferences")
}
func testPreferenceDeleteCategoryAndName(t *testing.T, ss store.Store) {
@@ -398,25 +397,25 @@ func testPreferenceDeleteCategoryAndName(t *testing.T, ss store.Store) {
err := ss.Preference().Save(&model.Preferences{preference1, preference2})
require.Nil(t, err)
if prefs := store.Must(ss.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 1 {
t.Fatal("should've returned 1 preference")
}
preferences, err := ss.Preference().GetAll(userId)
require.Nil(t, err)
assert.Len(t, preferences, 1, "should've returned 1 preference")
if prefs := store.Must(ss.Preference().GetAll(userId2)).(model.Preferences); len([]model.Preference(prefs)) != 1 {
t.Fatal("should've returned 1 preference")
}
preferences, err = ss.Preference().GetAll(userId2)
require.Nil(t, err)
assert.Len(t, preferences, 1, "should've returned 1 preference")
if err := ss.Preference().DeleteCategoryAndName(category, name); err != nil {
if err = ss.Preference().DeleteCategoryAndName(category, name); err != nil {
t.Fatal(err)
}
if prefs := store.Must(ss.Preference().GetAll(userId)).(model.Preferences); len([]model.Preference(prefs)) != 0 {
t.Fatal("should've returned no preferences")
}
preferences, err = ss.Preference().GetAll(userId)
require.Nil(t, err)
assert.Len(t, preferences, 0, "should've returned no preference")
if prefs := store.Must(ss.Preference().GetAll(userId2)).(model.Preferences); len([]model.Preference(prefs)) != 0 {
t.Fatal("should've returned no preferences")
}
preferences, err = ss.Preference().GetAll(userId2)
require.Nil(t, err)
assert.Len(t, preferences, 0, "should've returned no preference")
}
func testPreferenceCleanupFlagsBatch(t *testing.T, ss store.Store) {