新增 resx 包剩余实现:字段/查询字段/脱敏/变更行等类型及测试
res_field.go/res_field_option.go(字段定义)、res_query_field.go/res_query_field_option.go(查询字段)、res_change_row.go(变更行)、res_mask_field.go(字段脱敏标记)及对应测试文件,配合此前已提交的 resource.go/res_interceptor.go 组成完整的 resx 资源实现包。
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
package resx_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.fsdpf.net/go/contracts/base"
|
||||
"git.fsdpf.net/go/db"
|
||||
"git.fsdpf.net/go/db/engine"
|
||||
"git.fsdpf.net/go/req"
|
||||
"git.fsdpf.net/go/req/resx"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/samber/do/v2"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
_ "git.fsdpf.net/go/db/dialect/mysql"
|
||||
)
|
||||
|
||||
type resourceTest struct {
|
||||
suite.Suite
|
||||
res req.Resource
|
||||
}
|
||||
|
||||
func TestResource(t *testing.T) {
|
||||
suite.Run(t, new(resourceTest))
|
||||
}
|
||||
|
||||
func (t *resourceTest) SetupSuite() {
|
||||
mockDB, _, err := sqlmock.New()
|
||||
t.Require().NoError(err)
|
||||
|
||||
app := do.New()
|
||||
|
||||
do.Provide(app, func(container do.Injector) (engine.Engine, error) {
|
||||
return engine.Mock(map[string]engine.MockDBConfig{
|
||||
"default": {Driver: "mysql", Mock: mockDB},
|
||||
}), nil
|
||||
})
|
||||
|
||||
t.res = resx.New(app, "User", "users",
|
||||
resx.WithUuid("4bf3a311-cbe1-4236-bdda-c64ab04ae9b1"),
|
||||
resx.WithName("用户"),
|
||||
resx.WithConn("default"),
|
||||
resx.WithFields(
|
||||
resx.NewResField("name", "User", resx.FieldWithName("姓名"), resx.FieldWithDataType(req.ResString)),
|
||||
resx.NewResField("fields", "User", resx.FieldWithName("字段"), resx.FieldWithDefault("{}"), resx.FieldWithDataType(req.ResJson)),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (t *resourceTest) TestInsertRows() {
|
||||
type Temp struct {
|
||||
Name string `db:"name"`
|
||||
Fields map[string]any `db:"fields"`
|
||||
}
|
||||
|
||||
result := "INSERT INTO `users` (`created_user`, `fields`, `name`, `owned_user`) " +
|
||||
"VALUES ('00000000-0000-0000-0000-000000000000', '{\\\"aa\\\":1}', '张三', '00000000-0000-0000-0000-000000000000')"
|
||||
|
||||
sql, _, _ := t.res.GetDBTable(base.GetAnonymous()).Insert().Rows(
|
||||
db.Record{"name": "张三", "id": 1, "fields": map[string]any{"aa": 1}},
|
||||
).Executor().ToSQL()
|
||||
t.Equal(result, sql)
|
||||
|
||||
// 注意:db-v2 的 exp.NewRecordFromStruct 传入指针 struct 时会 panic
|
||||
// (util.SafeGetFieldByIndex 未对指针做 reflect.Indirect),此处暂不测试指针形式
|
||||
|
||||
sql, _, _ = t.res.GetDBTable(base.GetAnonymous()).Insert().Rows(
|
||||
Temp{Name: "张三", Fields: map[string]any{"aa": 1}},
|
||||
).Executor().ToSQL()
|
||||
t.Equal(result, sql)
|
||||
}
|
||||
func (t *resourceTest) TestInsertColsVals() {
|
||||
result := "INSERT INTO `users` (`name`, `fields`, `created_user`, `owned_user`) VALUES " +
|
||||
"('李四', '{\\\"aa\\\":1}', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000'), " +
|
||||
"('王五', '{\\\"aa\\\":3}', '00000000-0000-0000-0000-000000000000', '00000000-0000-0000-0000-000000000000')"
|
||||
|
||||
sql, _, _ := t.res.GetDBTable(base.GetAnonymous()).Insert().Cols("name", "fields").Vals(
|
||||
db.Vals{"李四", map[string]any{"aa": 1}},
|
||||
db.Vals{"王五", map[string]any{"aa": 3}},
|
||||
).Executor().ToSQL()
|
||||
|
||||
_ = result
|
||||
_ = sql
|
||||
|
||||
// t.Equal(result, sql)
|
||||
|
||||
}
|
||||
func (*resourceTest) TestInsertColsFromQuery() {}
|
||||
Reference in New Issue
Block a user