Testing for controller/main.go

pull/247/head
HFO4 5 years ago
parent 6ba4d1ae82
commit 2c9e0c1119

@ -4,5 +4,11 @@ go 1.12
require (
github.com/gin-gonic/gin v1.4.0
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/jinzhu/gorm v1.9.11
github.com/leodido/go-urn v1.2.0 // indirect
github.com/stretchr/testify v1.4.0
gopkg.in/go-playground/validator.v8 v8.18.2
gopkg.in/go-playground/validator.v9 v9.30.0
)

@ -0,0 +1,34 @@
package model
import (
"Cloudreve/pkg/util"
"github.com/jinzhu/gorm"
"time"
//
_ "github.com/jinzhu/gorm/dialects/mysql"
)
// DB 数据库链接单例
var DB *gorm.DB
// Database 在中间件中初始化mysql链接
func Database(connString string) {
db, err := gorm.Open("mysql", connString)
db.LogMode(true)
// Error
if err != nil {
util.Log().Panic("连接数据库不成功", err)
}
//设置连接池
//空闲
db.DB().SetMaxIdleConns(50)
//打开
db.DB().SetMaxOpenConns(100)
//超时
db.DB().SetConnMaxLifetime(time.Second * 30)
DB = db
migration()
}

@ -0,0 +1,8 @@
package model
//执行数据迁移
func migration() {
// 自动迁移模式
DB.AutoMigrate()
}

@ -0,0 +1,106 @@
package util
import (
"fmt"
"os"
"time"
)
const (
// LevelError 错误
LevelError = iota
// LevelWarning 警告
LevelWarning
// LevelInformational 提示
LevelInformational
// LevelDebug 除错
LevelDebug
)
var logger *Logger
// Logger 日志
type Logger struct {
level int
}
// Println 打印
func (ll *Logger) Println(msg string) {
fmt.Printf("%s %s", time.Now().Format("2006-01-02 15:04:05 -0700"), msg)
}
// Panic 极端错误
func (ll *Logger) Panic(format string, v ...interface{}) {
if LevelError > ll.level {
return
}
msg := fmt.Sprintf("[Panic] "+format, v...)
ll.Println(msg)
os.Exit(0)
}
// Error 错误
func (ll *Logger) Error(format string, v ...interface{}) {
if LevelError > ll.level {
return
}
msg := fmt.Sprintf("[E] "+format, v...)
ll.Println(msg)
}
// Warning 警告
func (ll *Logger) Warning(format string, v ...interface{}) {
if LevelWarning > ll.level {
return
}
msg := fmt.Sprintf("[W] "+format, v...)
ll.Println(msg)
}
// Info 信息
func (ll *Logger) Info(format string, v ...interface{}) {
if LevelInformational > ll.level {
return
}
msg := fmt.Sprintf("[I] "+format, v...)
ll.Println(msg)
}
// Debug 校验
func (ll *Logger) Debug(format string, v ...interface{}) {
if LevelDebug > ll.level {
return
}
msg := fmt.Sprintf("[D] "+format, v...)
ll.Println(msg)
}
// BuildLogger 构建logger
func BuildLogger(level string) {
intLevel := LevelError
switch level {
case "error":
intLevel = LevelError
case "warning":
intLevel = LevelWarning
case "info":
intLevel = LevelInformational
case "debug":
intLevel = LevelDebug
}
l := Logger{
level: intLevel,
}
logger = &l
}
// Log 返回日志对象
func Log() *Logger {
if logger == nil {
l := Logger{
level: LevelDebug,
}
logger = &l
}
return logger
}

@ -1,7 +1,7 @@
package controllers
import (
"Cloudreve/serializer"
"Cloudreve/pkg/serializer"
"encoding/json"
"gopkg.in/go-playground/validator.v8"
)

@ -0,0 +1,83 @@
package controllers
import (
"Cloudreve/pkg/serializer"
"encoding/json"
"errors"
"github.com/stretchr/testify/assert"
"gopkg.in/go-playground/validator.v8"
"testing"
)
func TestErrorResponse(t *testing.T) {
asserts := assert.New(t)
type Test struct {
UserName string `validate:"required,min=10,max=20"`
}
testCase1 := Test{}
validate := validator.New(&validator.Config{TagName: "validate"})
errs := validate.Struct(testCase1)
res := ErrorResponse(errs)
asserts.Equal(serializer.ParamErr(ParamErrorMsg("UserName", "required"), errs), res)
testCase2 := Test{
UserName: "a",
}
validate2 := validator.New(&validator.Config{TagName: "validate"})
errs = validate2.Struct(testCase2)
res = ErrorResponse(errs)
asserts.Equal(serializer.ParamErr(ParamErrorMsg("UserName", "min"), errs), res)
type JsonTest struct {
UserName string `json:"UserName"`
}
testCase3 := JsonTest{}
errs = json.Unmarshal([]byte("{\"UserName\":1}"), &testCase3)
res = ErrorResponse(errs)
asserts.Equal(serializer.ParamErr("JSON类型不匹配", errs), res)
errs = errors.New("Unknown error")
res = ErrorResponse(errs)
asserts.Equal(serializer.ParamErr("参数错误", errs), res)
}
// 测试 ParamErrorMs
func TestParamErrorMsg(t *testing.T) {
asserts := assert.New(t)
testCase := []struct {
field string
tag string
expect string
}{
{
"UserName",
"required",
"用户名不能为空",
},
{
"Password",
"min",
"密码太短",
},
{
"Password",
"max",
"密码太长",
},
{
"something unexpected",
"max",
"",
},
{
"",
"",
"",
},
}
for _, value := range testCase {
asserts.Equal(value.expect, ParamErrorMsg(value.field, value.tag), "case %v", value)
}
}

@ -1,7 +1,7 @@
package controllers
import (
"Cloudreve/serializer"
"Cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
)

@ -1,7 +1,7 @@
package service
import (
"Cloudreve/serializer"
"Cloudreve/pkg/serializer"
"github.com/gin-gonic/gin"
)

Loading…
Cancel
Save