Migrate TermsOfService and UserTermsOfService stores to sync by default (#11282)

Этот коммит содержится в:
Jesús Espino
2019-07-02 16:22:20 +02:00
коммит произвёл GitHub
родитель 1050aacb42
Коммит 76ac7d8ef0
9 изменённых файлов: 209 добавлений и 252 удалений

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

@@ -17,29 +17,13 @@ func (a *App) CreateTermsOfService(text, userId string) (*model.TermsOfService,
return nil, err return nil, err
} }
result := <-a.Srv.Store.TermsOfService().Save(termsOfService) return a.Srv.Store.TermsOfService().Save(termsOfService)
if result.Err != nil {
return nil, result.Err
}
termsOfService = result.Data.(*model.TermsOfService)
return termsOfService, nil
} }
func (a *App) GetLatestTermsOfService() (*model.TermsOfService, *model.AppError) { func (a *App) GetLatestTermsOfService() (*model.TermsOfService, *model.AppError) {
if result := <-a.Srv.Store.TermsOfService().GetLatest(true); result.Err != nil { return a.Srv.Store.TermsOfService().GetLatest(true)
return nil, result.Err
} else {
termsOfService := result.Data.(*model.TermsOfService)
return termsOfService, nil
}
} }
func (a *App) GetTermsOfService(id string) (*model.TermsOfService, *model.AppError) { func (a *App) GetTermsOfService(id string) (*model.TermsOfService, *model.AppError) {
if result := <-a.Srv.Store.TermsOfService().Get(id, true); result.Err != nil { return a.Srv.Store.TermsOfService().Get(id, true)
return nil, result.Err
} else {
termsOfService := result.Data.(*model.TermsOfService)
return termsOfService, nil
}
} }

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

@@ -6,11 +6,7 @@ package app
import "github.com/mattermost/mattermost-server/model" import "github.com/mattermost/mattermost-server/model"
func (a *App) GetUserTermsOfService(userId string) (*model.UserTermsOfService, *model.AppError) { func (a *App) GetUserTermsOfService(userId string) (*model.UserTermsOfService, *model.AppError) {
if result := <-a.Srv.Store.UserTermsOfService().GetByUser(userId); result.Err != nil { return a.Srv.Store.UserTermsOfService().GetByUser(userId)
return nil, result.Err
} else {
return result.Data.(*model.UserTermsOfService), nil
}
} }
func (a *App) SaveUserTermsOfService(userId, termsOfServiceId string, accepted bool) *model.AppError { func (a *App) SaveUserTermsOfService(userId, termsOfServiceId string, accepted bool) *model.AppError {
@@ -20,12 +16,12 @@ func (a *App) SaveUserTermsOfService(userId, termsOfServiceId string, accepted b
TermsOfServiceId: termsOfServiceId, TermsOfServiceId: termsOfServiceId,
} }
if result := <-a.Srv.Store.UserTermsOfService().Save(userTermsOfService); result.Err != nil { if _, err := a.Srv.Store.UserTermsOfService().Save(userTermsOfService); err != nil {
return result.Err return err
} }
} else { } else {
if result := <-a.Srv.Store.UserTermsOfService().Delete(userId, termsOfServiceId); result.Err != nil { if err := a.Srv.Store.UserTermsOfService().Delete(userId, termsOfServiceId); err != nil {
return result.Err return err
} }
} }

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

