A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat: add config repo unit test
LinkinStar3c8bdcde29b6
diff --git a/internal/repo/config/config_repo.go b/internal/repo/config/config_repo.goindex 4f97ad68..329e2b65 100644--- a/internal/repo/config/config_repo.go+++ b/internal/repo/config/config_repo.go@@ -65,10 +65,14 @@ func (cr *configRepo) Get(key string) (interface{}, error) {// key stringfunc (cr *configRepo) GetString(key string) (string, error) {value, err := cr.Get(key)- if value != nil {- return value.(string), err+ if err != nil {+ return "", err+ }+ str, ok := value.(string)+ if !ok {+ return "", errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("config value is wrong type: %v", key))}- return "", err+ 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 typefunc (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 configfunc (cr *configRepo) SetConfig(key, value string) (err error) {id := Key2IDMapping[key]_, err = cr.data.DB.ID(id).Update(&entity.Config{Value: value})diff --git a/internal/repo/repo_test/config_repo_test.go b/internal/repo/repo_test/config_repo_test.gonew file mode 100644index 00000000..5f76ddef--- /dev/null+++ b/internal/repo/repo_test/config_repo_test.go@@ -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)+}diff --git a/internal/service/config/config_service.go b/internal/service/config/config_service.goindex 818b7f28..882b8fa5 100644--- a/internal/service/config/config_service.go+++ b/internal/service/config/config_service.go@@ -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)}diff --git a/internal/service/question_common/question.go b/internal/service/question_common/question.goindex c9c105c8..98b9db8a 100644--- a/internal/service/question_common/question.go+++ b/internal/service/question_common/question.go@@ -156,7 +156,7 @@ func (qs *QuestionCommon) Info(ctx context.Context, questionId string, loginUserlog.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 {diff --git a/internal/service/report_backyard/report_backyard.go b/internal/service/report_backyard/report_backyard.goindex 20249fce..947e7197 100644--- a/internal/service/report_backyard/report_backyard.go+++ b/internal/service/report_backyard/report_backyard.go@@ -201,13 +201,13 @@ func (rs *ReportBackyardService) parseObject(ctx context.Context, resp *[]*schemr.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