Skip to content

Commit

Permalink
feat: add VISOR_TEST_DB environment variable to specify test database (
Browse files Browse the repository at this point in the history
  • Loading branch information
iand authored Sep 25, 2020
1 parent f0dc231 commit 7f8acce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
7 changes: 4 additions & 3 deletions services/indexer/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/filecoin-project/sentinel-visor/lens"
"github.com/filecoin-project/sentinel-visor/model/blocks"
"github.com/filecoin-project/sentinel-visor/storage"
"github.com/filecoin-project/sentinel-visor/testutil"
)

func init() {
Expand All @@ -47,11 +48,11 @@ func (nodeWrapper) Store() adt.Store {
}

func TestIndex(t *testing.T) {
if testing.Short() {
t.Skip("short testing specified but this test requires external dependencies")
if testing.Short() || !testutil.DatabaseAvailable() {
t.Skip("short testing requested or VISOR_TEST_DB not set")
}

db, err := storage.NewDatabase(context.Background(), "postgres://postgres:password@localhost:5432/postgres?sslmode=disable", 10)
db, err := storage.NewDatabase(context.Background(), testutil.Database(), 10)
require.NoError(t, err, "connecting to database")

t.Logf("truncating database tables")
Expand Down
2 changes: 1 addition & 1 deletion storage/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ var models = []interface{}{

func NewDatabase(ctx context.Context, url string, poolSize int) (*Database, error) {
opt, err := pg.ParseURL(url)
opt.PoolSize = poolSize
if err != nil {
return nil, xerrors.Errorf("parse database URL: %w", err)
}
opt.PoolSize = poolSize

db := pg.Connect(opt)
db = db.WithContext(ctx)
Expand Down
8 changes: 5 additions & 3 deletions storage/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/go-pg/pg/v10"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/sentinel-visor/testutil"
)

// may want to change this to use a named schema such as lotus
Expand Down Expand Up @@ -378,11 +380,11 @@ var expectedTables = []struct {
}

func TestCreateSchema(t *testing.T) {
if testing.Short() {
t.Skip("short testing specified")
if testing.Short() || !testutil.DatabaseAvailable() {
t.Skip("short testing requested or VISOR_TEST_DB not set")
}

db, err := NewDatabase(context.Background(), "postgres://postgres:password@localhost:5432/postgres?sslmode=disable", 10)
db, err := NewDatabase(context.Background(), testutil.Database(), 10)
if !assert.NoError(t, err, "connecting to database") {
return
}
Expand Down
17 changes: 17 additions & 0 deletions testutil/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package testutil

import (
"os"
)

var testDatabase = os.Getenv("VISOR_TEST_DB")

// DatabaseAvailable reports whether a database is available for testing
func DatabaseAvailable() bool {
return testDatabase != ""
}

// Database returns the connection string for connecting to the test database
func Database() string {
return testDatabase
}

0 comments on commit 7f8acce

Please sign in to comment.