mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-11 14:48:14 +01:00
graph/db: refactor to make postgres fixtures re-usable
In preparation for tests where we will want to spin up SQL DBs many times, we do some refactoring so that it is easy to re-use postgres fixtures since those are expensive to spin up.
This commit is contained in:
@@ -6,9 +6,46 @@ import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/lightningnetwork/lnd/sqldb"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// NewTestDB is a helper function that creates a SQLStore backed by a SQL
|
||||
// database for testing.
|
||||
func NewTestDB(t testing.TB) V1Store {
|
||||
return NewTestDBWithFixture(t, nil)
|
||||
}
|
||||
|
||||
// NewTestDBFixture creates a new sqldb.TestPgFixture for testing purposes.
|
||||
func NewTestDBFixture(t *testing.T) *sqldb.TestPgFixture {
|
||||
pgFixture := sqldb.NewTestPgFixture(t, sqldb.DefaultPostgresFixtureLifetime)
|
||||
t.Cleanup(func() {
|
||||
pgFixture.TearDown(t)
|
||||
})
|
||||
return pgFixture
|
||||
}
|
||||
|
||||
// NewTestDBWithFixture is a helper function that creates a SQLStore backed by a
|
||||
// SQL database for testing.
|
||||
func NewTestDBWithFixture(t testing.TB, pgFixture *sqldb.TestPgFixture) V1Store {
|
||||
var querier BatchedSQLQueries
|
||||
if pgFixture == nil {
|
||||
querier = newBatchQuerier(t)
|
||||
} else {
|
||||
querier = newBatchQuerierWithFixture(t, pgFixture)
|
||||
}
|
||||
|
||||
store, err := NewSQLStore(
|
||||
&SQLStoreConfig{
|
||||
ChainHash: *chaincfg.MainNetParams.GenesisHash,
|
||||
}, querier,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
// newBatchQuerier creates a new BatchedSQLQueries instance for testing
|
||||
// using a PostgreSQL database fixture.
|
||||
func newBatchQuerier(t testing.TB) BatchedSQLQueries {
|
||||
@@ -19,6 +56,14 @@ func newBatchQuerier(t testing.TB) BatchedSQLQueries {
|
||||
pgFixture.TearDown(t)
|
||||
})
|
||||
|
||||
return newBatchQuerierWithFixture(t, pgFixture)
|
||||
}
|
||||
|
||||
// newBatchQuerierWithFixture creates a new BatchedSQLQueries instance for
|
||||
// testing using a PostgreSQL database fixture.
|
||||
func newBatchQuerierWithFixture(t testing.TB,
|
||||
pgFixture *sqldb.TestPgFixture) BatchedSQLQueries {
|
||||
|
||||
db := sqldb.NewTestPostgresDB(t, pgFixture).BaseDB
|
||||
|
||||
return sqldb.NewTransactionExecutor(
|
||||
|
||||
Reference in New Issue
Block a user