@@ -40,107 +40,81 @@ func NewSqlTermsOfServiceStore(sqlStore SqlStore, metrics einterfaces.MetricsInt
func (s SqlTermsOfServiceStore) CreateIndexesIfNotExists() { func (s SqlTermsOfServiceStore) CreateIndexesIfNotExists() {
} }
func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) store.StoreChannel { func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
return store.Do(func(result *store.StoreResult) { if len(termsOfService.Id) > 0 {
if len(termsOfService.Id) > 0 { return nil, model.NewAppError("SqlTermsOfServiceStore.Save", "store.sql_terms_of_service_store.save.existing.app_error", nil, "id="+termsOfService.Id, http.StatusBadRequest)
result.Err = model.NewAppError( }
"SqlTermsOfServiceStore.Save",
"store.sql_terms_of_service_store.save.existing.app_error", termsOfService.PreSave()
nil,
"id="+termsOfService.Id, http.StatusBadRequest, if err := termsOfService.IsValid(); err != nil {
) return nil, err
return }
if err := s.GetMaster().Insert(termsOfService); err != nil {
return nil, model.NewAppError("SqlTermsOfServiceStore.Save", "store.sql_terms_of_service.save.app_error", nil, "terms_of_service_id="+termsOfService.Id+",err="+err.Error(), http.StatusInternalServerError)
}
termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
return termsOfService, nil
}
func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
if allowFromCache {
if termsOfServiceCache.Len() != 0 {
if cacheItem, ok := termsOfServiceCache.Get(termsOfServiceCache.Keys()[0]); ok {
if s.metrics != nil {
s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
}
return cacheItem.(*model.TermsOfService), nil
}
} }
}
termsOfService.PreSave() if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
if result.Err = termsOfService.IsValid(); result.Err != nil { var termsOfService *model.TermsOfService
return
err := s.GetReplica().SelectOne(&termsOfService, "SELECT * FROM TermsOfService ORDER BY CreateAt DESC LIMIT 1")
if err != nil {
if err == sql.ErrNoRows {
return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "err="+err.Error(), http.StatusNotFound)
} }
return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
}
if err := s.GetMaster().Insert(termsOfService); err != nil { if allowFromCache {
result.Err = model.NewAppError(
"SqlTermsOfServiceStore.Save",
"store.sql_terms_of_service.save.app_error",
nil,
"terms_of_service_id="+termsOfService.Id+",err="+err.Error(),
http.StatusInternalServerError,
)
}
result.Data = termsOfService
termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService) termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
}) }
return termsOfService, nil
} }
func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) store.StoreChannel { func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
return store.Do(func(result *store.StoreResult) { if allowFromCache {
if allowFromCache { if termsOfServiceCache.Len() != 0 {
if termsOfServiceCache.Len() == 0 { if cacheItem, ok := termsOfServiceCache.Get(id); ok {
if s.metrics != nil { if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName) s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
} }
} else {
if cacheItem, ok := termsOfServiceCache.Get(termsOfServiceCache.Keys()[0]); ok {
if s.metrics != nil {
s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
}
result.Data = cacheItem.(*model.TermsOfService) return cacheItem.(*model.TermsOfService), nil
return
} else if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
} }
} }
}
if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
var termsOfService *model.TermsOfService obj, err := s.GetReplica().Get(model.TermsOfService{}, id)
if err != nil {
err := s.GetReplica().SelectOne(&termsOfService, "SELECT * FROM TermsOfService ORDER BY CreateAt DESC LIMIT 1") return nil, model.NewAppError("SqlTermsOfServiceStore.Get", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
if err != nil { }
if err == sql.ErrNoRows { if obj == nil {
result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "err="+err.Error(), http.StatusNotFound) return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "", http.StatusNotFound)
} else { }
result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) return obj.(*model.TermsOfService), nil
}
} else {
result.Data = termsOfService
if allowFromCache {
termsOfServiceCache.AddWithDefaultExpires(termsOfService.Id, termsOfService)
}
}
})
}
func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if allowFromCache {
if termsOfServiceCache.Len() == 0 {
if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
} else {
if cacheItem, ok := termsOfServiceCache.Get(id); ok {
if s.metrics != nil {
s.metrics.IncrementMemCacheHitCounter(termsOfServiceCacheName)
}
result.Data = cacheItem.(*model.TermsOfService)
return
} else if s.metrics != nil {
s.metrics.IncrementMemCacheMissCounter(termsOfServiceCacheName)
}
}
}
if obj, err := s.GetReplica().Get(model.TermsOfService{}, id); err != nil {
result.Err = model.NewAppError("SqlTermsOfServiceStore.Get", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
} else if obj == nil {
result.Err = model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "", http.StatusNotFound)
} else {
result.Data = obj.(*model.TermsOfService)
}
})
} }

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

