Files
req/resx/resource_test.go
what 3c757d53c5 重构: User 默认实现移到 req/userx,消除 req 对 contracts 的循环依赖
GetAnonymous/GetSystemUser 原来在 contracts/base 里实现,req 自己的测试
又要用它们构造测试用户,形成 req(测试)依赖 contracts、contracts(正式
代码)依赖 req 的循环——两边 go.mod 只能靠指向本地兄弟目录的 replace 绕过,
没法各自独立解析成真实版本号。

跟 resx 之于 req.Resource 一个思路:接口留在 req 包,默认实现挪到子包
req/userx,req 主包保持不引入实现细节所需的依赖(这里是 samber/lo)。
req 自己的测试文件改用 req/userx,不用触碰 contracts 了;contracts/base
里的 GetAnonymous/GetSystemUser 改成指向 req/userx 的函数别名,保持
兼容不用改调用方。
2026-08-20 18:00:40 +08:00

89 lines
2.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package resx_test
import (
"testing"
"git.fsdpf.net/go/db"
"git.fsdpf.net/go/db/engine"
"git.fsdpf.net/go/req"
"git.fsdpf.net/go/req/resx"
"git.fsdpf.net/go/req/userx"
"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(userx.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(userx.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(userx.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() {}