github.com/miguelgrubin/gin-boilerplate@v0.0.0-20231208120009-f8f00c6d4138/pkg/petshop/infrastructure/storage/sql_setup_test.go (about) 1 package storage_test 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "path/filepath" 8 "runtime" 9 10 "github.com/miguelgrubin/gin-boilerplate/pkg" 11 "github.com/miguelgrubin/gin-boilerplate/pkg/petshop/infrastructure/storage" 12 "github.com/miguelgrubin/gin-boilerplate/pkg/shared/infrastructure" 13 "gorm.io/gorm" 14 ) 15 16 func DBConn() (*gorm.DB, error) { 17 return LocalDatabase() 18 } 19 20 func LocalDatabase() (*gorm.DB, error) { 21 var ( 22 _, b, _, _ = runtime.Caller(0) 23 basepath = filepath.Dir(b) 24 ) 25 rootpath := fmt.Sprintf("%s/../../../../test", basepath) 26 err := os.Chdir(rootpath) 27 28 if err != nil { 29 log.Println("Can not load test config file") 30 return nil, err 31 } 32 33 appConfig, err := pkg.ReadConfig() 34 if err != nil { 35 return nil, err 36 } 37 38 db := infrastructure.NewDbConnection(appConfig.Database.Driver, appConfig.Database.Address) 39 40 err = db.Migrator().DropTable(&storage.PetEntity{}) 41 if err != nil { 42 return nil, err 43 } 44 45 err = storage.Automigrate(db) 46 if err != nil { 47 return nil, err 48 } 49 return db, nil 50 }