web/webtest: setup database only on request
continuous-integration/drone the build failed Details

This commit is contained in:
genofire 2021-09-28 04:31:23 +02:00
parent 1e1571a2c3
commit b3f53011f2
1 changed files with 42 additions and 39 deletions

View File

@ -23,10 +23,11 @@ var (
// Option to configure TestServer // Option to configure TestServer
type Option struct { type Option struct {
ReRun bool
Mailer bool
DBSetup func(db *database.Database)
ModuleLoader web.ModuleRegisterFunc ModuleLoader web.ModuleRegisterFunc
Database bool
DBReRun bool
DBSetup func(db *database.Database)
Mailer bool
} }
// TestServer - to run it without listen an server // TestServer - to run it without listen an server
@ -47,57 +48,57 @@ type Login struct {
// New starts WebService for testing // New starts WebService for testing
func New(modules web.ModuleRegisterFunc) (*TestServer, error) { func New(modules web.ModuleRegisterFunc) (*TestServer, error) {
return NewWithOption(Option{ModuleLoader: modules}) return Option{ModuleLoader: modules}.New()
} }
// NewWithDBSetup allows to reconfigure before ReRun the database - e.g. for adding Migration-Steps // NewWithDBSetup allows to reconfigure before ReRun the database - e.g. for adding Migration-Steps
func NewWithDBSetup(modules web.ModuleRegisterFunc, dbCall func(db *database.Database)) (*TestServer, error) { func NewWithDBSetup(modules web.ModuleRegisterFunc, dbCall func(db *database.Database)) (*TestServer, error) {
return NewWithOption(Option{ return Option{
ReRun: true, Database: true,
DBReRun: true,
DBSetup: dbCall, DBSetup: dbCall,
ModuleLoader: modules, ModuleLoader: modules,
}) }.New()
} }
// NewWithOption allows to configure WebService for testing // New allows to configure WebService for testing
func NewWithOption(option Option) (*TestServer, error) { func (option Option) New() (*TestServer, error) {
// db setup
dbConfig := database.Database{
Connection: DBConnection,
Testdata: true,
Debug: false,
LogLevel: 0,
}
if option.DBSetup != nil {
option.DBSetup(&dbConfig)
}
var err error
if option.ReRun {
err = dbConfig.ReRun()
} else {
err = dbConfig.Run()
}
if err != nil && err != database.ErrNothingToMigrate {
return nil, err
}
if dbConfig.DB == nil {
return nil, database.ErrNotConnected
}
// api setup // api setup
gin.EnableJsonDecoderDisallowUnknownFields() gin.EnableJsonDecoderDisallowUnknownFields()
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
ws := &web.Service{ ws := &web.Service{}
DB: dbConfig.DB,
}
ts := &TestServer{
DB: &dbConfig,
WS: ws,
}
ws.ModuleRegister(option.ModuleLoader)
ws.Session.Name = "mysession" ws.Session.Name = "mysession"
ws.Session.Secret = "hidden" ws.Session.Secret = "hidden"
ts := &TestServer{
WS: ws,
}
// db setup
if option.Database {
ts.DB = &database.Database{
Connection: DBConnection,
Testdata: true,
Debug: false,
LogLevel: 0,
}
if option.DBSetup != nil {
option.DBSetup(ts.DB)
}
var err error
if option.DBReRun {
err = ts.DB.ReRun()
} else {
err = ts.DB.Run()
}
if err != nil && err != database.ErrNothingToMigrate {
return nil, err
}
if ts.DB.DB == nil {
return nil, database.ErrNotConnected
}
ws.DB = ts.DB.DB
}
if option.Mailer { if option.Mailer {
mock, mail := mailer.NewFakeServer() mock, mail := mailer.NewFakeServer()
@ -109,6 +110,8 @@ func NewWithOption(option Option) (*TestServer, error) {
ts.Close = mock.Close ts.Close = mock.Close
} }
ws.ModuleRegister(option.ModuleLoader)
r := gin.Default() r := gin.Default()
ws.LoadSession(r) ws.LoadSession(r)
ws.Bind(r) ws.Bind(r)