Merge pull request #17 from marinbek/master

NewPGStore should not ignore errors
This commit is contained in:
Anton Lindström 2016-07-19 21:16:22 +02:00 committed by GitHub
commit f20977c3dd
3 changed files with 21 additions and 8 deletions

View File

@ -8,7 +8,12 @@ import (
) )
func TestCleanup(t *testing.T) { func TestCleanup(t *testing.T) {
ss := NewPGStore(os.Getenv("PGSTORE_TEST_CONN"), []byte(secret)) ss, err := NewPGStore(os.Getenv("PGSTORE_TEST_CONN"), []byte(secret))
if err != nil {
t.Fatal("Failed to get store", err)
}
if ss == nil { if ss == nil {
t.Skip("This test requires a real database") t.Skip("This test requires a real database")
} }

View File

@ -33,11 +33,11 @@ type Session struct {
// NewPGStore creates a new PGStore instance and a new database/sql pool. // NewPGStore creates a new PGStore instance and a new database/sql pool.
// This will also create in the database the schema needed by pgstore. // This will also create in the database the schema needed by pgstore.
func NewPGStore(dbURL string, keyPairs ...[]byte) *PGStore { func NewPGStore(dbURL string, keyPairs ...[]byte) (*PGStore, error) {
db, err := sql.Open("postgres", dbURL) db, err := sql.Open("postgres", dbURL)
if err != nil { if err != nil {
// Ignore and return nil // Ignore and return nil
return nil return nil, err
} }
return NewPGStoreFromPool(db, keyPairs...) return NewPGStoreFromPool(db, keyPairs...)
} }
@ -45,7 +45,7 @@ func NewPGStore(dbURL string, keyPairs ...[]byte) *PGStore {
// NewPGStoreFromPool creates a new PGStore instance from an existing // NewPGStoreFromPool creates a new PGStore instance from an existing
// database/sql pool. // database/sql pool.
// This will also create in the database the schema needed by pgstore. // This will also create in the database the schema needed by pgstore.
func NewPGStoreFromPool(db *sql.DB, keyPairs ...[]byte) *PGStore { func NewPGStoreFromPool(db *sql.DB, keyPairs ...[]byte) (*PGStore, error) {
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}} dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
dbStore := &PGStore{ dbStore := &PGStore{
@ -63,10 +63,10 @@ func NewPGStoreFromPool(db *sql.DB, keyPairs ...[]byte) *PGStore {
if err != nil { if err != nil {
// Ignore and return nil // Ignore and return nil
return nil return nil, err
} }
return dbStore return dbStore, nil
} }
// Close closes the database connection // Close closes the database connection

View File

@ -27,7 +27,11 @@ func (ho headerOnlyResponseWriter) WriteHeader(int) {
var secret = "EyaC2BPcJtNqU3tjEHy+c+Wmqc1yihYIbUWEl/jk0Ga73kWBclmuSFd9HuJKwJw/Wdsh1XnjY2Bw1HBVph6WOw==" var secret = "EyaC2BPcJtNqU3tjEHy+c+Wmqc1yihYIbUWEl/jk0Ga73kWBclmuSFd9HuJKwJw/Wdsh1XnjY2Bw1HBVph6WOw=="
func TestPGStore(t *testing.T) { func TestPGStore(t *testing.T) {
ss := NewPGStore(os.Getenv("PGSTORE_TEST_CONN"), []byte(secret)) ss, err := NewPGStore(os.Getenv("PGSTORE_TEST_CONN"), []byte(secret))
if err != nil {
t.Fatal("failed to get store", err.Error())
}
if ss == nil { if ss == nil {
t.Skip("This test requires a real database") t.Skip("This test requires a real database")
@ -121,7 +125,11 @@ func TestPGStore(t *testing.T) {
} }
func TestSessionOptionsAreUniquePerSession(t *testing.T) { func TestSessionOptionsAreUniquePerSession(t *testing.T) {
ss := NewPGStore(os.Getenv("PGSTORE_TEST_CONN"), []byte(secret)) ss, err := NewPGStore(os.Getenv("PGSTORE_TEST_CONN"), []byte(secret))
if err != nil {
t.Fatal("Failed to get store", err)
}
if ss == nil { if ss == nil {
t.Skip("This test requires a real database") t.Skip("This test requires a real database")