mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
emhace: unit testing cases update for package cmd/gf
and contrib/drivers
(#3453)
This commit is contained in:
parent
d7a0482146
commit
6e2d238f56
@ -41,9 +41,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
|
|||||||
defer gfile.Remove(path)
|
defer gfile.Remove(path)
|
||||||
|
|
||||||
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
|
_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
|
||||||
if err != nil {
|
t.AssertNil(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// apiInterface file
|
// apiInterface file
|
||||||
var (
|
var (
|
||||||
|
@ -8,18 +8,18 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gogf/gf/v2/os/gcmd"
|
"github.com/gogf/gf/cmd/gf/v2/internal/cmd/genpbentity"
|
||||||
"github.com/gogf/gf/v2/os/gfile"
|
"github.com/gogf/gf/v2/os/gfile"
|
||||||
"github.com/gogf/gf/v2/test/gtest"
|
"github.com/gogf/gf/v2/test/gtest"
|
||||||
"github.com/gogf/gf/v2/text/gstr"
|
"github.com/gogf/gf/v2/text/gstr"
|
||||||
"github.com/gogf/gf/v2/util/guid"
|
"github.com/gogf/gf/v2/util/guid"
|
||||||
|
"github.com/gogf/gf/v2/util/gutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_Gen_Pbentity_NameCase(t *testing.T) {
|
func Test_Gen_Pbentity_Default(t *testing.T) {
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
@ -38,31 +38,107 @@ func Test_Gen_Pbentity_NameCase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
defer dropTableWithDb(db, table)
|
defer dropTableWithDb(db, table)
|
||||||
var path = gfile.Temp(guid.S())
|
|
||||||
|
var (
|
||||||
|
path = gfile.Temp(guid.S())
|
||||||
|
in = genpbentity.CGenPbEntityInput{
|
||||||
|
Path: path,
|
||||||
|
Package: "unittest",
|
||||||
|
Link: link,
|
||||||
|
Tables: "",
|
||||||
|
Prefix: "",
|
||||||
|
RemovePrefix: "",
|
||||||
|
RemoveFieldPrefix: "",
|
||||||
|
NameCase: "",
|
||||||
|
JsonCase: "",
|
||||||
|
Option: "",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
err = gutil.FillStructWithDefault(&in)
|
||||||
|
t.AssertNil(err)
|
||||||
|
|
||||||
err = gfile.Mkdir(path)
|
err = gfile.Mkdir(path)
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
defer gfile.Remove(path)
|
defer gfile.Remove(path)
|
||||||
|
|
||||||
root, err := gcmd.NewFromObject(GF)
|
_, err = genpbentity.CGenPbEntity{}.PbEntity(ctx, in)
|
||||||
t.AssertNil(err)
|
|
||||||
err = root.AddObject(
|
|
||||||
Gen,
|
|
||||||
)
|
|
||||||
t.AssertNil(err)
|
|
||||||
os.Args = []string{"gf", "gen", "pbentity", "-l", link, "-p", path, "-package=unittest", "-nameCase=SnakeScreaming"}
|
|
||||||
|
|
||||||
err = root.RunWithError(ctx)
|
|
||||||
t.AssertNil(err)
|
t.AssertNil(err)
|
||||||
|
|
||||||
files := []string{
|
// files
|
||||||
filepath.FromSlash(path + "/table_user.proto"),
|
files, err := gfile.ScanDir(path, "*.proto", false)
|
||||||
}
|
t.AssertNil(err)
|
||||||
|
t.Assert(files, []string{
|
||||||
|
path + filepath.FromSlash("/table_user.proto"),
|
||||||
|
})
|
||||||
|
|
||||||
testPath := gtest.DataPath("genpbentity", "generated_user")
|
// contents
|
||||||
|
testPath := gtest.DataPath("genpbentity", "generated")
|
||||||
expectFiles := []string{
|
expectFiles := []string{
|
||||||
filepath.FromSlash(testPath + "/table_user.proto"),
|
testPath + filepath.FromSlash("/table_user.proto"),
|
||||||
|
}
|
||||||
|
for i := range files {
|
||||||
|
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Gen_Pbentity_NameCase_SnakeScreaming(t *testing.T) {
|
||||||
|
gtest.C(t, func(t *gtest.T) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
db = testDB
|
||||||
|
table = "table_user"
|
||||||
|
sqlContent = fmt.Sprintf(
|
||||||
|
gtest.DataContent(`genpbentity`, `user.tpl.sql`),
|
||||||
|
table,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
dropTableWithDb(db, table)
|
||||||
|
array := gstr.SplitAndTrim(sqlContent, ";")
|
||||||
|
for _, v := range array {
|
||||||
|
if _, err = db.Exec(ctx, v); err != nil {
|
||||||
|
t.AssertNil(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer dropTableWithDb(db, table)
|
||||||
|
|
||||||
|
var (
|
||||||
|
path = gfile.Temp(guid.S())
|
||||||
|
in = genpbentity.CGenPbEntityInput{
|
||||||
|
Path: path,
|
||||||
|
Package: "unittest",
|
||||||
|
Link: link,
|
||||||
|
Tables: "",
|
||||||
|
Prefix: "",
|
||||||
|
RemovePrefix: "",
|
||||||
|
RemoveFieldPrefix: "",
|
||||||
|
NameCase: "SnakeScreaming",
|
||||||
|
JsonCase: "",
|
||||||
|
Option: "",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
err = gutil.FillStructWithDefault(&in)
|
||||||
|
t.AssertNil(err)
|
||||||
|
|
||||||
|
err = gfile.Mkdir(path)
|
||||||
|
t.AssertNil(err)
|
||||||
|
defer gfile.Remove(path)
|
||||||
|
|
||||||
|
_, err = genpbentity.CGenPbEntity{}.PbEntity(ctx, in)
|
||||||
|
t.AssertNil(err)
|
||||||
|
|
||||||
|
// files
|
||||||
|
files, err := gfile.ScanDir(path, "*.proto", false)
|
||||||
|
t.AssertNil(err)
|
||||||
|
t.Assert(files, []string{
|
||||||
|
path + filepath.FromSlash("/table_user.proto"),
|
||||||
|
})
|
||||||
|
|
||||||
|
// contents
|
||||||
|
testPath := gtest.DataPath("genpbentity", "generated")
|
||||||
|
expectFiles := []string{
|
||||||
|
testPath + filepath.FromSlash("/table_user_snake_screaming.proto"),
|
||||||
}
|
}
|
||||||
// check files content
|
|
||||||
for i := range files {
|
for i := range files {
|
||||||
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
|
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
|
||||||
}
|
}
|
||||||
|
@ -42,9 +42,7 @@ func Test_Gen_Service_Default(t *testing.T) {
|
|||||||
defer gfile.Remove(path)
|
defer gfile.Remove(path)
|
||||||
|
|
||||||
_, err = genservice.CGenService{}.Service(ctx, in)
|
_, err = genservice.CGenService{}.Service(ctx, in)
|
||||||
if err != nil {
|
t.AssertNil(err)
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// logic file
|
// logic file
|
||||||
var (
|
var (
|
||||||
|
21
cmd/gf/internal/cmd/testdata/genpbentity/generated/table_user.proto
vendored
Normal file
21
cmd/gf/internal/cmd/testdata/genpbentity/generated/table_user.proto
vendored
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// ==========================================================================
|
||||||
|
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
|
||||||
|
// ==========================================================================
|
||||||
|
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package unittest;
|
||||||
|
|
||||||
|
option go_package = "unittest";
|
||||||
|
|
||||||
|
import "google/protobuf/timestamp.proto";
|
||||||
|
|
||||||
|
message TableUser {
|
||||||
|
uint32 Id = 1; // User ID
|
||||||
|
string Passport = 2; // User Passport
|
||||||
|
string Password = 3; // User Password
|
||||||
|
string Nickname = 4; // User Nickname
|
||||||
|
string Score = 5; // Total score amount.
|
||||||
|
google.protobuf.Timestamp CreateAt = 6; // Created Time
|
||||||
|
google.protobuf.Timestamp UpdateAt = 7; // Updated Time
|
||||||
|
}
|
@ -139,7 +139,7 @@ func Test_DB_Query(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestModelSave(t *testing.T) {
|
func TestModelSave(t *testing.T) {
|
||||||
table := createTable("test")
|
table := createTable()
|
||||||
defer dropTable(table)
|
defer dropTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
type User struct {
|
type User struct {
|
||||||
|
@ -2590,7 +2590,7 @@ func Test_Model_ScanAndCount(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Model_Save(t *testing.T) {
|
func Test_Model_Save(t *testing.T) {
|
||||||
table := createTable("test")
|
table := createTable()
|
||||||
defer dropTable(table)
|
defer dropTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
type User struct {
|
type User struct {
|
||||||
|
@ -1101,7 +1101,7 @@ func Test_Model_WhereOrNotLike(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_Model_Save(t *testing.T) {
|
func Test_Model_Save(t *testing.T) {
|
||||||
table := createTable("test")
|
table := createTable()
|
||||||
defer dropTable(table)
|
defer dropTable(table)
|
||||||
gtest.C(t, func(t *gtest.T) {
|
gtest.C(t, func(t *gtest.T) {
|
||||||
type User struct {
|
type User struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user