mirror of https://gitee.com/answerdev/answer.git
feat: add config repo unit test
This commit is contained in:
parent
2e13736ee7
commit
3c8bdcde29
|
@ -65,10 +65,14 @@ func (cr *configRepo) Get(key string) (interface{}, error) {
|
|||
// key string
|
||||
func (cr *configRepo) GetString(key string) (string, error) {
|
||||
value, err := cr.Get(key)
|
||||
if value != nil {
|
||||
return value.(string), err
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "", err
|
||||
str, ok := value.(string)
|
||||
if !ok {
|
||||
return "", errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("config value is wrong type: %v", key))
|
||||
}
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// GetInt method for getting the config value to int64
|
||||
|
@ -77,9 +81,8 @@ func (cr *configRepo) GetInt(key string) (int, error) {
|
|||
value, err := cr.GetString(key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return converter.StringToInt(value), nil
|
||||
}
|
||||
return converter.StringToInt(value), nil
|
||||
}
|
||||
|
||||
// GetArrayString method for getting the config value to string array
|
||||
|
@ -96,31 +99,35 @@ func (cr *configRepo) GetArrayString(key string) ([]string, error) {
|
|||
// GetConfigType method for getting the config type
|
||||
func (cr *configRepo) GetConfigType(key string) (int, error) {
|
||||
value, ok := Key2IDMapping[key]
|
||||
if ok {
|
||||
return value, nil
|
||||
} else {
|
||||
if !ok {
|
||||
return 0, errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config type: %v", key))
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// GetConfigById get config key from config id
|
||||
func (cr *configRepo) GetConfigById(id int, value any) (err error) {
|
||||
var (
|
||||
ok = true
|
||||
key string
|
||||
conf interface{}
|
||||
)
|
||||
key, ok = ID2KeyMapping[id]
|
||||
// GetJsonConfigByIDAndSetToObject get config key from config id
|
||||
func (cr *configRepo) GetJsonConfigByIDAndSetToObject(id int, object any) (err error) {
|
||||
key, ok := ID2KeyMapping[id]
|
||||
if !ok {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config id: %v", id))
|
||||
return
|
||||
return errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config id: %v", id))
|
||||
}
|
||||
|
||||
conf, err = cr.Get(key)
|
||||
value = json.Unmarshal([]byte(conf.(string)), value)
|
||||
conf, err := cr.Get(key)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err)
|
||||
}
|
||||
str, ok := conf.(string)
|
||||
if !ok {
|
||||
return errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config id: %v", id))
|
||||
}
|
||||
err = json.Unmarshal([]byte(str), object)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config id: %v", id))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SetConfig set config
|
||||
func (cr *configRepo) SetConfig(key, value string) (err error) {
|
||||
id := Key2IDMapping[key]
|
||||
_, err = cr.data.DB.ID(id).Update(&entity.Config{Value: value})
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
package repo_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
"github.com/answerdev/answer/internal/schema"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_configRepo_Get(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
_, err := configRepo.Get("email.config")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_configRepo_GetArrayString(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
got, err := configRepo.GetArrayString("daily_rank_limit.exclude")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, len(got))
|
||||
assert.Equal(t, "answer.accepted", got[0])
|
||||
}
|
||||
|
||||
func Test_configRepo_GetConfigById(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
|
||||
closeInfo := &schema.GetReportTypeResp{}
|
||||
err := configRepo.GetJsonConfigByIDAndSetToObject(74, closeInfo)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "needs close", closeInfo.Name)
|
||||
}
|
||||
|
||||
func Test_configRepo_GetConfigType(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
configType, err := configRepo.GetConfigType("answer.accepted")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, configType)
|
||||
}
|
||||
|
||||
func Test_configRepo_GetInt(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
got, err := configRepo.GetInt("answer.accepted")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 15, got)
|
||||
}
|
||||
|
||||
func Test_configRepo_GetString(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
_, err := configRepo.GetString("email.config")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_configRepo_SetConfig(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
got, err := configRepo.GetString("email.config")
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = configRepo.SetConfig("email.config", got)
|
||||
assert.NoError(t, err)
|
||||
}
|
|
@ -7,7 +7,7 @@ type ConfigRepo interface {
|
|||
GetInt(key string) (int, error)
|
||||
GetArrayString(key string) ([]string, error)
|
||||
GetConfigType(key string) (int, error)
|
||||
GetConfigById(id int, value any) (err error)
|
||||
GetJsonConfigByIDAndSetToObject(id int, value any) (err error)
|
||||
SetConfig(key, value string) (err error)
|
||||
}
|
||||
|
||||
|
|
|
@ -156,7 +156,7 @@ func (qs *QuestionCommon) Info(ctx context.Context, questionId string, loginUser
|
|||
log.Error("json.Unmarshal CloseQuestionMeta error", err.Error())
|
||||
} else {
|
||||
closeinfo := &schema.GetReportTypeResp{}
|
||||
err = qs.configRepo.GetConfigById(closemsg.CloseType, closeinfo)
|
||||
err = qs.configRepo.GetJsonConfigByIDAndSetToObject(closemsg.CloseType, closeinfo)
|
||||
if err != nil {
|
||||
log.Error("json.Unmarshal QuestionCloseJson error", err.Error())
|
||||
} else {
|
||||
|
|
|
@ -201,13 +201,13 @@ func (rs *ReportBackyardService) parseObject(ctx context.Context, resp *[]*schem
|
|||
r.Reason = &schema.ReasonItem{
|
||||
ReasonType: r.ReportType,
|
||||
}
|
||||
err = rs.configRepo.GetConfigById(r.ReportType, r.Reason)
|
||||
err = rs.configRepo.GetJsonConfigByIDAndSetToObject(r.ReportType, r.Reason)
|
||||
}
|
||||
if r.FlaggedType > 0 {
|
||||
r.FlaggedReason = &schema.ReasonItem{
|
||||
ReasonType: r.FlaggedType,
|
||||
}
|
||||
_ = rs.configRepo.GetConfigById(r.FlaggedType, r.FlaggedReason)
|
||||
_ = rs.configRepo.GetJsonConfigByIDAndSetToObject(r.FlaggedType, r.FlaggedReason)
|
||||
}
|
||||
|
||||
res[i] = r
|
||||
|
|
Loading…
Reference in New Issue