From 2c9e0c111941388bcf64bafa65daab1bad3ff6f5 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Wed, 6 Nov 2019 22:35:31 +0800 Subject: [PATCH] Testing for controller/main.go --- go.mod | 6 ++ models/init.go | 34 ++++++++ models/migration.go | 8 ++ {serializer => pkg/serializer}/common.go | 0 pkg/util/logger.go | 106 +++++++++++++++++++++++ routers/controllers/main.go | 2 +- routers/controllers/main_test.go | 83 ++++++++++++++++++ routers/controllers/ping.go | 2 +- service/user/user_login.go | 2 +- 9 files changed, 240 insertions(+), 3 deletions(-) create mode 100644 models/init.go create mode 100644 models/migration.go rename {serializer => pkg/serializer}/common.go (100%) create mode 100644 pkg/util/logger.go create mode 100644 routers/controllers/main_test.go diff --git a/go.mod b/go.mod index 993e688..91e02c9 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/models/init.go b/models/init.go new file mode 100644 index 0000000..dbc6ba4 --- /dev/null +++ b/models/init.go @@ -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() +} diff --git a/models/migration.go b/models/migration.go new file mode 100644 index 0000000..c706a24 --- /dev/null +++ b/models/migration.go @@ -0,0 +1,8 @@ +package model + +//执行数据迁移 + +func migration() { + // 自动迁移模式 + DB.AutoMigrate() +} diff --git a/serializer/common.go b/pkg/serializer/common.go similarity index 100% rename from serializer/common.go rename to pkg/serializer/common.go diff --git a/pkg/util/logger.go b/pkg/util/logger.go new file mode 100644 index 0000000..b8f1222 --- /dev/null +++ b/pkg/util/logger.go @@ -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 +} diff --git a/routers/controllers/main.go b/routers/controllers/main.go index 24b0cfe..7890b87 100644 --- a/routers/controllers/main.go +++ b/routers/controllers/main.go @@ -1,7 +1,7 @@ package controllers import ( - "Cloudreve/serializer" + "Cloudreve/pkg/serializer" "encoding/json" "gopkg.in/go-playground/validator.v8" ) diff --git a/routers/controllers/main_test.go b/routers/controllers/main_test.go new file mode 100644 index 0000000..47eac22 --- /dev/null +++ b/routers/controllers/main_test.go @@ -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) + } +} diff --git a/routers/controllers/ping.go b/routers/controllers/ping.go index f1fdbbb..6f4b27c 100644 --- a/routers/controllers/ping.go +++ b/routers/controllers/ping.go @@ -1,7 +1,7 @@ package controllers import ( - "Cloudreve/serializer" + "Cloudreve/pkg/serializer" "github.com/gin-gonic/gin" ) diff --git a/service/user/user_login.go b/service/user/user_login.go index 4ac2556..8c9930e 100644 --- a/service/user/user_login.go +++ b/service/user/user_login.go @@ -1,7 +1,7 @@ package service import ( - "Cloudreve/serializer" + "Cloudreve/pkg/serializer" "github.com/gin-gonic/gin" )