Skip to content

Commit

Permalink
test(cmd/gf): add unit testing case generating dao/do/entity files fo…
Browse files Browse the repository at this point in the history
…r sqlite in command `gen dao` (#3808)
  • Loading branch information
gqcn authored Oct 8, 2024
1 parent 7a68898 commit ae3ae8b
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 8 deletions.
77 changes: 77 additions & 0 deletions cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,80 @@ func Test_Gen_Dao_Issue3749(t *testing.T) {
}
})
}

func Test_Gen_Dao_Sqlite3(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
table = "table_user"
path = gfile.Temp(guid.S())
linkSqlite3 = fmt.Sprintf("sqlite::@file(%s/db.sqlite3)", path)
sqlContent = fmt.Sprintf(
gtest.DataContent(`gendao`, `sqlite3`, `user.sqlite3.sql`),
table,
)
)
err = gfile.Mkdir(path)
t.AssertNil(err)
defer gfile.Remove(path)

dbSqlite3, err := gdb.New(gdb.ConfigNode{
Link: linkSqlite3,
})
t.AssertNil(err)

array := gstr.SplitAndTrim(sqlContent, ";")
for _, v := range array {
if v == "" {
continue
}
if _, err = dbSqlite3.Exec(ctx, v); err != nil {
t.AssertNil(err)
}
}

var (
group = "test"
in = gendao.CGenDaoInput{
Path: path,
Link: linkSqlite3,
Group: group,
Tables: table,
}
)
err = gutil.FillStructWithDefault(&in)
t.AssertNil(err)

// for go mod import path auto retrieve.
err = gfile.Copy(
gtest.DataPath("gendao", "go.mod.txt"),
gfile.Join(path, "go.mod"),
)
t.AssertNil(err)

_, err = gendao.CGenDao{}.Dao(ctx, in)
t.AssertNil(err)
defer gfile.Remove(path)

// files
files, err := gfile.ScanDir(path, "*.go", true)
t.AssertNil(err)
t.Assert(files, []string{
filepath.FromSlash(path + "/dao/internal/table_user.go"),
filepath.FromSlash(path + "/dao/table_user.go"),
filepath.FromSlash(path + "/model/do/table_user.go"),
filepath.FromSlash(path + "/model/entity/table_user.go"),
})
// content
testPath := gtest.DataPath("gendao", "generated_user_sqlite3")
expectFiles := []string{
filepath.FromSlash(testPath + "/dao/internal/table_user.go"),
filepath.FromSlash(testPath + "/dao/table_user.go"),
filepath.FromSlash(testPath + "/model/do/table_user.go"),
filepath.FromSlash(testPath + "/model/entity/table_user.go"),
}
for i, _ := range files {
t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i]))
}
})
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// =================================================================================
// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
// =================================================================================

package dao

import (
"for-gendao-test/pkg/dao/internal"
)

// internalTableUserDao is internal type for wrapping internal DAO implements.
type internalTableUserDao = *internal.TableUserDao

// tableUserDao is the data access object for table table_user.
// You can define custom methods on it to extend its functionality as you wish.
type tableUserDao struct {
internalTableUserDao
}

var (
// TableUser is globally public accessible object for table table_user operations.
TableUser = tableUserDao{
internal.NewTableUserDao(),
}
)

// Fill with you ideas below.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions cmd/gf/internal/cmd/testdata/gendao/sqlite3/user.sqlite3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create table `%s`(
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
passport VARCHAR(45) NOT NULL DEFAULT passport,
password VARCHAR(128) NOT NULL DEFAULT password,
nickname VARCHAR(45),
created_at TIMESTAMP,
updated_at TIMESTAMP
)
11 changes: 3 additions & 8 deletions contrib/drivers/sqlite/sqlite_z_unit_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,16 @@ func createTableWithDb(db gdb.DB, table ...string) (name string) {

if _, err := db.Exec(ctx, fmt.Sprintf(`
CREATE TABLE %s (
id INTEGER PRIMARY KEY AUTOINCREMENT
UNIQUE
NOT NULL,
passport VARCHAR(45) NOT NULL
DEFAULT passport,
password VARCHAR(128) NOT NULL
DEFAULT password,
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
passport VARCHAR(45) NOT NULL DEFAULT passport,
password VARCHAR(128) NOT NULL DEFAULT password,
nickname VARCHAR(45),
create_time DATETIME
);
`, db.GetCore().QuoteWord(name),
)); err != nil {
gtest.Fatal(err)
}

return
}

Expand Down

0 comments on commit ae3ae8b

Please sign in to comment.