[fix] unit-test

This commit is contained in:
Max 2022-10-18 23:12:35 +08:00
parent 355ae10dcf
commit 84279e9bfc
2 changed files with 13 additions and 3 deletions

View file

@ -1,6 +1,7 @@
package i18n package i18n
import ( import (
"crypto/sha256"
"fmt" "fmt"
"path/filepath" "path/filepath"
"strings" "strings"
@ -76,7 +77,9 @@ func Load(cfg config.Config) error {
func Trans(langName string, widgets []string, data interface{}) (interface{}, error) { func Trans(langName string, widgets []string, data interface{}) (interface{}, error) {
// Get From cache // Get From cache
key := fmt.Sprintf("%s::%s", langName, strings.Join(widgets, "::")) hash := sha256.Sum256([]byte(fmt.Sprintf("%v", data)))
key := fmt.Sprintf("%s::%s::%s", langName, strings.Join(widgets, "::"), hash)
if res, has := cache.data[key]; has { if res, has := cache.data[key]; has {
return res, nil return res, nil
} }
@ -99,7 +102,8 @@ func Trans(langName string, widgets []string, data interface{}) (interface{}, er
func cacheSet(dict *lang.Dict, widgets []string, value interface{}) { func cacheSet(dict *lang.Dict, widgets []string, value interface{}) {
cache.mu.Lock() cache.mu.Lock()
defer cache.mu.Unlock() defer cache.mu.Unlock()
key := fmt.Sprintf("%s::%s", dict.Name, strings.Join(widgets, "::")) hash := sha256.Sum256([]byte(fmt.Sprintf("%v", value)))
key := fmt.Sprintf("%s::%s::%s", dict.Name, strings.Join(widgets, "::"), hash)
cache.data[key] = value cache.data[key] = value
} }

View file

@ -142,6 +142,8 @@ func TestExport(t *testing.T) {
func TestProcessSetting(t *testing.T) { func TestProcessSetting(t *testing.T) {
loadApp(t) loadApp(t)
backup := config.Conf.Lang
config.Conf.Lang = "en-us"
res, err := gou.NewProcess("yao.app.Setting").Exec() res, err := gou.NewProcess("yao.app.Setting").Exec()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -169,10 +171,14 @@ func TestProcessSetting(t *testing.T) {
lang := gou.NewProcess("yao.app.Setting").WithSID(setting.Sid).Lang() lang := gou.NewProcess("yao.app.Setting").WithSID(setting.Sid).Lang()
assert.Equal(t, "zh-hk", lang) assert.Equal(t, "zh-hk", lang)
config.Conf.Lang = backup
} }
func TestProcessXgen(t *testing.T) { func TestProcessXgen(t *testing.T) {
loadApp(t) loadApp(t)
backup := config.Conf.Lang
config.Conf.Lang = "en-us"
res, err := gou.NewProcess("yao.app.Xgen").Exec() res, err := gou.NewProcess("yao.app.Xgen").Exec()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -207,7 +213,7 @@ func TestProcessXgen(t *testing.T) {
lang := gou.NewProcess("yao.app.Setting").WithSID(xgen2.Get("sid").(string)).Lang() lang := gou.NewProcess("yao.app.Setting").WithSID(xgen2.Get("sid").(string)).Lang()
assert.Equal(t, "zh-hk", lang) assert.Equal(t, "zh-hk", lang)
config.Conf.Lang = backup
} }
func TestProcessMenu(t *testing.T) { func TestProcessMenu(t *testing.T) {