From a0fe660f97688c3037fbccfa647e18208750e6be Mon Sep 17 00:00:00 2001 From: Geno Date: Wed, 2 Jun 2021 18:00:53 +0200 Subject: [PATCH] test(database): improve --- database/error.go | 12 ++++++++++++ database/main.go | 3 +++ database/main_test.go | 34 ++++++++++++++++++++++++++++++++++ database/migration.go | 6 ------ 4 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 database/error.go diff --git a/database/error.go b/database/error.go new file mode 100644 index 0000000..4392f12 --- /dev/null +++ b/database/error.go @@ -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") +) diff --git a/database/main.go b/database/main.go index a43de0c..5381a68 100644 --- a/database/main.go +++ b/database/main.go @@ -40,6 +40,9 @@ func (config *Database) Run() error { // Status get status - is database pingable func (config *Database) Status() error { + if config.DB == nil { + return ErrNotConnected + } sqlDB, err := config.DB.DB() if err != nil { return err diff --git a/database/main_test.go b/database/main_test.go index 636bab8..063db55 100644 --- a/database/main_test.go +++ b/database/main_test.go @@ -1 +1,35 @@ 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) +} diff --git a/database/migration.go b/database/migration.go index 696fe40..2e64c14 100644 --- a/database/migration.go +++ b/database/migration.go @@ -1,18 +1,12 @@ package database import ( - "errors" "sort" "github.com/bdlm/log" 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 { var migrations []*gormigrate.Migration for _, v := range config.migrations {