Skip to content

Commit

Permalink
emhace: unit testing cases update for package cmd/gf and `contrib/d…
Browse files Browse the repository at this point in the history
…rivers` (gogf#3453)
  • Loading branch information
oldme-git authored Apr 10, 2024
1 parent d7a0482 commit 6e2d238
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 24 deletions.
4 changes: 1 addition & 3 deletions cmd/gf/internal/cmd/cmd_z_unit_gen_ctrl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) {
defer gfile.Remove(path)

_, err = genctrl.CGenCtrl{}.Ctrl(ctx, in)
if err != nil {
panic(err)
}
t.AssertNil(err)

// apiInterface file
var (
Expand Down
106 changes: 91 additions & 15 deletions cmd/gf/internal/cmd/cmd_z_unit_gen_pbentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ package cmd

import (
"fmt"
"os"
"path/filepath"
"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/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
"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) {
var (
err error
Expand All @@ -38,31 +38,107 @@ func Test_Gen_Pbentity_NameCase(t *testing.T) {
}
}
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)
t.AssertNil(err)
defer gfile.Remove(path)

root, err := gcmd.NewFromObject(GF)
_, err = genpbentity.CGenPbEntity{}.PbEntity(ctx, in)
t.AssertNil(err)
err = root.AddObject(
Gen,

// 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.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)
os.Args = []string{"gf", "gen", "pbentity", "-l", link, "-p", path, "-package=unittest", "-nameCase=SnakeScreaming"}

err = root.RunWithError(ctx)
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)

files := []string{
filepath.FromSlash(path + "/table_user.proto"),
}
_, 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"),
})

testPath := gtest.DataPath("genpbentity", "generated_user")
// contents
testPath := gtest.DataPath("genpbentity", "generated")
expectFiles := []string{
filepath.FromSlash(testPath + "/table_user.proto"),
testPath + filepath.FromSlash("/table_user_snake_screaming.proto"),
}
// check files content
for i := range files {
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/gf/internal/cmd/cmd_z_unit_gen_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ func Test_Gen_Service_Default(t *testing.T) {
defer gfile.Remove(path)

_, err = genservice.CGenService{}.Service(ctx, in)
if err != nil {
panic(err)
}
t.AssertNil(err)

// logic file
var (
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion contrib/drivers/dm/dm_z_unit_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func Test_DB_Query(t *testing.T) {
}

func TestModelSave(t *testing.T) {
table := createTable("test")
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
type User struct {
Expand Down
2 changes: 1 addition & 1 deletion contrib/drivers/mssql/mssql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2590,7 +2590,7 @@ func Test_Model_ScanAndCount(t *testing.T) {
}

func Test_Model_Save(t *testing.T) {
table := createTable("test")
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
type User struct {
Expand Down
2 changes: 1 addition & 1 deletion contrib/drivers/oracle/oracle_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ func Test_Model_WhereOrNotLike(t *testing.T) {
}

func Test_Model_Save(t *testing.T) {
table := createTable("test")
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
type User struct {
Expand Down

0 comments on commit 6e2d238

Please sign in to comment.