2018-12-10 14:50:12 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedbtest
|
|
|
|
|
|
|
|
// This package should be referenced only in test files!
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2018-12-10 14:50:12 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// postgres connstring that works with docker-compose
|
|
|
|
defaultPostgresConn = "postgres://storj:storj-pass@test-postgres/teststorj?sslmode=disable"
|
2019-01-15 16:08:45 +00:00
|
|
|
defaultSqliteConn = "sqlite3://file::memory:?mode=memory"
|
2018-12-10 14:50:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testPostgres = flag.String("postgres-test-db", os.Getenv("STORJ_POSTGRES_TEST"), "PostgreSQL test database connection string")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run method will iterate over all supported databases. Will establish
|
|
|
|
// connection and will create tables for each DB.
|
2018-12-27 09:56:25 +00:00
|
|
|
func Run(t *testing.T, test func(t *testing.T, db satellite.DB)) {
|
2018-12-10 14:50:12 +00:00
|
|
|
for _, dbInfo := range []struct {
|
|
|
|
dbName string
|
|
|
|
dbURL string
|
|
|
|
dbMessage string
|
|
|
|
}{
|
|
|
|
{"Sqlite", defaultSqliteConn, ""},
|
|
|
|
{"Postgres", *testPostgres, "Postgres flag missing, example: -postgres-test-db=" + defaultPostgresConn},
|
|
|
|
} {
|
|
|
|
t.Run(dbInfo.dbName, func(t *testing.T) {
|
|
|
|
if dbInfo.dbURL == "" {
|
|
|
|
t.Skipf("Database %s connection string not provided. %s", dbInfo.dbName, dbInfo.dbMessage)
|
|
|
|
}
|
|
|
|
|
2018-12-11 09:30:09 +00:00
|
|
|
db, err := satellitedb.New(dbInfo.dbURL)
|
2018-12-10 14:50:12 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = db.CreateTables()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-12-11 09:30:09 +00:00
|
|
|
test(t, db)
|
2018-12-10 14:50:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|