mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-11 03:33:53 +08:00
发布v2.15.1版本,更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md
This commit is contained in:
67
server/internal/library/hgorm/hook/tenant_test.go
Normal file
67
server/internal/library/hgorm/hook/tenant_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
// Package hook
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package hook_test
|
||||
|
||||
import (
|
||||
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/hgorm/hook"
|
||||
"hotgo/internal/model"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func init() {
|
||||
g.DB().SetDryRun(true)
|
||||
g.DB().SetDebug(true)
|
||||
}
|
||||
|
||||
// TestSaveTenant_User 以用户身份增改购买订单数据
|
||||
func TestSaveTenant_User(t *testing.T) {
|
||||
|
||||
// 设置上下文用户身份为用户
|
||||
ctx := context.WithValue(gctx.New(), consts.ContextHTTPKey, &model.Context{
|
||||
// 为了测试只设置了hook中需要用到的数据
|
||||
User: &model.Identity{
|
||||
Id: 12,
|
||||
DeptType: consts.DeptTypeUser,
|
||||
},
|
||||
})
|
||||
|
||||
cols := dao.AddonHgexampleTenantOrder.Columns()
|
||||
orderSn := grand.Letters(32)
|
||||
|
||||
// 以用户身份插入购买订单数据,自动维护租户关系字段
|
||||
data := g.Map{
|
||||
//cols.TenantId: 8,
|
||||
//cols.MerchantId: 11,
|
||||
//cols.UserId: 12,
|
||||
cols.ProductName: "无线运动耳机",
|
||||
cols.OrderSn: orderSn,
|
||||
cols.Money: 99,
|
||||
cols.Status: consts.PayStatusWait,
|
||||
}
|
||||
|
||||
_, err := dao.AddonHgexampleTenantOrder.Ctx(ctx).Data(data).Hook(hook.SaveTenant).Insert()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 以用户身份插入修改订单数据,自动维护租户关系字段
|
||||
update := g.Map{
|
||||
cols.Status: consts.PayStatusOk,
|
||||
}
|
||||
|
||||
_, err = dao.AddonHgexampleTenantOrder.Ctx(ctx).Where(cols.OrderSn, orderSn).Data(update).Hook(hook.SaveTenant).Update()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user