lnd/graph/db/test_postgres.go
Oli 8047149c6a
multi: upgrade to btcd v2 modules
Migrate all btcd dependencies to the new per-package v2 modules (wire/v2,
txscript/v2, chaincfg/v2, chainhash/v2, btcutil/v2, psbt/v2, btcec/v2)
introduced by btcd v0.26.0, and pin the tagged ecosystem versions:
btcwallet v0.17.0, neutrino v0.18.0 and lightning-onion v1.4.0.

The bulk of the import rewrite was produced by the scripted diff from
https://github.com/btcsuite/btcd/pull/2547 (followed by 'make rpc'). The
address symbols that moved out of btcutil into the new address package
are imported as btcaddr where a local "address" variable would otherwise
shadow them. The go.mod/go.sum updates and the remaining manual
compilation fixes are folded into this single commit so it builds on its
own (the migration was previously split into a reproducible scripted-diff
plus follow-ups, intended to be squashed on merge).
2026-06-24 10:58:36 -07:00

82 lines
2 KiB
Go

//go:build test_db_postgres && !test_db_sqlite
package graphdb
import (
"database/sql"
"testing"
"github.com/btcsuite/btcd/chaincfg/v2"
"github.com/lightningnetwork/lnd/sqldb"
"github.com/stretchr/testify/require"
)
// isSQLDB indicates that this build uses a SQL database.
var isSQLDB = true
// NewTestDB is a helper function that creates a SQLStore backed by a SQL
// database for testing.
func NewTestDB(t testing.TB) Store {
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) Store {
var querier BatchedSQLQueries
if pgFixture == nil {
querier = newBatchQuerier(t)
} else {
querier = newBatchQuerierWithFixture(t, pgFixture)
}
store, err := NewSQLStore(
&SQLStoreConfig{
ChainHash: *chaincfg.MainNetParams.GenesisHash,
QueryCfg: sqldb.DefaultPostgresConfig(),
}, 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 {
pgFixture := sqldb.NewTestPgFixture(
t, sqldb.DefaultPostgresFixtureLifetime,
)
t.Cleanup(func() {
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(
db, func(tx *sql.Tx) SQLQueries {
return db.WithTx(tx)
},
)
}