最新可以跑单元测试
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)
|
||||
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()
|
||||
}
|
||||
|
||||
func GetTestServer() *rest.Server {
|
||||
flag.Parse()
|
||||
// 测试代码 可以直接复制新建
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
// var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
|
||||
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
defer server.Stop()
|
||||
// func GetTestServer() *rest.Server {
|
||||
// flag.Parse()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
// var c config.Config
|
||||
// conf.MustLoad(*testConfigFile, &c)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
return server
|
||||
}
|
||||
// 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
|
||||
// }
|
||||
|
|
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)
|
||||
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)
|
||||
if loginInfo.UserId == 0 {
|
||||
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)
|
||||
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)
|
||||
if loginInfo.UserId == 0 {
|
||||
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)
|
||||
|
||||
data := &types.DataStatusConfig{
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
|
@ -125,4 +125,43 @@ type Auth struct {
|
|||
AccessExpire int64 `json:"accessExpire"`
|
||||
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/handler"
|
||||
"fusenapi/server/home-user-auth/internal/svc"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"github.com/474420502/requests"
|
||||
|
@ -14,30 +13,29 @@ import (
|
|||
"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 {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
conf.MustLoad(*testConfigFile, &cnf)
|
||||
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
server := rest.MustNewServer(cnf.RestConf)
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
ctx := svc.NewServiceContext(cnf)
|
||||
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
|
||||
}
|
||||
|
||||
func TestCaseAddressList(t *testing.T) {
|
||||
server := GetTestServer()
|
||||
// http.NewRequest("POST", "http://localhost:8888/user/login", body io.Reader)
|
||||
|
||||
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{}{
|
||||
"name": "devenv@sina.cn",
|
||||
"pwd": "$2y$13$6UFDMZQMEfqFYiNLpiUCi.B3fpvGEamPAjIgzUqv/u7jT05nB3pOC",
|
||||
|
@ -48,13 +46,28 @@ func TestCaseAddressList(t *testing.T) {
|
|||
}
|
||||
result := resp.Json()
|
||||
token := result.Get("data.token")
|
||||
ses.Header.Add("Authorization", token.String())
|
||||
log.Println(resp.ContentString())
|
||||
|
||||
resp, err = ses.Get("http://localhost:8888/user/address-list").TestInServer(server)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
if !token.Exists() {
|
||||
t.Error("data.token is not exists")
|
||||
}
|
||||
log.Println(string(resp.Content()))
|
||||
ses.Header.Add("Authorization", token.String())
|
||||
|
||||
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/address-list", cnf.Host, cnf.Port)).TestInServer(server)
|
||||
if err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
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: ""
|
||||
email: ""
|
||||
)
|
||||
type request {
|
||||
|
||||
}
|
||||
|
||||
// response 统一返回码
|
||||
type response {
|
||||
Code int `json:"code"`
|
||||
|
|
|
@ -42,7 +42,8 @@ func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *R
|
|||
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatus(sr *StatusResponse, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
Code: sr.Code,
|
||||
Message: sr.Message,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
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…
Reference in New Issue
Block a user