add gen service unit testing (#3142)

This commit is contained in:
oldme 2023-11-09 20:04:05 +08:00 committed by GitHub
parent 0bd15bdab5
commit d4b14fd717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 154 additions and 3 deletions

View File

@ -33,7 +33,6 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
}
)
err := gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
@ -45,7 +44,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
panic(err)
}
// apiInterface files
// apiInterface file
var (
genApi = apiFolder + filepath.FromSlash("/article/article.go")
genApiExpect = apiFolder + filepath.FromSlash("/article/article_expect.go")
@ -79,7 +78,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
testPath + filepath.FromSlash("/article/article_v2_create.go"),
testPath + filepath.FromSlash("/article/article_v2_update.go"),
}
for i, _ := range files {
for i := range files {
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
}
})

View File

@ -0,0 +1,72 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package cmd
import (
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/genservice"
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/guid"
"github.com/gogf/gf/v2/util/gutil"
"path/filepath"
"testing"
)
func Test_Gen_Service_Default(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
path = gfile.Temp(guid.S())
dstFolder = path + filepath.FromSlash("/service")
apiFolder = gtest.DataPath("genservice", "logic")
in = genservice.CGenServiceInput{
SrcFolder: apiFolder,
DstFolder: dstFolder,
DstFileNameCase: "Snake",
WatchFile: "",
StPattern: "",
Packages: nil,
ImportPrefix: "",
Clear: false,
}
)
err := gutil.FillStructWithDefault(&in)
t.AssertNil(err)
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)
_, err = genservice.CGenService{}.Service(ctx, in)
if err != nil {
panic(err)
}
// logic file
var (
genApi = apiFolder + filepath.FromSlash("/logic.go")
genApiExpect = apiFolder + filepath.FromSlash("/logic_expect.go")
)
defer gfile.Remove(genApi)
t.Assert(gfile.GetContents(genApi), gfile.GetContents(genApiExpect))
// files
files, err := gfile.ScanDir(dstFolder, "*.go", true)
t.AssertNil(err)
t.Assert(files, []string{
dstFolder + filepath.FromSlash("/article.go"),
})
// contents
testPath := gtest.DataPath("genservice", "service")
expectFiles := []string{
testPath + filepath.FromSlash("/article.go"),
}
for i := range files {
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
}
})
}

View File

@ -0,0 +1,33 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package article
import (
"context"
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/service"
)
type sArticle struct {
}
func init() {
service.RegisterArticle(&sArticle{})
}
// Get article details
func (s *sArticle) Get(ctx context.Context, id uint) (info struct{}, err error) {
return struct{}{}, err
}
// Create
/**
* create an article.
* @author oldme
*/
func (s *sArticle) Create(ctx context.Context, info struct{}) (id uint, err error) {
return id, err
}

View File

@ -0,0 +1,9 @@
// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================
package logic
import (
_ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/logic/article"
)

View File

@ -0,0 +1,38 @@
// ================================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// You can delete these comments if you wish manually maintain this interface file.
// ================================================================================
package service
import (
"context"
)
type (
IArticle interface {
// Get article details
Get(ctx context.Context, id uint) (info struct{}, err error)
// Create
/**
* create an article.
* @author oldme
*/
Create(ctx context.Context, info struct{}) (id uint, err error)
}
)
var (
localArticle IArticle
)
func Article() IArticle {
if localArticle == nil {
panic("implement not found for interface IArticle, forgot register?")
}
return localArticle
}
func RegisterArticle(i IArticle) {
localArticle = i
}