test(database): improve
continuous-integration/drone the build failed Details

This commit is contained in:
Geno 2021-06-02 18:00:53 +02:00
parent 95b99107f5
commit a0fe660f97
4 changed files with 49 additions and 6 deletions

12
database/error.go Normal file
View File

@ -0,0 +1,12 @@
package database
import (
"errors"
)
var (
// ErrNotConnected - database is not connected
ErrNotConnected = errors.New("database is not connected")
// ErrNothingToMigrate if nothing has to be migrated
ErrNothingToMigrate = errors.New("there is nothing to migrate")
)

View File

@ -40,6 +40,9 @@ func (config *Database) Run() error {
// Status get status - is database pingable // Status get status - is database pingable
func (config *Database) Status() error { func (config *Database) Status() error {
if config.DB == nil {
return ErrNotConnected
}
sqlDB, err := config.DB.DB() sqlDB, err := config.DB.DB()
if err != nil { if err != nil {
return err return err

View File

@ -1 +1,35 @@
package database package database
import (
"testing"
"github.com/stretchr/testify/assert"
)
var (
// DBConnection - url to database on setting up default WebService for webtest
DBConnection = "user=root password=root dbname=defaultdb host=localhost port=26257 sslmode=disable"
)
func TestStatus(t *testing.T) {
assert := assert.New(t)
d := Database{
Debug: true,
}
err := d.Status()
assert.Error(err)
assert.Equal(ErrNotConnected, err)
err = d.Run()
assert.Error(err)
assert.Contains(err.Error(), "dial error")
d.Connection = DBConnection
err = d.Run()
assert.Error(err)
assert.Equal(ErrNothingToMigrate, err)
err = d.Status()
assert.NoError(err)
}

View File

@ -1,18 +1,12 @@
package database package database
import ( import (
"errors"
"sort" "sort"
"github.com/bdlm/log" "github.com/bdlm/log"
gormigrate "github.com/go-gormigrate/gormigrate/v2" gormigrate "github.com/go-gormigrate/gormigrate/v2"
) )
var (
// ErrNothingToMigrate if nothing has to be migrated
ErrNothingToMigrate = errors.New("there is nothing to migrate")
)
func (config *Database) sortedMigration(testdata bool) []*gormigrate.Migration { func (config *Database) sortedMigration(testdata bool) []*gormigrate.Migration {
var migrations []*gormigrate.Migration var migrations []*gormigrate.Migration
for _, v := range config.migrations { for _, v := range config.migrations {