Feat: Setting model

pull/247/head
HFO4 5 years ago
parent 29fe6047a1
commit 943c84320c

@ -5,4 +5,4 @@ git:
depth: 1
script:
- go build main.go
- go test -v ./...
- go test -v -cover ./...

@ -16,20 +16,44 @@ type Setting struct {
var settingCache = make(map[string]string)
// GetSettingByName 用 Name 获取设置值
func GetSettingByName(name string) (string, error) {
func GetSettingByName(name string) string {
var setting Setting
// 优先从缓存中查找
if optionValue, ok := settingCache[name]; ok {
return optionValue, nil
} else {
// 尝试数据库中查找
result := DB.Where("name = ?", name).First(&setting)
if result.Error == nil {
settingCache[setting.Name] = setting.Value
return setting.Value, nil
}
return "", result.Error
return optionValue
}
// 尝试数据库中查找
result := DB.Where("name = ?", name).First(&setting)
if result.Error == nil {
settingCache[setting.Name] = setting.Value
return setting.Value
}
return ""
}
// GetSettingByNames 用多个 Name 获取设置值
func GetSettingByNames(names []string) map[string]string {
var queryRes []Setting
res := make(map[string]string)
DB.Where("name IN (?)", names).Find(&queryRes)
for _, setting := range queryRes {
res[setting.Name] = setting.Value
}
return res
}
// GetSettingByType 获取一个或多个分组的所有设置值
func GetSettingByType(types []string) map[string]string {
var queryRes []Setting
res := make(map[string]string)
DB.Where("type IN (?)", types).Find(&queryRes)
for _, setting := range queryRes {
res[setting.Name] = setting.Value
}
return res
}

@ -0,0 +1,116 @@
package model
import (
"database/sql"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"testing"
)
var mock sqlmock.Sqlmock
// TestMain 初始化数据库Mock
func TestMain(m *testing.M) {
var db *sql.DB
var err error
db, mock, err = sqlmock.New()
if err != nil {
panic("An error was not expected when opening a stub database connection")
}
DB, _ = gorm.Open("mysql", db)
defer db.Close()
m.Run()
}
func TestGetSettingByType(t *testing.T) {
asserts := assert.New(t)
//找到设置时
rows := sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName", "Cloudreve", "basic").
AddRow("siteDes", "Something wonderful", "basic")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings := GetSettingByType([]string{"basic"})
asserts.Equal(map[string]string{
"siteName": "Cloudreve",
"siteDes": "Something wonderful",
}, settings)
rows = sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName", "Cloudreve", "basic").
AddRow("siteDes", "Something wonderful", "basic2")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings = GetSettingByType([]string{"basic", "basic2"})
asserts.Equal(map[string]string{
"siteName": "Cloudreve",
"siteDes": "Something wonderful",
}, settings)
//找不到
rows = sqlmock.NewRows([]string{"name", "value", "type"})
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings = GetSettingByType([]string{"basic233"})
asserts.Equal(map[string]string{}, settings)
}
func TestGetSettingByNames(t *testing.T) {
asserts := assert.New(t)
//找到设置时
rows := sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName", "Cloudreve", "basic").
AddRow("siteDes", "Something wonderful", "basic")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings := GetSettingByNames([]string{"siteName", "siteDes"})
asserts.Equal(map[string]string{
"siteName": "Cloudreve",
"siteDes": "Something wonderful",
}, settings)
asserts.NoError(mock.ExpectationsWereMet())
//找到其中一个设置时
rows = sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName", "Cloudreve", "basic")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings = GetSettingByNames([]string{"siteName", "siteDes2333"})
asserts.Equal(map[string]string{
"siteName": "Cloudreve",
}, settings)
asserts.NoError(mock.ExpectationsWereMet())
//找不到设置时
rows = sqlmock.NewRows([]string{"name", "value", "type"})
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
settings = GetSettingByNames([]string{"siteName2333", "siteDes2333"})
asserts.Equal(map[string]string{}, settings)
asserts.NoError(mock.ExpectationsWereMet())
}
// TestGetSettingByName 测试GetSettingByName
func TestGetSettingByName(t *testing.T) {
asserts := assert.New(t)
//找到设置时
rows := sqlmock.NewRows([]string{"name", "value", "type"}).
AddRow("siteName", "Cloudreve", "basic")
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
siteName := GetSettingByName("siteName")
asserts.Equal("Cloudreve", siteName)
asserts.NoError(mock.ExpectationsWereMet())
// 第二次查询应返回缓存内容
siteNameCache := GetSettingByName("siteName")
asserts.Equal("Cloudreve", siteNameCache)
asserts.NoError(mock.ExpectationsWereMet())
// 找不到设置
rows = sqlmock.NewRows([]string{"name", "value", "type"})
mock.ExpectQuery("^SELECT \\* FROM `(.+)` WHERE `(.+)`\\.`deleted_at` IS NULL AND(.+)$").WillReturnRows(rows)
siteName = GetSettingByName("siteName not exist")
asserts.Equal("", siteName)
asserts.NoError(mock.ExpectationsWereMet())
}

@ -11,14 +11,6 @@ import (
func TestGetUser(t *testing.T) {
asserts := assert.New(t)
// 准备数据库 Mock
db, mock, err := sqlmock.New()
if err != nil {
t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
}
DB, _ = gorm.Open("mysql", db)
defer db.Close()
//找到用户时
rows := sqlmock.NewRows([]string{"id", "deleted_at", "email"}).
AddRow(1, nil, "admin@cloudreve.org")

@ -74,7 +74,7 @@ func (ll *Logger) Debug(format string, v ...interface{}) {
ll.Println(msg)
}
// GORM 的 Logger实现
// Print GORM 的 Logger实现
func (ll *Logger) Print(v ...interface{}) {
if LevelDebug > ll.level {
return

@ -3,6 +3,7 @@ package service
import (
"cloudreve/models"
"cloudreve/pkg/serializer"
"fmt"
"github.com/gin-gonic/gin"
)
@ -16,14 +17,12 @@ type UserLoginService struct {
// Login 用户登录函数
func (service *UserLoginService) Login(c *gin.Context) serializer.Response {
siteName, err := model.GetSettingByName("siteName")
if err == nil {
return serializer.Response{
Code: 0,
Msg: siteName,
}
} else {
return serializer.Err(5001, "无法获取参数值", err)
siteName := model.GetSettingByName("siteName")
basic := model.GetSettingByNames([]string{"siteDes", "siteKeywords"})
fmt.Println(basic)
return serializer.Response{
Code: 0,
Msg: siteName,
}
}

Loading…
Cancel
Save