Skip to content

Commit

Permalink
Refactor test methods to make them more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlycoconuts committed Sep 30, 2024
1 parent 1ffe047 commit f6e5f3e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions api/it/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ func connectionString(db string) string {
}

func create(conn *sql.DB, dbName string) (*sql.DB, error) {
var err error
if _, err := conn.Exec("CREATE DATABASE " + dbName); err != nil {
return nil, err
} else if testDb, err := sql.Open("postgres", connectionString(dbName)); err != nil {
}

var testDb *sql.DB
if testDb, err = sql.Open("postgres", connectionString(dbName)); err != nil {
if _, err := conn.Exec("DROP DATABASE " + dbName); err != nil {
log.Fatalf("Failed to cleanup integration test database: \n%s", err)
}
return nil, err
} else { //nolint:revive // https://github.com/mgechev/revive/issues/564 (false positive)
return testDb, nil
}
return testDb, nil
}

func migrate(db *sql.DB, dbName string) (*sql.DB, error) {
Expand Down Expand Up @@ -84,12 +87,14 @@ func CreateTestDatabase() (*gorm.DB, func(), error) {

cleanup()
return nil, nil, err
} else if gormDb, err := gorm.Open("postgres", testDb); err != nil {
}

var gormDb *gorm.DB
if gormDb, err = gorm.Open("postgres", testDb); err != nil {
cleanup()
return nil, nil, err
} else { //nolint:revive // https://github.com/mgechev/revive/issues/564 (false positive)
return gormDb, cleanup, nil
}
return gormDb, cleanup, nil
}

func WithTestDatabase(t *testing.T, test func(t *testing.T, db *gorm.DB)) {
Expand Down

0 comments on commit f6e5f3e

Please sign in to comment.