@@ -31,60 +31,43 @@ func (s SqlUserTermsOfServiceStore) CreateIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_user_terms_of_service_user_id", "UserTermsOfService", "UserId") s.CreateIndexIfNotExists("idx_user_terms_of_service_user_id", "UserTermsOfService", "UserId")
} }
func (s SqlUserTermsOfServiceStore) GetByUser(userId string) store.StoreChannel { func (s SqlUserTermsOfServiceStore) GetByUser(userId string) (*model.UserTermsOfService, *model.AppError) {
return store.Do(func(result *store.StoreResult) { var userTermsOfService *model.UserTermsOfService
var userTermsOfService *model.UserTermsOfService
err := s.GetReplica().SelectOne(&userTermsOfService, "SELECT * FROM UserTermsOfService WHERE UserId = :userId", map[string]interface{}{"userId": userId}) err := s.GetReplica().SelectOne(&userTermsOfService, "SELECT * FROM UserTermsOfService WHERE UserId = :userId", map[string]interface{}{"userId": userId})
if err != nil { if err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
result.Err = model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", nil, "", http.StatusNotFound) return nil, model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", nil, "", http.StatusNotFound)
} else {
result.Err = model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.app_error", nil, "", http.StatusInternalServerError)
}
} else {
result.Data = userTermsOfService
} }
}) return nil, model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.app_error", nil, "", http.StatusInternalServerError)
}
return userTermsOfService, nil
} }
func (s SqlUserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) store.StoreChannel { func (s SqlUserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) (*model.UserTermsOfService, *model.AppError) {
return store.Do(func(result *store.StoreResult) { userTermsOfService.PreSave()
userTermsOfService.PreSave()
if result.Err = userTermsOfService.IsValid(); result.Err != nil { if err := userTermsOfService.IsValid(); err != nil {
return return nil, err
}
c, err := s.GetMaster().Update(userTermsOfService)
if err != nil {
return nil, model.NewAppError("SqlUserTermsOfServiceStore.Save", "store.sql_user_terms_of_service.save.app_error", nil, "user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(), http.StatusInternalServerError)
}
if c == 0 {
if err := s.GetMaster().Insert(userTermsOfService); err != nil {
return nil, model.NewAppError("SqlUserTermsOfServiceStore.Save", "store.sql_user_terms_of_service.save.app_error", nil, "user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(), http.StatusInternalServerError)
} }
}
if c, err := s.GetMaster().Update(userTermsOfService); err != nil { return userTermsOfService, nil
result.Err = model.NewAppError(
"SqlUserTermsOfServiceStore.Save",
"store.sql_user_terms_of_service.save.app_error",
nil,
"user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(),
http.StatusInternalServerError,
)
} else if c == 0 {
if err := s.GetMaster().Insert(userTermsOfService); err != nil {
result.Err = model.NewAppError(
"SqlUserTermsOfServiceStore.Save",
"store.sql_user_terms_of_service.save.app_error",
nil,
"user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(),
http.StatusInternalServerError,
)
}
}
result.Data = userTermsOfService
})
} }
func (s SqlUserTermsOfServiceStore) Delete(userId, termsOfServiceId string) store.StoreChannel { func (s SqlUserTermsOfServiceStore) Delete(userId, termsOfServiceId string) *model.AppError {
return store.Do(func(result *store.StoreResult) { if _, err := s.GetMaster().Exec("DELETE FROM UserTermsOfService WHERE UserId = :UserId AND TermsOfServiceId = :TermsOfServiceId", map[string]interface{}{"UserId": userId, "TermsOfServiceId": termsOfServiceId}); err != nil {
if _, err := s.GetMaster().Exec("DELETE FROM UserTermsOfService WHERE UserId = :UserId AND TermsOfServiceId = :TermsOfServiceId", map[string]interface{}{"UserId": userId, "TermsOfServiceId": termsOfServiceId}); err != nil { return model.NewAppError("SqlUserTermsOfServiceStore.Delete", "store.sql_user_terms_of_service.delete.app_error", nil, "userId="+userId+", termsOfServiceId="+termsOfServiceId, http.StatusInternalServerError)
result.Err = model.NewAppError("SqlUserTermsOfServiceStore.Delete", "store.sql_user_terms_of_service.delete.app_error", nil, "userId="+userId+", termsOfServiceId="+termsOfServiceId, http.StatusInternalServerError) }
return return nil
}
})
} }

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

@@ -561,15 +561,15 @@ type SchemeStore interface {
} }
type TermsOfServiceStore interface { type TermsOfServiceStore interface {
Save(termsOfService *model.TermsOfService) StoreChannel Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError)
GetLatest(allowFromCache bool) StoreChannel GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError)
Get(id string, allowFromCache bool) StoreChannel Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError)
} }
type UserTermsOfServiceStore interface { type UserTermsOfServiceStore interface {
GetByUser(userId string) StoreChannel GetByUser(userId string) (*model.UserTermsOfService, *model.AppError)
Save(userTermsOfService *model.UserTermsOfService) StoreChannel Save(userTermsOfService *model.UserTermsOfService) (*model.UserTermsOfService, *model.AppError)
Delete(userId, termsOfServiceId string) StoreChannel Delete(userId, termsOfServiceId string) *model.AppError
} }
type GroupStore interface { type GroupStore interface {

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

@@ -6,7 +6,6 @@ package mocks
import mock "github.com/stretchr/testify/mock" import mock "github.com/stretchr/testify/mock"
import model "github.com/mattermost/mattermost-server/model" import model "github.com/mattermost/mattermost-server/model"
import store "github.com/mattermost/mattermost-server/store"
// TermsOfServiceStore is an autogenerated mock type for the TermsOfServiceStore type // TermsOfServiceStore is an autogenerated mock type for the TermsOfServiceStore type
type TermsOfServiceStore struct { type TermsOfServiceStore struct {
@@ -14,49 +13,76 @@ type TermsOfServiceStore struct {
} }
// Get provides a mock function with given fields: id, allowFromCache // Get provides a mock function with given fields: id, allowFromCache
func (_m *TermsOfServiceStore) Get(id string, allowFromCache bool) store.StoreChannel { func (_m *TermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) {
ret := _m.Called(id, allowFromCache) ret := _m.Called(id, allowFromCache)
var r0 store.StoreChannel var r0 *model.TermsOfService
if rf, ok := ret.Get(0).(func(string, bool) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, bool) *model.TermsOfService); ok {
r0 = rf(id, allowFromCache) r0 = rf(id, allowFromCache)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.TermsOfService)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, bool) *model.AppError); ok {
r1 = rf(id, allowFromCache)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetLatest provides a mock function with given fields: allowFromCache // GetLatest provides a mock function with given fields: allowFromCache
func (_m *TermsOfServiceStore) GetLatest(allowFromCache bool) store.StoreChannel { func (_m *TermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) {
ret := _m.Called(allowFromCache) ret := _m.Called(allowFromCache)
var r0 store.StoreChannel var r0 *model.TermsOfService
if rf, ok := ret.Get(0).(func(bool) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(bool) *model.TermsOfService); ok {
r0 = rf(allowFromCache) r0 = rf(allowFromCache)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.TermsOfService)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(bool) *model.AppError); ok {
r1 = rf(allowFromCache)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// Save provides a mock function with given fields: termsOfService // Save provides a mock function with given fields: termsOfService
func (_m *TermsOfServiceStore) Save(termsOfService *model.TermsOfService) store.StoreChannel { func (_m *TermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) {
ret := _m.Called(termsOfService) ret := _m.Called(termsOfService)
var r0 store.StoreChannel var r0 *model.TermsOfService
if rf, ok := ret.Get(0).(func(*model.TermsOfService) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(*model.TermsOfService) *model.TermsOfService); ok {
r0 = rf(termsOfService) r0 = rf(termsOfService)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.TermsOfService)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.TermsOfService) *model.AppError); ok {
r1 = rf(termsOfService)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }

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

@@ -6,7 +6,6 @@ package mocks
import mock "github.com/stretchr/testify/mock" import mock "github.com/stretchr/testify/mock"
import model "github.com/mattermost/mattermost-server/model" import model "github.com/mattermost/mattermost-server/model"
import store "github.com/mattermost/mattermost-server/store"
// UserTermsOfServiceStore is an autogenerated mock type for the UserTermsOfServiceStore type // UserTermsOfServiceStore is an autogenerated mock type for the UserTermsOfServiceStore type
type UserTermsOfServiceStore struct { type UserTermsOfServiceStore struct {
@@ -14,15 +13,15 @@ type UserTermsOfServiceStore struct {
} }
// Delete provides a mock function with given fields: userId, termsOfServiceId // Delete provides a mock function with given fields: userId, termsOfServiceId
func (_m *UserTermsOfServiceStore) Delete(userId string, termsOfServiceId string) store.StoreChannel { func (_m *UserTermsOfServiceStore) Delete(userId string, termsOfServiceId string) *model.AppError {
ret := _m.Called(userId, termsOfServiceId) ret := _m.Called(userId, termsOfServiceId)
var r0 store.StoreChannel var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string, string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, string) *model.AppError); ok {
r0 = rf(userId, termsOfServiceId) r0 = rf(userId, termsOfServiceId)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AppError)
} }
} }
@@ -30,33 +29,51 @@ func (_m *UserTermsOfServiceStore) Delete(userId string, termsOfServiceId string
} }
// GetByUser provides a mock function with given fields: userId // GetByUser provides a mock function with given fields: userId
func (_m *UserTermsOfServiceStore) GetByUser(userId string) store.StoreChannel { func (_m *UserTermsOfServiceStore) GetByUser(userId string) (*model.UserTermsOfService, *model.AppError) {
ret := _m.Called(userId) ret := _m.Called(userId)
var r0 store.StoreChannel var r0 *model.UserTermsOfService
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.UserTermsOfService); ok {
r0 = rf(userId) r0 = rf(userId)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.UserTermsOfService)
} }
} }
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
} }
// Save provides a mock function with given fields: userTermsOfService // Save provides a mock function with given fields: userTermsOfService
func (_m *UserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) store.StoreChannel { func (_m *UserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) (*model.UserTermsOfService, *model.AppError) {
ret := _m.Called(userTermsOfService) ret := _m.Called(userTermsOfService)
var r0 store.StoreChannel var r0 *model.UserTermsOfService
if rf, ok := ret.Get(0).(func(*model.UserTermsOfService) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(*model.UserTermsOfService) *model.UserTermsOfService); ok {
r0 = rf(userTermsOfService) r0 = rf(userTermsOfService)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.UserTermsOfService)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(*model.UserTermsOfService) *model.AppError); ok {
r1 = rf(userTermsOfService)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }

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

@@ -9,6 +9,7 @@ import (
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/store"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestTermsOfServiceStore(t *testing.T, ss store.Store) { func TestTermsOfServiceStore(t *testing.T, ss store.Store) {
@@ -25,13 +26,9 @@ func testSaveTermsOfService(t *testing.T, ss store.Store) {
store.Must(ss.User().Save(&u1)) store.Must(ss.User().Save(&u1))
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id} termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
r1 := <-ss.TermsOfService().Save(termsOfService) savedTermsOfService, err := ss.TermsOfService().Save(termsOfService)
require.Nil(t, err)
if r1.Err != nil {
t.Fatal(r1.Err)
}
savedTermsOfService := r1.Data.(*model.TermsOfService)
if len(savedTermsOfService.Id) != 26 { if len(savedTermsOfService.Id) != 26 {
t.Fatal("Id should have been populated") t.Fatal("Id should have been populated")
} }
@@ -49,14 +46,10 @@ func testGetLatestTermsOfService(t *testing.T, ss store.Store) {
store.Must(ss.User().Save(&u1)) store.Must(ss.User().Save(&u1))
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id} termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
store.Must(ss.TermsOfService().Save(termsOfService)) _, err := ss.TermsOfService().Save(termsOfService)
r1 := <-ss.TermsOfService().GetLatest(true) fetchedTermsOfService, err := ss.TermsOfService().GetLatest(true)
if r1.Err != nil { require.Nil(t, err)
t.Fatal(r1.Err)
}
fetchedTermsOfService := r1.Data.(*model.TermsOfService)
assert.Equal(t, termsOfService.Text, fetchedTermsOfService.Text) assert.Equal(t, termsOfService.Text, fetchedTermsOfService.Text)
assert.Equal(t, termsOfService.UserId, fetchedTermsOfService.UserId) assert.Equal(t, termsOfService.UserId, fetchedTermsOfService.UserId)
} }
@@ -69,15 +62,14 @@ func testGetTermsOfService(t *testing.T, ss store.Store) {
store.Must(ss.User().Save(&u1)) store.Must(ss.User().Save(&u1))
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id} termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
store.Must(ss.TermsOfService().Save(termsOfService)) _, err := ss.TermsOfService().Save(termsOfService)
require.Nil(t, err)
r1 := <-ss.TermsOfService().Get("an_invalid_id", true) r1, err := ss.TermsOfService().Get("an_invalid_id", true)
assert.NotNil(t, r1.Err) assert.NotNil(t, err)
assert.Nil(t, r1.Data) assert.Nil(t, r1)
r1 = <-ss.TermsOfService().Get(termsOfService.Id, true) receivedTermsOfService, err := ss.TermsOfService().Get(termsOfService.Id, true)
assert.Nil(t, r1.Err) assert.Nil(t, err)
receivedTermsOfService := r1.Data.(*model.TermsOfService)
assert.Equal(t, "terms of service", receivedTermsOfService.Text) assert.Equal(t, "terms of service", receivedTermsOfService.Text)
} }

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

