mirror of
https://github.com/lightningnetwork/lnd.git
synced 2026-08-13 12:32:48 +02:00
sqldb: add validate-network unit tests
This commit is contained in:
parent
5eebbb0e69
commit
d988682e97
3 changed files with 122 additions and 0 deletions
87
chainparams/store_test.go
Normal file
87
chainparams/store_test.go
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
//go:build test_db_postgres || test_db_sqlite
|
||||
|
||||
package chainparams
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestValidateNetworkMismatch verifies that ValidateNetwork persists the first
|
||||
// network and fails when a different network is used later.
|
||||
func TestValidateNetworkMismatch(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := NewStore(newTestDB(t))
|
||||
|
||||
// First call: persists regtest into the store.
|
||||
err := store.ValidateNetwork(t.Context(), &chaincfg.RegressionNetParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Second call: a different network — must fail with ErrNetworkMismatch.
|
||||
err = store.ValidateNetwork(t.Context(), &chaincfg.SimNetParams)
|
||||
require.ErrorIs(t, err, ErrNetworkMismatch)
|
||||
}
|
||||
|
||||
// TestValidateNetworkSameNetwork verifies that ValidateNetwork succeeds when
|
||||
// called repeatedly with the same network (idempotent-match path).
|
||||
func TestValidateNetworkSameNetwork(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := NewStore(newTestDB(t))
|
||||
|
||||
// First call: persists the network.
|
||||
err := store.ValidateNetwork(t.Context(), &chaincfg.RegressionNetParams)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Second call: same network again — reads the stored value and must
|
||||
// succeed (idempotent match).
|
||||
err = store.ValidateNetwork(t.Context(), &chaincfg.RegressionNetParams)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// TestValidateNetworkNormalizesTestnet verifies that network aliases collapse
|
||||
// to the same persisted value.
|
||||
func TestValidateNetworkNormalizesTestnet(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := NewStore(newTestDB(t))
|
||||
|
||||
// First call: persists canonical testnet3 parameters.
|
||||
err := store.ValidateNetwork(t.Context(), &chaincfg.TestNet3Params)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Second call: same logical network, different Name field — still
|
||||
// matches after normalization (not ErrNetworkMismatch).
|
||||
testnetAlias := chaincfg.TestNet3Params
|
||||
testnetAlias.Name = "testnet"
|
||||
|
||||
err = store.ValidateNetwork(t.Context(), &testnetAlias)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// TestValidateNetworkRejectsEmptyName verifies that malformed network params
|
||||
// are rejected before touching the database.
|
||||
func TestValidateNetworkRejectsEmptyName(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := NewStore(newTestDB(t))
|
||||
|
||||
// Empty Params.Name — rejected before any database read or write.
|
||||
err := store.ValidateNetwork(t.Context(), &chaincfg.Params{})
|
||||
require.ErrorContains(t, err, "must define a network")
|
||||
}
|
||||
|
||||
// TestValidateNetworkRejectsNilParams verifies that callers provide network
|
||||
// parameters.
|
||||
func TestValidateNetworkRejectsNilParams(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
store := NewStore(newTestDB(t))
|
||||
|
||||
// Nil params — rejected before any database read or write.
|
||||
err := store.ValidateNetwork(t.Context(), nil)
|
||||
require.ErrorContains(t, err, "must not be nil")
|
||||
}
|
||||
21
chainparams/test_postgres.go
Normal file
21
chainparams/test_postgres.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
//go:build test_db_postgres && !test_db_sqlite
|
||||
|
||||
package chainparams
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/lightningnetwork/lnd/sqldb"
|
||||
)
|
||||
|
||||
// newTestDB creates a Postgres-backed BaseDB for use in unit tests.
|
||||
func newTestDB(t testing.TB) *sqldb.BaseDB {
|
||||
pgFixture := sqldb.NewTestPgFixture(
|
||||
t, sqldb.DefaultPostgresFixtureLifetime,
|
||||
)
|
||||
t.Cleanup(func() {
|
||||
pgFixture.TearDown(t)
|
||||
})
|
||||
|
||||
return sqldb.NewTestPostgresDB(t, pgFixture).GetBaseDB()
|
||||
}
|
||||
14
chainparams/test_sqlite.go
Normal file
14
chainparams/test_sqlite.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
//go:build !test_db_postgres && test_db_sqlite
|
||||
|
||||
package chainparams
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/lightningnetwork/lnd/sqldb"
|
||||
)
|
||||
|
||||
// newTestDB creates a SQLite-backed BaseDB for use in unit tests.
|
||||
func newTestDB(t testing.TB) *sqldb.BaseDB {
|
||||
return sqldb.NewTestSqliteDB(t).GetBaseDB()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue