From d96e897a5a072d032723dbf57955f3497a707b3b Mon Sep 17 00:00:00 2001 From: tr1v3r Date: Tue, 16 Jan 2024 13:02:35 +0800 Subject: [PATCH] feat: add prefix "_gent_test_" for test global var --- internal/template/method.go | 4 ++-- internal/template/query.go | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/template/method.go b/internal/template/method.go index 5d2eda59..d900795a 100644 --- a/internal/template/method.go +++ b/internal/template/method.go @@ -265,14 +265,14 @@ func ({{.S}} *{{.QueryStructName}}Do) withDO(do gen.Dao) (*{{.QueryStructName}}D const CRUDMethodTest = ` func init() { InitializeDB() - err := db.AutoMigrate(&{{.StructInfo.Package}}.{{.ModelStructName}}{}) + err := _gen_test_db.AutoMigrate(&{{.StructInfo.Package}}.{{.ModelStructName}}{}) if err != nil{ fmt.Printf("Error: AutoMigrate(&{{.StructInfo.Package}}.{{.ModelStructName}}{}) fail: %s", err) } } func Test_{{.QueryStructName}}Query(t *testing.T) { - {{.QueryStructName}} := new{{.ModelStructName}}(db) + {{.QueryStructName}} := new{{.ModelStructName}}(_gen_test_db) {{.QueryStructName}} = *{{.QueryStructName}}.As({{.QueryStructName}}.TableName()) _do := {{.QueryStructName}}.WithContext(context.Background()).Debug() diff --git a/internal/template/query.go b/internal/template/query.go index 390eb635..d508d4e3 100644 --- a/internal/template/query.go +++ b/internal/template/query.go @@ -114,22 +114,22 @@ func (q *QueryTx) RollbackTo(name string) error { // QueryMethodTest query method test template const QueryMethodTest = ` -const dbName = "gen_test.db" +const _gen_test_db_name = "gen_test.db" -var db *gorm.DB -var once sync.Once +var _gen_test_db *gorm.DB +var _gen_test_once sync.Once func init() { InitializeDB() - db.AutoMigrate(&_another{}) + _gen_test_db.AutoMigrate(&_another{}) } func InitializeDB() { - once.Do(func() { + _gen_test_once.Do(func() { var err error - db, err = gorm.Open(sqlite.Open(dbName), &gorm.Config{}) + _gen_test_db, err = gorm.Open(sqlite.Open(_gen_test_db_name), &gorm.Config{}) if err != nil { - panic(fmt.Errorf("open sqlite %q fail: %w", dbName, err)) + panic(fmt.Errorf("open sqlite %q fail: %w", _gen_test_db_name, err)) } }) } @@ -147,13 +147,13 @@ type _another struct { func (*_another) TableName() string { return "another_for_unit_test" } func Test_Available(t *testing.T) { - if !Use(db).Available() { + if !Use(_gen_test_db).Available() { t.Errorf("query.Available() == false") } } func Test_WithContext(t *testing.T) { - query := Use(db) + query := Use(_gen_test_db) if !query.Available() { t.Errorf("query Use(db) fail: query.Available() == false") } @@ -174,9 +174,9 @@ func Test_WithContext(t *testing.T) { } func Test_Transaction(t *testing.T) { - query := Use(db) + query := Use(_gen_test_db) if !query.Available() { - t.Errorf("query Use(db) fail: query.Available() == false") + t.Errorf("query Use(_gen_test_db) fail: query.Available() == false") } err := query.Transaction(func(tx *Query) error { return nil })