Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add VISOR_TEST_DB environment variable to specify test database #35

Merged
merged 1 commit into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.


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
}