ability to drive database test connections from env variables

This commit is contained in:
Brad Rydzewski 2014-09-27 12:00:58 -07:00
parent d36f82784c
commit ae95232419
3 changed files with 24 additions and 3 deletions

View file

@ -2,6 +2,7 @@ package database
import (
"database/sql"
"os"
"github.com/drone/drone/server/datastore"
"github.com/drone/drone/server/datastore/database/migrate"
@ -41,7 +42,7 @@ func Connect(driver, datasource string) (*sql.DB, error) {
}
// MustConnect is a helper function that creates a
// new database commention and auto-generates the
// new database connection and auto-generates the
// database schema. An error causes a panic.
func MustConnect(driver, datasource string) *sql.DB {
db, err := Connect(driver, datasource)
@ -51,6 +52,26 @@ func MustConnect(driver, datasource string) *sql.DB {
return db
}
// mustConnectTest is a helper function that creates a
// new database connection using environment variables.
// If not environment varaibles are found, the default
// in-memory SQLite database is used.
func mustConnectTest() *sql.DB {
var (
driver = os.Getenv("TEST_DRIVER")
datasource = os.Getenv("TEST_DATASOURCE")
)
if len(driver) == 0 {
driver = driverSqlite
datasource = ":memory:"
}
db, err := Connect(driver, datasource)
if err != nil {
panic(err)
}
return db
}
// New returns a new DataStore
func New(db *sql.DB) datastore.Datastore {
return struct {

View file

@ -8,7 +8,7 @@ import (
)
func TestRepostore(t *testing.T) {
db := MustConnect("sqlite3", ":memory:")
db := mustConnectTest()
rs := NewRepostore(db)
ps := NewPermstore(db)
defer db.Close()

View file

@ -8,7 +8,7 @@ import (
)
func TestUserstore(t *testing.T) {
db := MustConnect("sqlite3", ":memory:")
db := mustConnectTest()
us := NewUserstore(db)
defer db.Close()