use helper function to create test db connection for planned pg support

This commit is contained in:
Brad Rydzewski 2015-10-19 11:45:53 -07:00
parent 505ec312b5
commit ffd42a1a0e
10 changed files with 46 additions and 41 deletions

View file

@ -8,7 +8,7 @@ import (
) )
func TestBuild(t *testing.T) { func TestBuild(t *testing.T) {
db := database.Open("sqlite3", ":memory:") db := database.OpenTest()
defer db.Close() defer db.Close()
g := goblin.Goblin(t) g := goblin.Goblin(t)

View file

@ -8,7 +8,7 @@ import (
) )
func TestJob(t *testing.T) { func TestJob(t *testing.T) {
db := database.Open("sqlite3", ":memory:") db := database.OpenTest()
defer db.Close() defer db.Close()
g := goblin.Goblin(t) g := goblin.Goblin(t)

View file

@ -8,7 +8,7 @@ import (
) )
func TestKey(t *testing.T) { func TestKey(t *testing.T) {
db := database.Open("sqlite3", ":memory:") db := database.OpenTest()
defer db.Close() defer db.Close()
g := goblin.Goblin(t) g := goblin.Goblin(t)

View file

@ -10,7 +10,7 @@ import (
) )
func TestLog(t *testing.T) { func TestLog(t *testing.T) {
db := database.Open("sqlite3", ":memory:") db := database.OpenTest()
defer db.Close() defer db.Close()
g := goblin.Goblin(t) g := goblin.Goblin(t)

View file

@ -8,7 +8,7 @@ import (
) )
func TestNode(t *testing.T) { func TestNode(t *testing.T) {
db := database.Open("sqlite3", ":memory:") db := database.OpenTest()
defer db.Close() defer db.Close()
g := goblin.Goblin(t) g := goblin.Goblin(t)

View file

@ -8,7 +8,7 @@ import (
) )
func TestRepostore(t *testing.T) { func TestRepostore(t *testing.T) {
db := database.Open("sqlite3", ":memory:") db := database.OpenTest()
defer db.Close() defer db.Close()
g := goblin.Goblin(t) g := goblin.Goblin(t)

View file

@ -8,7 +8,7 @@ import (
) )
func TestUserstore(t *testing.T) { func TestUserstore(t *testing.T) {
db := database.Open("sqlite3", ":memory:") db := database.OpenTest()
defer db.Close() defer db.Close()
g := goblin.Goblin(t) g := goblin.Goblin(t)

View file

@ -4,28 +4,20 @@ import (
"code.google.com/p/go.net/context" "code.google.com/p/go.net/context"
) )
const reqkey = "remote" const key = "remote"
// NewContext returns a Context whose Value method returns // Setter defines a context that enables setting values.
// the applications Remote instance. type Setter interface {
func NewContext(parent context.Context, v Remote) context.Context { Set(string, interface{})
return &wrapper{parent, v}
}
type wrapper struct {
context.Context
v Remote
}
// Value returns the named key from the context.
func (c *wrapper) Value(key interface{}) interface{} {
if key == reqkey {
return c.v
}
return c.Context.Value(key)
} }
// FromContext returns the Remote associated with this context. // FromContext returns the Remote associated with this context.
func FromContext(c context.Context) Remote { func FromContext(c context.Context) Remote {
return c.Value(reqkey).(Remote) return c.Value(key).(Remote)
}
// ToContext adds the Remote to this context if it supports
// the Setter interface.
func ToContext(c Setter, r Remote) {
c.Set(key, r)
} }

View file

@ -4,6 +4,7 @@ package database
import ( import (
"database/sql" "database/sql"
"os"
"github.com/drone/drone/shared/envconfig" "github.com/drone/drone/shared/envconfig"
@ -49,3 +50,15 @@ func Open(driver, config string) *sql.DB {
} }
return db return db
} }
func OpenTest() *sql.DB {
var (
driver = "sqlite3"
config = ":memory:"
)
if os.Getenv("DATABASE_DRIVER") != "" {
driver = os.Getenv("DATABASE_DRIVER")
config = os.Getenv("DATABASE_CONFIG")
}
return Open(driver, config)
}

View file

@ -2,15 +2,15 @@
CREATE TABLE users ( CREATE TABLE users (
user_id SERIAL PRIMARY KEY user_id SERIAL PRIMARY KEY
,user_login VARCHAR(500) ,user_login VARCHAR(40)
,user_token VARCHAR(500) ,user_token VARCHAR(128)
,user_secret VARCHAR(500) ,user_secret VARCHAR(128)
,user_expiry INTEGER ,user_expiry INTEGER
,user_email VARCHAR(500) ,user_email VARCHAR(256)
,user_avatar VARCHAR(500) ,user_avatar VARCHAR(256)
,user_active BOOLEAN ,user_active BOOLEAN
,user_admin BOOLEAN ,user_admin BOOLEAN
,user_hash VARCHAR(500) ,user_hash VARCHAR(128)
,UNIQUE(user_login) ,UNIQUE(user_login)
); );
@ -60,21 +60,21 @@ CREATE TABLE builds (
build_id SERIAL PRIMARY KEY build_id SERIAL PRIMARY KEY
,build_repo_id INTEGER ,build_repo_id INTEGER
,build_number INTEGER ,build_number INTEGER
,build_event VARCHAR(500) ,build_event VARCHAR(25)
,build_status VARCHAR(500) ,build_status VARCHAR(25)
,build_enqueued INTEGER ,build_enqueued INTEGER
,build_created INTEGER ,build_created INTEGER
,build_started INTEGER ,build_started INTEGER
,build_finished INTEGER ,build_finished INTEGER
,build_commit VARCHAR(500) ,build_commit VARCHAR(40)
,build_branch VARCHAR(500) ,build_branch VARCHAR(256)
,build_ref VARCHAR(500) ,build_ref VARCHAR(512)
,build_refspec VARCHAR(1000) ,build_refspec VARCHAR(512)
,build_remote VARCHAR(500) ,build_remote VARCHAR(512)
,build_title VARCHAR(1000) ,build_title VARCHAR(1000)
,build_message VARCHAR(2000) ,build_message VARCHAR(2000)
,build_timestamp INTEGER ,build_timestamp INTEGER
,build_author VARCHAR(500) ,build_author VARCHAR(40)
,build_avatar VARCHAR(1000) ,build_avatar VARCHAR(1000)
,build_email VARCHAR(500) ,build_email VARCHAR(500)
,build_link VARCHAR(1000) ,build_link VARCHAR(1000)
@ -90,7 +90,7 @@ CREATE TABLE jobs (
,job_node_id INTEGER ,job_node_id INTEGER
,job_build_id INTEGER ,job_build_id INTEGER
,job_number INTEGER ,job_number INTEGER
,job_status VARCHAR(500) ,job_status VARCHAR(25)
,job_exit_code INTEGER ,job_exit_code INTEGER
,job_started INTEGER ,job_started INTEGER
,job_enqueued INTEGER ,job_enqueued INTEGER