最新可以跑单元测试
This commit is contained in:
parent
e0b0e2aff8
commit
fc806b71c6
@ -24,3 +24,23 @@ func main() {
|
|||||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||||
server.Start()
|
server.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// var testConfigFile = flag.String("f", "../etc/{{.serviceName}}.yaml", "the config file")
|
||||||
|
|
||||||
|
// func GetTestServer() *rest.Server {
|
||||||
|
// flag.Parse()
|
||||||
|
|
||||||
|
// var c config.Config
|
||||||
|
// conf.MustLoad(*testConfigFile, &c)
|
||||||
|
|
||||||
|
// server := rest.MustNewServer(c.RestConf)
|
||||||
|
// defer server.Stop()
|
||||||
|
|
||||||
|
// ctx := svc.NewServiceContext(c)
|
||||||
|
// handler.RegisterHandlers(server, ctx)
|
||||||
|
|
||||||
|
// fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||||
|
// return server
|
||||||
|
// }
|
@ -30,18 +30,22 @@ func main() {
|
|||||||
server.Start()
|
server.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTestServer() *rest.Server {
|
// 测试代码 可以直接复制新建
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
var c config.Config
|
// var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
|
||||||
conf.MustLoad(*configFile, &c)
|
|
||||||
|
|
||||||
server := rest.MustNewServer(c.RestConf)
|
// func GetTestServer() *rest.Server {
|
||||||
defer server.Stop()
|
// flag.Parse()
|
||||||
|
|
||||||
ctx := svc.NewServiceContext(c)
|
// var c config.Config
|
||||||
handler.RegisterHandlers(server, ctx)
|
// conf.MustLoad(*testConfigFile, &c)
|
||||||
|
|
||||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
// server := rest.MustNewServer(c.RestConf)
|
||||||
return server
|
// defer server.Stop()
|
||||||
}
|
|
||||||
|
// ctx := svc.NewServiceContext(c)
|
||||||
|
// handler.RegisterHandlers(server, ctx)
|
||||||
|
|
||||||
|
// fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||||
|
// return server
|
||||||
|
// }
|
||||||
|
52
server/home-user-auth/internal/handler/routes.go
Normal file
52
server/home-user-auth/internal/handler/routes.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Code generated by goctl. DO NOT EDIT.
|
||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"fusenapi/server/home-user-auth/internal/svc"
|
||||||
|
|
||||||
|
"github.com/zeromicro/go-zero/rest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||||
|
server.AddRoutes(
|
||||||
|
[]rest.Route{
|
||||||
|
{
|
||||||
|
Method: http.MethodPost,
|
||||||
|
Path: "/user/login",
|
||||||
|
Handler: UserLoginHandler(serverCtx),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/user/fonts",
|
||||||
|
Handler: UserFontsHandler(serverCtx),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/user/get-type",
|
||||||
|
Handler: UserGetTypeHandler(serverCtx),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodPost,
|
||||||
|
Path: "/user/basic-info",
|
||||||
|
Handler: UserSaveBasicInfoHandler(serverCtx),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodPost,
|
||||||
|
Path: "/user/status-config",
|
||||||
|
Handler: UserStatusConfigHandler(serverCtx),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/user/basic-info",
|
||||||
|
Handler: UserBasicInfoHandler(serverCtx),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Method: http.MethodGet,
|
||||||
|
Path: "/user/address-list",
|
||||||
|
Handler: UserAddressListHandler(serverCtx),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
@ -26,7 +26,7 @@ func NewUserAddressListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *U
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *UserAddressListLogic) UserAddressList(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
|
func (l *UserAddressListLogic) UserAddressList(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||||
m := model.NewFsAddressModel(l.svcCtx.MysqlConn)
|
m := model.NewFsAddressModel(l.svcCtx.MysqlConn)
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ func NewUserBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Use
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
|
func (l *UserBasicInfoLogic) UserBasicInfo(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||||
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
|
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
|
||||||
if loginInfo.UserId == 0 {
|
if loginInfo.UserId == 0 {
|
||||||
return resp.SetStatus(basic.CodeOK, "parse login info err ")
|
return resp.SetStatus(basic.CodeOK, "parse login info err ")
|
||||||
|
@ -26,7 +26,7 @@ func NewUserGetTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserG
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *UserGetTypeLogic) UserGetType(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
|
func (l *UserGetTypeLogic) UserGetType(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||||
|
|
||||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||||
data, err := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindGetType(l.ctx)
|
data, err := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindGetType(l.ctx)
|
||||||
|
@ -26,7 +26,7 @@ func NewUserSaveBasicInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoForm, userinfo *auth.UserInfo) (resp *types.Response) {
|
func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoForm, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||||
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
|
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
|
||||||
if loginInfo.UserId == 0 {
|
if loginInfo.UserId == 0 {
|
||||||
return resp.SetStatus(basic.CodeOK, "parse login info err ")
|
return resp.SetStatus(basic.CodeOK, "parse login info err ")
|
||||||
|
@ -25,7 +25,7 @@ func NewUserStatusConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *UserStatusConfigLogic) UserStatusConfig(req *types.Request, userinfo *auth.UserInfo) (resp *types.Response) {
|
func (l *UserStatusConfigLogic) UserStatusConfig(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||||
|
|
||||||
data := &types.DataStatusConfig{
|
data := &types.DataStatusConfig{
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fusenapi/utils/basic"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Request struct {
|
type Request struct {
|
||||||
@ -126,3 +126,42 @@ type Auth struct {
|
|||||||
RefreshAfter int64 `json:"refreshAfter"`
|
RefreshAfter int64 `json:"refreshAfter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set 设置Response的Code和Message值
|
||||||
|
func (resp *Response) Set(Code int, Message string) *Response {
|
||||||
|
return &Response{
|
||||||
|
Code: Code,
|
||||||
|
Message: Message,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set 设置整个Response
|
||||||
|
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
|
||||||
|
return &Response{
|
||||||
|
Code: Code,
|
||||||
|
Message: Message,
|
||||||
|
Data: Data,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
|
||||||
|
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
|
||||||
|
newResp := &Response{
|
||||||
|
Code: sr.Code,
|
||||||
|
}
|
||||||
|
if len(data) == 1 {
|
||||||
|
newResp.Data = data[0]
|
||||||
|
}
|
||||||
|
return newResp
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
|
||||||
|
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
|
||||||
|
newResp := &Response{
|
||||||
|
Code: sr.Code,
|
||||||
|
Message: msg,
|
||||||
|
}
|
||||||
|
if len(data) == 1 {
|
||||||
|
newResp.Data = data[0]
|
||||||
|
}
|
||||||
|
return newResp
|
||||||
|
}
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"fusenapi/server/home-user-auth/internal/config"
|
"fusenapi/server/home-user-auth/internal/config"
|
||||||
"fusenapi/server/home-user-auth/internal/handler"
|
"fusenapi/server/home-user-auth/internal/handler"
|
||||||
"fusenapi/server/home-user-auth/internal/svc"
|
"fusenapi/server/home-user-auth/internal/svc"
|
||||||
"log"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/474420502/requests"
|
"github.com/474420502/requests"
|
||||||
@ -14,30 +13,29 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/rest"
|
"github.com/zeromicro/go-zero/rest"
|
||||||
)
|
)
|
||||||
|
|
||||||
var configFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
|
var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
|
||||||
|
var cnf config.Config
|
||||||
|
|
||||||
func GetTestServer() *rest.Server {
|
func GetTestServer() *rest.Server {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
var c config.Config
|
conf.MustLoad(*testConfigFile, &cnf)
|
||||||
conf.MustLoad(*configFile, &c)
|
|
||||||
|
|
||||||
server := rest.MustNewServer(c.RestConf)
|
server := rest.MustNewServer(cnf.RestConf)
|
||||||
defer server.Stop()
|
defer server.Stop()
|
||||||
|
|
||||||
ctx := svc.NewServiceContext(c)
|
ctx := svc.NewServiceContext(cnf)
|
||||||
handler.RegisterHandlers(server, ctx)
|
handler.RegisterHandlers(server, ctx)
|
||||||
|
|
||||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
fmt.Printf("Starting server at %s:%d...\n", cnf.Host, cnf.Port)
|
||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCaseAddressList(t *testing.T) {
|
func TestCaseAddressList(t *testing.T) {
|
||||||
server := GetTestServer()
|
server := GetTestServer()
|
||||||
// http.NewRequest("POST", "http://localhost:8888/user/login", body io.Reader)
|
|
||||||
|
|
||||||
ses := requests.NewSession()
|
ses := requests.NewSession()
|
||||||
tp := ses.Post("http://localhost:8888/user/login")
|
tp := ses.Post(fmt.Sprintf("http://%s:%d/user/login", cnf.Host, cnf.Port))
|
||||||
tp.SetBodyJson(map[string]interface{}{
|
tp.SetBodyJson(map[string]interface{}{
|
||||||
"name": "devenv@sina.cn",
|
"name": "devenv@sina.cn",
|
||||||
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
|
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
|
||||||
@ -48,13 +46,28 @@ func TestCaseAddressList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
result := resp.Json()
|
result := resp.Json()
|
||||||
token := result.Get("data.token")
|
token := result.Get("data.token")
|
||||||
|
if !token.Exists() {
|
||||||
|
t.Error("data.token is not exists")
|
||||||
|
}
|
||||||
ses.Header.Add("Authorization", token.String())
|
ses.Header.Add("Authorization", token.String())
|
||||||
log.Println(resp.ContentString())
|
|
||||||
|
|
||||||
resp, err = ses.Get("http://localhost:8888/user/address-list").TestInServer(server)
|
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestInServer(server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
t.Error(err)
|
||||||
|
}
|
||||||
|
result = resp.Json().Get("code")
|
||||||
|
if !result.Exists() {
|
||||||
|
t.Error("code is not exists")
|
||||||
|
}
|
||||||
|
if result.Int() != 200 {
|
||||||
|
t.Error("code != 200")
|
||||||
}
|
}
|
||||||
log.Println(string(resp.Content()))
|
|
||||||
|
|
||||||
|
result = resp.Json().Get("msg")
|
||||||
|
if !result.Exists() {
|
||||||
|
t.Error("msg is not exists")
|
||||||
|
}
|
||||||
|
if result.String() != "success" {
|
||||||
|
t.Error(result.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,8 @@ info (
|
|||||||
author: ""
|
author: ""
|
||||||
email: ""
|
email: ""
|
||||||
)
|
)
|
||||||
type request {
|
|
||||||
|
|
||||||
}
|
|
||||||
// response 统一返回码
|
// response 统一返回码
|
||||||
type response {
|
type response {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
|
@ -43,6 +43,7 @@ func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *R
|
|||||||
func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Response {
|
func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Response {
|
||||||
newResp := &Response{
|
newResp := &Response{
|
||||||
Code: sr.Code,
|
Code: sr.Code,
|
||||||
|
Message: sr.Message,
|
||||||
}
|
}
|
||||||
if len(data) == 1 {
|
if len(data) == 1 {
|
||||||
newResp.Data = data[0]
|
newResp.Data = data[0]
|
||||||
|
@ -1,49 +0,0 @@
|
|||||||
package sqlfs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var dbTag = "db"
|
|
||||||
|
|
||||||
// RawFieldNames converts golang struct field into slice string.
|
|
||||||
func RawFieldNames[T any]() []string {
|
|
||||||
|
|
||||||
var out []string
|
|
||||||
|
|
||||||
var a T
|
|
||||||
v := reflect.ValueOf(a)
|
|
||||||
typ := v.Type()
|
|
||||||
|
|
||||||
for i := 0; i < v.NumField(); i++ {
|
|
||||||
// gets us a StructField
|
|
||||||
fi := typ.Field(i)
|
|
||||||
tagv := fi.Tag.Get(dbTag)
|
|
||||||
switch tagv {
|
|
||||||
case "-":
|
|
||||||
continue
|
|
||||||
default:
|
|
||||||
// get tag name with the tag opton, e.g.:
|
|
||||||
// `db:"id"`
|
|
||||||
// `db:"id,type=char,length=16"`
|
|
||||||
// `db:",type=char,length=16"`
|
|
||||||
// `db:"-,type=char,length=16"`
|
|
||||||
if strings.Contains(tagv, ",") {
|
|
||||||
tagv = strings.TrimSpace(strings.Split(tagv, ",")[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
if tagv == "-" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(tagv) == 0 {
|
|
||||||
tagv = fi.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
out = append(out, tagv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return out
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user