github.com/jxgolibs/go-oauth2-server@v1.0.1/test-util/database.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/RichardKnop/go-fixtures"
     8  	"github.com/RichardKnop/go-oauth2-server/util/migrations"
     9  	"github.com/jinzhu/gorm"
    10  	// Drivers
    11  	_ "github.com/lib/pq"
    12  	_ "github.com/mattn/go-sqlite3"
    13  )
    14  
    15  // CreateTestDatabase recreates the test database and
    16  // runs migrations and fixtures as passed in, returning
    17  // a pointer to the database
    18  func CreateTestDatabase(dbPath string, migrationFunctions []func(*gorm.DB) error, fixtureFiles []string) (*gorm.DB, error) {
    19  
    20  	// Init in-memory test database
    21  	inMemoryDB, err := rebuildDatabase(dbPath)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	// Run all migrations
    27  	migrations.MigrateAll(inMemoryDB, migrationFunctions)
    28  
    29  	// Load data from data
    30  	if err = fixtures.LoadFiles(fixtureFiles, inMemoryDB.DB(), "sqlite"); err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	return inMemoryDB, nil
    35  }
    36  
    37  // CreateTestDatabasePostgres is similar to CreateTestDatabase but it uses
    38  // Postgres instead of sqlite, this is needed for testing packages that rely
    39  // on some Postgres specifuc features (such as table inheritance)
    40  func CreateTestDatabasePostgres(dbHost, dbUser, dbName string, migrationFunctions []func(*gorm.DB) error, fixtureFiles []string) (*gorm.DB, error) {
    41  
    42  	// Postgres test database
    43  	db, err := rebuildDatabasePostgres(dbHost, dbUser, dbName)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	// Run all migrations
    49  	migrations.MigrateAll(db, migrationFunctions)
    50  
    51  	// Load data from data
    52  	if err = fixtures.LoadFiles(fixtureFiles, db.DB(), "postgres"); err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	return db, nil
    57  }
    58  
    59  // rebuildDatabase attempts to delete an existing in memory
    60  // database and rebuild it, returning a pointer to it
    61  func rebuildDatabase(dbPath string) (*gorm.DB, error) {
    62  	// Delete the current database if it exists
    63  	os.Remove(dbPath)
    64  
    65  	// Init a new in-memory test database connection
    66  	inMemoryDB, err := gorm.Open("sqlite3", dbPath)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	return inMemoryDB, nil
    71  }
    72  
    73  // rebuildDatabase attempts to delete an existing Postgres
    74  // database and rebuild it, returning a pointer to it
    75  func rebuildDatabasePostgres(dbHost, dbUser, dbName string) (*gorm.DB, error) {
    76  	db, err := openPostgresDB(dbHost, dbUser, "template1")
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	if err := db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s;", dbName)).Error; err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	if err := db.Exec(fmt.Sprintf("CREATE DATABASE %s;", dbName)).Error; err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	return openPostgresDB(dbHost, dbUser, dbName)
    90  }
    91  
    92  func openPostgresDB(dbHost, dbUser, dbName string) (*gorm.DB, error) {
    93  	// Init a new postgres test database connection
    94  	db, err := gorm.Open("postgres",
    95  		fmt.Sprintf(
    96  			"sslmode=disable host=%s port=5432 user=%s password='' dbname=%s",
    97  			dbHost,
    98  			dbUser,
    99  			dbName,
   100  		),
   101  	)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	return db, nil
   106  }