mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
fix: goctl unit test (#3636)
This commit is contained in:
parent
87b7a1120d
commit
d84dfe1b20
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
"github.com/zeromicro/go-zero/tools/goctl/api/parser"
|
||||||
|
"github.com/zeromicro/go-zero/tools/goctl/pkg/env"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
|
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||||
)
|
)
|
||||||
@ -53,7 +54,10 @@ func TestParser(t *testing.T) {
|
|||||||
filename := "greet.api"
|
filename := "greet.api"
|
||||||
err := os.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
|
err := os.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
defer os.Remove(filename)
|
env.Set(t, env.GoctlExperimental, "off")
|
||||||
|
t.Cleanup(func() {
|
||||||
|
os.Remove(filename)
|
||||||
|
})
|
||||||
|
|
||||||
api, err := parser.Parse(filename)
|
api, err := parser.Parse(filename)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFileSplitor(t *testing.T) {
|
func TestFileSplitor(t *testing.T) {
|
||||||
|
t.Skip("skip this test because it is used to split the apiparser_parser.go file by developer.")
|
||||||
dir := "."
|
dir := "."
|
||||||
data, err := os.ReadFile(filepath.Join(dir, "apiparser_parser.go"))
|
data, err := os.ReadFile(filepath.Join(dir, "apiparser_parser.go"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -76,9 +76,9 @@ func TestGenCacheKeys(t *testing.T) {
|
|||||||
VarExpression: `cacheGoZeroUserIdPrefix = "cache:goZero:user:id:"`,
|
VarExpression: `cacheGoZeroUserIdPrefix = "cache:goZero:user:id:"`,
|
||||||
KeyLeft: "goZeroUserIdKey",
|
KeyLeft: "goZeroUserIdKey",
|
||||||
KeyRight: `fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, id)`,
|
KeyRight: `fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, id)`,
|
||||||
DataKeyRight: `fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.ID)`,
|
DataKeyRight: `fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.Id)`,
|
||||||
KeyExpression: `goZeroUserIdKey := fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, id)`,
|
KeyExpression: `goZeroUserIdKey := fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, id)`,
|
||||||
DataKeyExpression: `goZeroUserIdKey := fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.ID)`,
|
DataKeyExpression: `goZeroUserIdKey := fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.Id)`,
|
||||||
FieldNameJoin: []string{"id"},
|
FieldNameJoin: []string{"id"},
|
||||||
})
|
})
|
||||||
}())
|
}())
|
||||||
@ -170,9 +170,9 @@ func TestGenCacheKeys(t *testing.T) {
|
|||||||
VarExpression: `cacheUserIdPrefix = "cache:user:id:"`,
|
VarExpression: `cacheUserIdPrefix = "cache:user:id:"`,
|
||||||
KeyLeft: "userIdKey",
|
KeyLeft: "userIdKey",
|
||||||
KeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
|
KeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
|
||||||
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, data.ID)`,
|
DataKeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
|
||||||
KeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
|
KeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
|
||||||
DataKeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.ID)`,
|
DataKeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
|
||||||
FieldNameJoin: []string{"id"},
|
FieldNameJoin: []string{"id"},
|
||||||
})
|
})
|
||||||
}())
|
}())
|
||||||
|
@ -43,7 +43,7 @@ func TestStudentModel(t *testing.T) {
|
|||||||
Valid: true,
|
Valid: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
err := mockStudent(func(mock sqlmock.Sqlmock) {
|
err := mockStudent(t, func(mock sqlmock.Sqlmock) {
|
||||||
mock.ExpectExec(fmt.Sprintf("insert into %s", testTable)).
|
mock.ExpectExec(fmt.Sprintf("insert into %s", testTable)).
|
||||||
WithArgs(data.Class, data.Name, data.Age, data.Score).
|
WithArgs(data.Class, data.Name, data.Age, data.Score).
|
||||||
WillReturnResult(sqlmock.NewResult(testInsertId, testRowsAffected))
|
WillReturnResult(sqlmock.NewResult(testInsertId, testRowsAffected))
|
||||||
@ -61,7 +61,7 @@ func TestStudentModel(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = mockStudent(func(mock sqlmock.Sqlmock) {
|
err = mockStudent(t, func(mock sqlmock.Sqlmock) {
|
||||||
mock.ExpectQuery(fmt.Sprintf("select (.+) from %s", testTable)).
|
mock.ExpectQuery(fmt.Sprintf("select (.+) from %s", testTable)).
|
||||||
WithArgs(testInsertId).
|
WithArgs(testInsertId).
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "class", "name", "age", "score", "create_time", "update_time"}).AddRow(testInsertId, data.Class, data.Name, data.Age, data.Score, testTimeValue, testTimeValue))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "class", "name", "age", "score", "create_time", "update_time"}).AddRow(testInsertId, data.Class, data.Name, data.Age, data.Score, testTimeValue, testTimeValue))
|
||||||
@ -79,7 +79,7 @@ func TestStudentModel(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = mockStudent(func(mock sqlmock.Sqlmock) {
|
err = mockStudent(t, func(mock sqlmock.Sqlmock) {
|
||||||
mock.ExpectExec(fmt.Sprintf("update %s", testTable)).WithArgs(data.Class, testUpdateName, data.Age, data.Score, testInsertId).WillReturnResult(sqlmock.NewResult(testInsertId, testRowsAffected))
|
mock.ExpectExec(fmt.Sprintf("update %s", testTable)).WithArgs(data.Class, testUpdateName, data.Age, data.Score, testInsertId).WillReturnResult(sqlmock.NewResult(testInsertId, testRowsAffected))
|
||||||
}, func(m StudentModel, redis *redis.Redis) {
|
}, func(m StudentModel, redis *redis.Redis) {
|
||||||
data.Name = testUpdateName
|
data.Name = testUpdateName
|
||||||
@ -93,7 +93,7 @@ func TestStudentModel(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
data.Name = testUpdateName
|
data.Name = testUpdateName
|
||||||
err = mockStudent(func(mock sqlmock.Sqlmock) {
|
err = mockStudent(t, func(mock sqlmock.Sqlmock) {
|
||||||
mock.ExpectQuery(fmt.Sprintf("select (.+) from %s ", testTable)).
|
mock.ExpectQuery(fmt.Sprintf("select (.+) from %s ", testTable)).
|
||||||
WithArgs(testInsertId).
|
WithArgs(testInsertId).
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "class", "name", "age", "score", "create_time", "update_time"}).AddRow(testInsertId, data.Class, data.Name, data.Age, data.Score, testTimeValue, testTimeValue))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "class", "name", "age", "score", "create_time", "update_time"}).AddRow(testInsertId, data.Class, data.Name, data.Age, data.Score, testTimeValue, testTimeValue))
|
||||||
@ -111,7 +111,7 @@ func TestStudentModel(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = mockStudent(func(mock sqlmock.Sqlmock) {
|
err = mockStudent(t, func(mock sqlmock.Sqlmock) {
|
||||||
mock.ExpectQuery(fmt.Sprintf("select (.+) from %s ", testTable)).
|
mock.ExpectQuery(fmt.Sprintf("select (.+) from %s ", testTable)).
|
||||||
WithArgs(class, testUpdateName).
|
WithArgs(class, testUpdateName).
|
||||||
WillReturnRows(sqlmock.NewRows([]string{"id", "class", "name", "age", "score", "create_time", "update_time"}).AddRow(testInsertId, data.Class, data.Name, data.Age, data.Score, testTimeValue, testTimeValue))
|
WillReturnRows(sqlmock.NewRows([]string{"id", "class", "name", "age", "score", "create_time", "update_time"}).AddRow(testInsertId, data.Class, data.Name, data.Age, data.Score, testTimeValue, testTimeValue))
|
||||||
@ -126,7 +126,7 @@ func TestStudentModel(t *testing.T) {
|
|||||||
})
|
})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
err = mockStudent(func(mock sqlmock.Sqlmock) {
|
err = mockStudent(t, func(mock sqlmock.Sqlmock) {
|
||||||
mock.ExpectExec(fmt.Sprintf("delete from %s where `id` = ?", testTable)).WithArgs(testInsertId).WillReturnResult(sqlmock.NewResult(testInsertId, testRowsAffected))
|
mock.ExpectExec(fmt.Sprintf("delete from %s where `id` = ?", testTable)).WithArgs(testInsertId).WillReturnResult(sqlmock.NewResult(testInsertId, testRowsAffected))
|
||||||
}, func(m StudentModel, redis *redis.Redis) {
|
}, func(m StudentModel, redis *redis.Redis) {
|
||||||
err = m.Delete(testInsertId, class, testUpdateName)
|
err = m.Delete(testInsertId, class, testUpdateName)
|
||||||
@ -228,7 +228,7 @@ func TestUserModel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// with cache
|
// with cache
|
||||||
func mockStudent(mockFn func(mock sqlmock.Sqlmock), fn func(m StudentModel, r *redis.Redis)) error {
|
func mockStudent(t *testing.T, mockFn func(mock sqlmock.Sqlmock), fn func(m StudentModel, r *redis.Redis)) error {
|
||||||
db, mock, err := sqlmock.New()
|
db, mock, err := sqlmock.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -241,13 +241,7 @@ func mockStudent(mockFn func(mock sqlmock.Sqlmock), fn func(m StudentModel, r *r
|
|||||||
mock.ExpectCommit()
|
mock.ExpectCommit()
|
||||||
|
|
||||||
conn := mocksql.NewMockConn(db)
|
conn := mocksql.NewMockConn(db)
|
||||||
r, clean, err := redistest.CreateRedis()
|
r := redistest.CreateRedis(t)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer clean()
|
|
||||||
|
|
||||||
m := NewStudentModel(conn, cache.CacheConf{
|
m := NewStudentModel(conn, cache.CacheConf{
|
||||||
{
|
{
|
||||||
RedisConf: redis.RedisConf{
|
RedisConf: redis.RedisConf{
|
||||||
|
@ -128,7 +128,7 @@ func unmarshalRow(v any, scanner rowsScanner, strict bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rv := reflect.ValueOf(v)
|
rv := reflect.ValueOf(v)
|
||||||
if err := mapping.ValidatePtr(&rv); err != nil {
|
if err := mapping.ValidatePtr(rv); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,7 +163,7 @@ func unmarshalRow(v any, scanner rowsScanner, strict bool) error {
|
|||||||
|
|
||||||
func unmarshalRows(v any, scanner rowsScanner, strict bool) error {
|
func unmarshalRows(v any, scanner rowsScanner, strict bool) error {
|
||||||
rv := reflect.ValueOf(v)
|
rv := reflect.ValueOf(v)
|
||||||
if err := mapping.ValidatePtr(&rv); err != nil {
|
if err := mapping.ValidatePtr(rv); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
9
tools/goctl/pkg/env/env.go
vendored
9
tools/goctl/pkg/env/env.go
vendored
@ -7,6 +7,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
|
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
|
||||||
sortedmap "github.com/zeromicro/go-zero/tools/goctl/pkg/collection"
|
sortedmap "github.com/zeromicro/go-zero/tools/goctl/pkg/collection"
|
||||||
@ -111,6 +112,14 @@ func Get(key string) string {
|
|||||||
return GetOr(key, "")
|
return GetOr(key, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set sets the environment variable for testing
|
||||||
|
func Set(t *testing.T, key, value string) {
|
||||||
|
goctlEnv.SetKV(key, value)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
goctlEnv.Remove(key)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func GetOr(key, def string) string {
|
func GetOr(key, def string) string {
|
||||||
return goctlEnv.GetStringOr(key, def)
|
return goctlEnv.GetStringOr(key, def)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user