Skip to content

Commit

Permalink
feat: add prefix "_gent_test_" for test global var
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Jan 16, 2024
1 parent 16a35f1 commit d96e897
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions internal/template/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 11 additions & 11 deletions internal/template/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
}
Expand All @@ -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")
}
Expand All @@ -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 })
Expand Down

0 comments on commit d96e897

Please sign in to comment.