Repository: zeromicro/go-zero
Pull request #3636 (2023-10-17 07:25:19)

fix: goctl unit test


Patch:
diff --git a/tools/goctl/pkg/env/env.go b/tools/goctl/pkg/env/env.go
index a6177840..8fbfd770 100644
--- a/tools/goctl/pkg/env/env.go
+++ b/tools/goctl/pkg/env/env.go
@@ -7,6 +7,7 @@ import (
 	"path/filepath"
 	"runtime"
 	"strings"
+	"testing"
 
 	"github.com/zeromicro/go-zero/tools/goctl/internal/version"
 	sortedmap "github.com/zeromicro/go-zero/tools/goctl/pkg/collection"
@@ -111,6 +112,14 @@ func Get(key string) string {
 	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 {
 	return goctlEnv.GetStringOr(key, def)
 }


Test patch:
diff --git a/tools/goctl/api/gogen/gen_test.go b/tools/goctl/api/gogen/gen_test.go
index 2aab5c68..d2cb6842 100644
--- a/tools/goctl/api/gogen/gen_test.go
+++ b/tools/goctl/api/gogen/gen_test.go
@@ -10,6 +10,7 @@ import (
 
 	"github.com/stretchr/testify/assert"
 	"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/util/pathx"
 )
@@ -53,7 +54,10 @@ func TestParser(t *testing.T) {
 	filename := "greet.api"
 	err := os.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
 	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)
 	assert.Nil(t, err)
diff --git a/tools/goctl/api/parser/g4/gen/api/file_splitor_test.go b/tools/goctl/api/parser/g4/gen/api/file_splitor_test.go
index 77ac0b1d..8353c078 100644
--- a/tools/goctl/api/parser/g4/gen/api/file_splitor_test.go
+++ b/tools/goctl/api/parser/g4/gen/api/file_splitor_test.go
@@ -16,6 +16,7 @@ import (
 )
 
 func TestFileSplitor(t *testing.T) {
+	t.Skip("skip this test because it is used to split the apiparser_parser.go file by developer.")
 	dir := "."
 	data, err := os.ReadFile(filepath.Join(dir, "apiparser_parser.go"))
 	if err != nil {
diff --git a/tools/goctl/model/sql/gen/keys_test.go b/tools/goctl/model/sql/gen/keys_test.go
index 145f136e..c54cc4fe 100644
--- a/tools/goctl/model/sql/gen/keys_test.go
+++ b/tools/goctl/model/sql/gen/keys_test.go
@@ -76,9 +76,9 @@ func TestGenCacheKeys(t *testing.T) {
 				VarExpression:     `cacheGoZeroUserIdPrefix = "cache:goZero:user:id:"`,
 				KeyLeft:           "goZeroUserIdKey",
 				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)`,
-				DataKeyExpression: `goZeroUserIdKey := fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.ID)`,
+				DataKeyExpression: `goZeroUserIdKey := fmt.Sprintf("%s%v", cacheGoZeroUserIdPrefix, data.Id)`,
 				FieldNameJoin:     []string{"id"},
 			})
 		}())
@@ -170,9 +170,9 @@ func TestGenCacheKeys(t *testing.T) {
 				VarExpression:     `cacheUserIdPrefix = "cache:user:id:"`,
 				KeyLeft:           "userIdKey",
 				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)`,
-				DataKeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.ID)`,
+				DataKeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
 				FieldNameJoin:     []string{"id"},
 			})
 		}())
diff --git a/tools/goctl/model/sql/test/model/model_test.go b/tools/goctl/model/sql/test/model/model_test.go
index 08bf121c..d42680ed 100644
--- a/tools/goctl/model/sql/test/model/model_test.go
+++ b/tools/goctl/model/sql/test/model/model_test.go
@@ -43,7 +43,7 @@ func TestStudentModel(t *testing.T) {
 		Valid: true,
 	}
 
-	err := mockStudent(func(mock sqlmock.Sqlmock) {
+	err := mockStudent(t, func(mock sqlmock.Sqlmock) {
 		mock.ExpectExec(fmt.Sprintf("insert into %s", testTable)).
 			WithArgs(data.Class, data.Name, data.Age, data.Score).
 			WillReturnResult(sqlmock.NewResult(testInsertId, testRowsAffected))
@@ -61,7 +61,7 @@ func TestStudentModel(t *testing.T) {
 	})
 	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)).
 			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))
@@ -79,7 +79,7 @@ func TestStudentModel(t *testing.T) {
 	})
 	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))
 	}, func(m StudentModel, redis *redis.Redis) {
 		data.Name = testUpdateName
@@ -93,7 +93,7 @@ func TestStudentModel(t *testing.T) {
 	assert.Nil(t, err)
 
 	data.Name = testUpdateName
-	err = mockStudent(func(mock sqlmock.Sqlmock) {
+	err = mockStudent(t, func(mock sqlmock.Sqlmock) {
 		mock.ExpectQuery(fmt.Sprintf("select (.+) from %s ", testTable)).
 			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))
@@ -111,7 +111,7 @@ func TestStudentModel(t *testing.T) {
 	})
 	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)).
 			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))
@@ -126,7 +126,7 @@ func TestStudentModel(t *testing.T) {
 	})
 	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))
 	}, func(m StudentModel, redis *redis.Redis) {
 		err = m.Delete(testInsertId, class, testUpdateName)
@@ -228,7 +228,7 @@ func TestUserModel(t *testing.T) {
 }
 
 // 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()
 	if err != nil {
 		return err
@@ -241,13 +241,7 @@ func mockStudent(mockFn func(mock sqlmock.Sqlmock), fn func(m StudentModel, r *r
 	mock.ExpectCommit()
 
 	conn := mocksql.NewMockConn(db)
-	r, clean, err := redistest.CreateRedis()
-	if err != nil {
-		return err
-	}
-
-	defer clean()
-
+	r := redistest.CreateRedis(t)
 	m := NewStudentModel(conn, cache.CacheConf{
 		{
 			RedisConf: redis.RedisConf{
diff --git a/tools/goctl/model/sql/test/orm.go b/tools/goctl/model/sql/test/orm.go
index 4f84f6f2..00b061d5 100644
--- a/tools/goctl/model/sql/test/orm.go
+++ b/tools/goctl/model/sql/test/orm.go
@@ -128,7 +128,7 @@ func unmarshalRow(v any, scanner rowsScanner, strict bool) error {
 	}
 
 	rv := reflect.ValueOf(v)
-	if err := mapping.ValidatePtr(&rv); err != nil {
+	if err := mapping.ValidatePtr(rv); err != nil {
 		return err
 	}
 
@@ -163,7 +163,7 @@ func unmarshalRow(v any, scanner rowsScanner, strict bool) error {
 
 func unmarshalRows(v any, scanner rowsScanner, strict bool) error {
 	rv := reflect.ValueOf(v)
-	if err := mapping.ValidatePtr(&rv); err != nil {
+	if err := mapping.ValidatePtr(rv); err != nil {
 		return err
 	}