完成一些测试

This commit is contained in:
eson 2023-06-15 17:51:06 +08:00
parent dfc411b9c6
commit 51a33052d9
11 changed files with 290 additions and 71 deletions

16
.vscode/launch.json vendored
View File

@ -1,16 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}/server/home-user-auth",
"args": ["-test.run", "^TestMain$"]
}
]
}

View File

@ -9,14 +9,14 @@ import (
// FsGetTypeCanteenType GetType返回前端的结构
type FsGetTypeCanteenType struct {
Id int64 `db:"id" json:"key"` // ID
Name string `db:"name" json:"name"` // 餐厅名字
Id int64 `gorm:"id" json:"key"` // ID
Name string `gorm:"name" json:"name"` // 餐厅名字
}
// TODO: 使用model的属性做你想做的
func (c *FsCanteenTypeModel) FindGetType(ctx context.Context, id int64) (resp []*FsGetTypeCanteenType, err error) {
err = c.db.WithContext(ctx).Model(&FsGetTypeCanteenType{}).Select("id,name").Order("sort desc").Where("`id` = ? and `status` = ? ", id, 1).Find(&resp).Error
func (c *FsCanteenTypeModel) FindAllGetType(ctx context.Context, id int64) (resp []*FsGetTypeCanteenType, err error) {
err = c.db.WithContext(ctx).Model(&FsCanteenType{}).Select("id,name").Order("sort desc").Where("`status` = ?", 1).Find(&resp).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return resp, err
}

View File

@ -6,17 +6,49 @@ import (
// TODO: 使用model的属性做你想做的
func (u *FsUserModel) FindUserByEmail(ctx context.Context, emailname string) (resp *FsUser, err error) {
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`email` = ?", emailname).First(resp).Error
type UserBasicInfoForSave struct {
ID uint `gorm:"primary_key" json:"id"`
FirstName string `gorm:"" json:"first_name"`
LastName string `gorm:"" json:"last_name"`
Mobile string `gorm:"" json:"mobile"`
Company string `gorm:"" json:"company"`
IsOrderStatusEmail int64 `gorm:"" json:"is_order_status_email"`
IsEmailAdvertisement int64 `gorm:"" json:"is_email_advertisement"`
IsOrderStatusPhone int64 `gorm:"" json:"is_order_status_phone"`
IsPhoneAdvertisement int64 `gorm:"" json:"is_phone_advertisement"`
Type int64 `gorm:"" json:"type"`
IsOpenRender int64 `gorm:"" json:"is_open_render"`
IsLowRendering int64 `gorm:"" json:"is_low_rendering"`
IsRemoveBg int64 `gorm:"" json:"is_remove_bg"`
}
func (u *FsUserModel) FindUserByEmail(ctx context.Context, emailname string) (resp FsUser, err error) {
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`email` = ?", emailname).First(&resp).Error
return resp, err
}
func (u *FsUserModel) FindUserById(ctx context.Context, Id int64) (resp *FsUser, err error) {
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`id` = ? and is_del = ?", Id, 0).First(resp).Error
func (u *FsUserModel) FindUserById(ctx context.Context, Id int64) (resp FsUser, err error) {
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`id` = ? and is_del = ?", Id, 0).First(&resp).Error
return resp, err
}
func (u *FsUserModel) UpdateUserBasicInfoById(ctx context.Context, Id int64, user *FsUser) (resp *FsUser, err error) {
err = u.db.WithContext(ctx).Model(&FsUser{}).Where("`id` = ? and is_del = ? and status = ?", user.Id, 0, 1).Updates(user).Error
return resp, err
func (u *FsUserModel) UpdateUserBasicInfoById(ctx context.Context, Id int64, user *UserBasicInfoForSave) (err error) {
err = u.db.WithContext(ctx).Model(&FsUser{}).
Where("`id` = ? and is_del = ? and status = ?", Id, 0, 1).
Updates(map[string]interface{}{
"first_name": user.FirstName,
"last_name": user.LastName,
"mobile": user.Mobile,
"company": user.Company,
"is_order_status_email": user.IsOrderStatusEmail,
"is_email_advertisement": user.IsEmailAdvertisement,
"is_order_status_phone": user.IsOrderStatusPhone,
"is_phone_advertisement": user.IsPhoneAdvertisement,
"type": user.Type,
"is_open_render": user.IsOpenRender,
"is_low_rendering": user.IsLowRendering,
"is_remove_bg": user.IsRemoveBg,
}).Error
return err
}

View File

@ -33,10 +33,10 @@ func (l *UserGetTypeLogic) UserGetType(req *types.Request, userinfo *auth.UserIn
}
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
data, err := gmodel.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindGetType(l.ctx, userinfo.UserId)
data, err := gmodel.NewFsCanteenTypeModel(l.svcCtx.MysqlConn).FindAllGetType(l.ctx, userinfo.UserId)
if err != nil {
logx.Error(err)
return
}
return resp.SetStatus(basic.CodeOK, "success", data)
return resp.SetStatus(basic.CodeOK, data)
}

View File

@ -33,9 +33,29 @@ func (l *UserSaveBasicInfoLogic) UserSaveBasicInfo(req *types.RequestBasicInfoFo
}
m := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
user, err := m.FindUserById(l.ctx, userinfo.UserId)
if err == gmodel.ErrRecordNotFound {
return resp.SetStatus(basic.CodeUserIdNotFoundErr)
err := m.UpdateUserBasicInfoById(l.ctx, userinfo.UserId, &gmodel.UserBasicInfoForSave{
FirstName: req.FirstName,
LastName: req.LastName,
Mobile: req.Mobile,
Company: req.Company,
IsOrderStatusEmail: req.IsOrderStatusEmail,
IsEmailAdvertisement: req.IsEmailAdvertisement,
IsOrderStatusPhone: req.IsOrderStatusPhone,
IsPhoneAdvertisement: req.IsPhoneAdvertisement,
Type: req.Type,
IsOpenRender: req.IsOpenRender,
IsLowRendering: req.IsLowRendering,
IsRemoveBg: req.IsRemoveBg,
})
if err != nil {
logx.Error(err)
switch err {
case gmodel.ErrRecordNotFound:
return resp.SetStatus(basic.CodeUserIdNotFoundErr)
default:
return resp.SetStatusWithMessage(basic.CodeUpdateErr, err.Error())
}
}
return resp.SetStatus(basic.CodeOK)

View File

@ -14,6 +14,7 @@ type RequestBasicInfoForm struct {
Company string `form:"company,optional" db:"company"` // 公司名称
Mobile string `form:"mobile,optional" db:"mobile"` // 手机号码
Email string `form:"email" db:"email"` // 邮箱
Type int64 `form:"type,optional" db:"type"` // 1正常 0不正常
IsOrderStatusEmail int64 `form:"is_order_status_email,optional" db:"is_order_status_email"` // 订单状态改变时是否接收邮件
IsEmailAdvertisement int64 `form:"is_email_advertisement,optional" db:"is_email_advertisement"` // 是否接收邮件广告
IsOrderStatusPhone int64 `form:"is_order_status_phone,optional" db:"is_order_status_phone"` // 订单状态改变是是否接收电话
@ -37,15 +38,15 @@ type DataGuest struct {
}
type DataUserBasicInfo struct {
Type int64 `db:"type"` // 1普通餐厅 2连锁餐厅
IsOrderStatusEmail bool `db:"is_order_status_email"` // 订单状态改变时是否接收邮件
IsEmailAdvertisement bool `db:"is_email_advertisement"` // 是否接收邮件广告
IsOrderStatusPhone bool `db:"is_order_status_phone"` // 订单状态改变是是否接收电话
IsPhoneAdvertisement bool `db:"is_phone_advertisement"` // 是否接收短信广告
IsOpenRender bool `db:"is_open_render"` // 是否打开个性化渲染1开启0关闭
IsThousandFace bool `db:"is_thousand_face"` // 是否已经存在千人千面1存在0不存在
IsLowRendering bool `db:"is_low_rendering"` // 是否开启低渲染模型渲染
IsRemoveBg bool `db:"is_remove_bg"` // 用户上传logo是否去除背景
Type int64 `json:"type"` // 1普通餐厅 2连锁餐厅
IsOrderStatusEmail bool `json:"is_order_status_email"` // 订单状态改变时是否接收邮件
IsEmailAdvertisement bool `json:"is_email_advertisement"` // 是否接收邮件广告
IsOrderStatusPhone bool `json:"is_order_status_phone"` // 订单状态改变是是否接收电话
IsPhoneAdvertisement bool `json:"is_phone_advertisement"` // 是否接收短信广告
IsOpenRender bool `json:"is_open_render"` // 是否打开个性化渲染1开启0关闭
IsThousandFace bool `json:"is_thousand_face"` // 是否已经存在千人千面1存在0不存在
IsLowRendering bool `json:"is_low_rendering"` // 是否开启低渲染模型渲染
IsRemoveBg bool `json:"is_remove_bg"` // 用户上传logo是否去除背景
}
type DataGetType struct {

View File

@ -1,7 +1,6 @@
package logic_test
import (
"flag"
"fmt"
"fusenapi/server/home-user-auth/internal/config"
"fusenapi/server/home-user-auth/internal/handler"
@ -13,7 +12,7 @@ import (
"github.com/zeromicro/go-zero/rest"
)
var testConfigFile = flag.String("f", "../etc/home-user-auth.yaml", "the config file")
var testConfigFile = "../etc/home-user-auth.yaml"
var cnf config.Config
var gserver *rest.Server
@ -22,9 +21,8 @@ func init() {
}
func GetTestServer() *rest.Server {
flag.Parse()
conf.MustLoad(*testConfigFile, &cnf)
conf.MustLoad(testConfigFile, &cnf)
server := rest.MustNewServer(cnf.RestConf)
defer server.Stop()

View File

@ -1 +1,129 @@
package logic_test
import (
"fmt"
"testing"
"github.com/474420502/requests"
"github.com/tidwall/gjson"
)
func TestCaseBasicInfoLogic(t *testing.T) {
var err error
var resp *requests.Response
var result gjson.Result
// 获取 session并携带 JWT token
ses := GetSesssionWithJwtToken(t, gserver)
// 向服务器发送 GET 请求,获取用户基本信息
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/basic-info", cnf.Host, cnf.Port)).TestInServer(gserver)
if err != nil {
t.Error(err)
}
// 检查返回值中的 code 字段是否存在,并且值是否为 200
result = resp.Json().Get("code")
if !result.Exists() {
t.Error("code is not exists")
}
if result.Int() != 200 {
t.Error("code != 200")
}
// 检查返回值中的 msg 字段是否存在,并且值是否为 "success"
result = resp.Json().Get("msg")
if !result.Exists() {
t.Error("msg is not exists")
}
if result.String() != "success" {
t.Error(result.String())
}
// 检查返回值中的 data 字段是否存在
result = resp.Json().Get("data")
if !result.Exists() {
t.Error("data is not exists")
}
// 检查返回值中的 type 字段是否存在,并且值是否为 0
result = resp.Json().Get("data.type")
if !result.Exists() {
t.Error("type is not exists")
}
if result.Int() != 0 {
t.Error("type != 0")
}
// 检查返回值中的 is_order_status_email 字段是否存在,并且值是否为 false
result = resp.Json().Get("data.is_order_status_email")
if !result.Exists() {
t.Error("is_order_status_email is not exists")
}
if result.Bool() != false {
t.Error("is_order_status_email != false")
}
// 检查返回值中的 is_email_advertisement 字段是否存在,并且值是否为 false
result = resp.Json().Get("data.is_email_advertisement")
if !result.Exists() {
t.Error("is_email_advertisement is not exists")
}
if result.Bool() != false {
t.Error("is_email_advertisement != false")
}
// 检查返回值中的 is_order_status_phone 字段是否存在,并且值是否为 false
result = resp.Json().Get("data.is_order_status_phone")
if !result.Exists() {
t.Error("is_order_status_phone is not exists")
}
if result.Bool() != false {
t.Error("is_order_status_phone != false")
}
// 检查返回值中的 is_phone_advertisement 字段是否存在,并且值是否为 false
result = resp.Json().Get("data.is_phone_advertisement")
if !result.Exists() {
t.Error("is_phone_advertisement is not exists")
}
if result.Bool() != false {
t.Error("is_phone_advertisement != false")
}
// 检查返回值中的 is_open_render 字段是否存在,并且值是否为 false
result = resp.Json().Get("data.is_open_render")
if !result.Exists() {
t.Error("is_open_render is not exists")
}
if result.Bool() != false {
t.Error("is_open_render != false")
}
// 检查返回值中的 is_thousand_face 字段是否存在,并且值是否为 false
result = resp.Json().Get("data.is_thousand_face")
if !result.Exists() {
t.Error("is_thousand_face is not exists")
}
if result.Bool() != false {
t.Error("is_thousand_face != false")
}
// 检查返回值中的 is_low_rendering 字段是否存在,并且值是否为 false
result = resp.Json().Get("data.is_low_rendering")
if !result.Exists() {
t.Error("is_low_rendering is not exists")
}
if result.Bool() != false {
t.Error("is_low_rendering != false")
}
// 检查返回值中的 is_remove_bg 字段是否存在,并且值是否为 true
result = resp.Json().Get("data.is_remove_bg")
if !result.Exists() {
t.Error("is_remove_bg is not exists")
}
if result.Bool() != true {
t.Error("is_remove_bg != true")
}
}

View File

@ -0,0 +1,59 @@
package logic_test
import (
"fmt"
"testing"
"github.com/474420502/requests"
"github.com/tidwall/gjson"
)
func TestCaseGetTypeLogic(t *testing.T) {
var err error
var resp *requests.Response
var result gjson.Result
// 获取 session并携带 JWT token
ses := GetSesssionWithJwtToken(t, gserver)
// 向服务器发送 GET 请求,获取用户类型信息
resp, err = ses.Get(fmt.Sprintf("http://%s:%d/user/get-type", cnf.Host, cnf.Port)).TestInServer(gserver)
if err != nil {
t.Error(err)
}
// 使用 gjson 解析返回的 json 数据
result = gjson.Parse(resp.ContentString())
// 检查返回值中的 code 字段是否存在,并且值是否为 200
code := result.Get("code").Int()
if code != 200 {
t.Errorf("Invalid code value: %d", code)
}
// 检查返回值中的 msg 字段是否存在,并且值是否为 "success"
msg := result.Get("msg").String()
if msg != "success" {
t.Errorf(`Invalid msg value: "%s"`, msg)
}
// 检查返回值中的 data 字段是否存在,并且值是否为数组类型
dataArray := result.Get("data").Array()
if len(dataArray) == 0 {
t.Error("Empty data field")
}
// 遍历每个元素,检查其 key 和 name 字段是否存在,并且值是否符合预期
for i, item := range dataArray {
key := item.Get("key").Int()
name := item.Get("name").String()
if key != int64(i+1) {
t.Errorf("Unexpected key value at index %d: %d", i, key)
}
if len(name) == 0 {
t.Errorf("Missing name value at index %d", i)
}
}
}

View File

@ -41,19 +41,19 @@ service home-user-auth {
}
type RequestBasicInfoForm {
FirstName string `form:"first_name,optional" db:"first_name"` // FirstName
LastName string `form:"last_name,optional" db:"last_name"` // LastName
Company string `form:"company,optional" db:"company"` // 公司名称
Mobile string `form:"mobile,optional" db:"mobile"` // 手机号码
Email string `form:"email" db:"email"` // 邮箱
// Status int64 `form:"status,optional" db:"status"` // 1正常 0不正常
IsOrderStatusEmail int64 `form:"is_order_status_email,optional" db:"is_order_status_email"` // 订单状态改变时是否接收邮件
IsEmailAdvertisement int64 `form:"is_email_advertisement,optional" db:"is_email_advertisement"` // 是否接收邮件广告
IsOrderStatusPhone int64 `form:"is_order_status_phone,optional" db:"is_order_status_phone"` // 订单状态改变是是否接收电话
IsPhoneAdvertisement int64 `form:"is_phone_advertisement,optional" db:"is_phone_advertisement"` // 是否接收短信广告
IsOpenRender int64 `form:"is_open_render,optional" db:"is_open_render"` // 是否打开个性化渲染1开启0关闭
IsLowRendering int64 `form:"is_low_rendering,optional" db:"is_low_rendering"` // 是否开启低渲染模型渲染
IsRemoveBg int64 `form:"is_remove_bg,optional" db:"is_remove_bg"` // 用户上传logo是否去除背景
FirstName string `form:"first_name,optional" db:"first_name"` // FirstName
LastName string `form:"last_name,optional" db:"last_name"` // LastName
Company string `form:"company,optional" db:"company"` // 公司名称
Mobile string `form:"mobile,optional" db:"mobile"` // 手机号码
Email string `form:"email" db:"email"` // 邮箱
Type int64 `form:"type,optional" db:"type"` // 1正常 0不正常
IsOrderStatusEmail int64 `form:"is_order_status_email,optional" db:"is_order_status_email"` // 订单状态改变时是否接收邮件
IsEmailAdvertisement int64 `form:"is_email_advertisement,optional" db:"is_email_advertisement"` // 是否接收邮件广告
IsOrderStatusPhone int64 `form:"is_order_status_phone,optional" db:"is_order_status_phone"` // 订单状态改变是是否接收电话
IsPhoneAdvertisement int64 `form:"is_phone_advertisement,optional" db:"is_phone_advertisement"` // 是否接收短信广告
IsOpenRender int64 `form:"is_open_render,optional" db:"is_open_render"` // 是否打开个性化渲染1开启0关闭
IsLowRendering int64 `form:"is_low_rendering,optional" db:"is_low_rendering"` // 是否开启低渲染模型渲染
IsRemoveBg int64 `form:"is_remove_bg,optional" db:"is_remove_bg"` // 用户上传logo是否去除背景
// NewPassword string `form:"new_password,optional" db:"new_password"` // new_password 如果存在新密码
}
@ -76,20 +76,17 @@ type DataGuest {
// UserBasicInfoHandler 返回data结构
type DataUserBasicInfo {
Type int64 `db:"type"` // 1普通餐厅 2连锁餐厅
IsOrderStatusEmail bool `db:"is_order_status_email"` // 订单状态改变时是否接收邮件
IsEmailAdvertisement bool `db:"is_email_advertisement"` // 是否接收邮件广告
IsOrderStatusPhone bool `db:"is_order_status_phone"` // 订单状态改变是是否接收电话
IsPhoneAdvertisement bool `db:"is_phone_advertisement"` // 是否接收短信广告
IsOpenRender bool `db:"is_open_render"` // 是否打开个性化渲染1开启0关闭
IsThousandFace bool `db:"is_thousand_face"` // 是否已经存在千人千面1存在0不存在
IsLowRendering bool `db:"is_low_rendering"` // 是否开启低渲染模型渲染
IsRemoveBg bool `db:"is_remove_bg"` // 用户上传logo是否去除背景
Type int64 `json:"type"` // 1普通餐厅 2连锁餐厅
IsOrderStatusEmail bool `json:"is_order_status_email"` // 订单状态改变时是否接收邮件
IsEmailAdvertisement bool `json:"is_email_advertisement"` // 是否接收邮件广告
IsOrderStatusPhone bool `json:"is_order_status_phone"` // 订单状态改变是是否接收电话
IsPhoneAdvertisement bool `json:"is_phone_advertisement"` // 是否接收短信广告
IsOpenRender bool `json:"is_open_render"` // 是否打开个性化渲染1开启0关闭
IsThousandFace bool `json:"is_thousand_face"` // 是否已经存在千人千面1存在0不存在
IsLowRendering bool `json:"is_low_rendering"` // 是否开启低渲染模型渲染
IsRemoveBg bool `json:"is_remove_bg"` // 用户上传logo是否去除背景
}
// /user/get-type reponse.data 结构
type DataGetType {
Id int64 `db:"id" json:"key"` // ID

View File

@ -14,7 +14,7 @@ var (
CodeUnAuth = &StatusResponse{401, "unauthorized"} // 未授权
CodeEmailNotFoundErr = &StatusResponse{5050, "the email was not found"} // email 不存在
CodeUserIdNotFoundErr = &StatusResponse{5051, "the user was not found"} // email 不存在
CodeUserIdNotFoundErr = &StatusResponse{5051, "the user was not found"} // userid 不存在
CodePasswordErr = &StatusResponse{5052, "invalid password"} // 密码错误
CodeGuestDupErr = &StatusResponse{5010, "the user is already a guest user and does not need to apply again"} // 用户已经是guest用户不需要重复申请 错误