@@ -9,6 +9,7 @@ import (
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store" "github.com/mattermost/mattermost-server/store"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestUserTermsOfServiceStore(t *testing.T, ss store.Store) { func TestUserTermsOfServiceStore(t *testing.T, ss store.Store) {
@@ -23,12 +24,8 @@ func testSaveUserTermsOfService(t *testing.T, ss store.Store) {
TermsOfServiceId: model.NewId(), TermsOfServiceId: model.NewId(),
} }
r1 := <-ss.UserTermsOfService().Save(userTermsOfService) savedUserTermsOfService, err := ss.UserTermsOfService().Save(userTermsOfService)
if r1.Err != nil { require.Nil(t, err)
t.Fatal(r1.Err)
}
savedUserTermsOfService := r1.Data.(*model.UserTermsOfService)
assert.Equal(t, userTermsOfService.UserId, savedUserTermsOfService.UserId) assert.Equal(t, userTermsOfService.UserId, savedUserTermsOfService.UserId)
assert.Equal(t, userTermsOfService.TermsOfServiceId, savedUserTermsOfService.TermsOfServiceId) assert.Equal(t, userTermsOfService.TermsOfServiceId, savedUserTermsOfService.TermsOfServiceId)
assert.NotEmpty(t, savedUserTermsOfService.CreateAt) assert.NotEmpty(t, savedUserTermsOfService.CreateAt)
@@ -40,17 +37,11 @@ func testGetByUserTermsOfService(t *testing.T, ss store.Store) {
TermsOfServiceId: model.NewId(), TermsOfServiceId: model.NewId(),
} }
r1 := <-ss.UserTermsOfService().Save(userTermsOfService) _, err := ss.UserTermsOfService().Save(userTermsOfService)
if r1.Err != nil { require.Nil(t, err)
t.Fatal(r1.Err)
}
r1 = <-ss.UserTermsOfService().GetByUser(userTermsOfService.UserId) fetchedUserTermsOfService, err := ss.UserTermsOfService().GetByUser(userTermsOfService.UserId)
if r1.Err != nil { require.Nil(t, err)
t.Fatal(r1.Err)
}
fetchedUserTermsOfService := r1.Data.(*model.UserTermsOfService)
assert.Equal(t, userTermsOfService.UserId, fetchedUserTermsOfService.UserId) assert.Equal(t, userTermsOfService.UserId, fetchedUserTermsOfService.UserId)
assert.Equal(t, userTermsOfService.TermsOfServiceId, fetchedUserTermsOfService.TermsOfServiceId) assert.Equal(t, userTermsOfService.TermsOfServiceId, fetchedUserTermsOfService.TermsOfServiceId)
assert.NotEmpty(t, fetchedUserTermsOfService.CreateAt) assert.NotEmpty(t, fetchedUserTermsOfService.CreateAt)
@@ -62,21 +53,15 @@ func testDeleteUserTermsOfService(t *testing.T, ss store.Store) {
TermsOfServiceId: model.NewId(), TermsOfServiceId: model.NewId(),
} }
r1 := <-ss.UserTermsOfService().Save(userTermsOfService) _, err := ss.UserTermsOfService().Save(userTermsOfService)
if r1.Err != nil { require.Nil(t, err)
t.Fatal(r1.Err)
}
r1 = <-ss.UserTermsOfService().GetByUser(userTermsOfService.UserId) _, err = ss.UserTermsOfService().GetByUser(userTermsOfService.UserId)
if r1.Err != nil { require.Nil(t, err)
t.Fatal(r1.Err)
}
r1 = <-ss.UserTermsOfService().Delete(userTermsOfService.UserId, userTermsOfService.TermsOfServiceId) err = ss.UserTermsOfService().Delete(userTermsOfService.UserId, userTermsOfService.TermsOfServiceId)
if r1.Err != nil { require.Nil(t, err)
t.Fatal(r1.Err)
}
r1 = <-ss.UserTermsOfService().GetByUser(userTermsOfService.UserId) _, err = ss.UserTermsOfService().GetByUser(userTermsOfService.UserId)
assert.Equal(t, "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", r1.Err.Id) assert.Equal(t, "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", err.Id)
} }