paymentsdb: add harness to run payment db agnostic tests
What changed, and why it matters
This commit only adds test helper code for the payments database. It introduces build-tag-guarded test files that let the same payment database tests run against either the existing key-value backend, a SQLite backend, or a PostgreSQL backend. There is no change to production code, user-facing behavior, or security-sensitive logic.
No security action needed. Treat as normal test-infrastructure change during code review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds three test-only files under payments/db: test_kvdb.go, test_postgres.go, and test_sqlite.go. They provide NewTestDB/NewTestDBWithFixture helpers and BatchedSQLQueries constructors gated by go:build tags (test_db_sqlite, test_db_postgres). The existing kvdb test file now requires !test_db_sqlite && !test_db_postgres. No runtime, protocol, or cryptographic code is modified.
Changed components
payments/db/test_kvdb.gopayments/db/test_postgres.gopayments/db/test_sqlite.goInspect captured patch +135 / −0
diff --git a/payments/db/test_kvdb.go b/payments/db/test_kvdb.go
index e0ee173..a4bbfcc 100644
--- a/payments/db/test_kvdb.go
+++ b/payments/db/test_kvdb.go
@@ -1,3 +1,5 @@
+//go:build !test_db_sqlite && !test_db_postgres
+
package paymentsdb
import (
diff --git a/payments/db/test_postgres.go b/payments/db/test_postgres.go
new file mode 100644
index 0000000..b4f00f9
--- /dev/null
+++ b/payments/db/test_postgres.go
@@ -0,0 +1,77 @@
+//go:build test_db_postgres && !test_db_sqlite
+
+package paymentsdb
+
+import (
+ "database/sql"
+ "testing"
+
+ "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, opts ...OptionModifier) DB {
+ return NewTestDBWithFixture(t, nil, opts...)
+}
+
+// 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, opts ...OptionModifier) DB {
+
+ var querier BatchedSQLQueries
+ if pgFixture == nil {
+ querier = newBatchQuerier(t)
+ } else {
+ querier = newBatchQuerierWithFixture(t, pgFixture)
+ }
+
+ store, err := NewSQLStore(
+ &SQLStoreConfig{
+ QueryCfg: sqldb.DefaultPostgresConfig(),
+ }, querier, opts...,
+ )
+ 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)
+ },
+ )
+}
diff --git a/payments/db/test_sqlite.go b/payments/db/test_sqlite.go
new file mode 100644
index 0000000..8664db4
--- /dev/null
+++ b/payments/db/test_sqlite.go
@@ -0,0 +1,56 @@
+//go:build !test_db_postgres && test_db_sqlite
+
+package paymentsdb
+
+import (
+ "database/sql"
+ "testing"
+
+ "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, opts ...OptionModifier) DB {
+ return NewTestDBWithFixture(t, nil, opts...)
+}
+
+// NewTestDBFixture is a no-op for the sqlite build.
+func NewTestDBFixture(_ *testing.T) *sqldb.TestPgFixture {
+ return nil
+}
+
+// NewTestDBWithFixture is a helper function that creates a SQLStore backed by a
+// SQL database for testing.
+func NewTestDBWithFixture(t testing.TB, _ *sqldb.TestPgFixture,
+ opts ...OptionModifier) DB {
+
+ store, err := NewSQLStore(
+ &SQLStoreConfig{
+ QueryCfg: sqldb.DefaultSQLiteConfig(),
+ }, newBatchQuerier(t), opts...,
+ )
+ require.NoError(t, err)
+ return store
+}
+
+// newBatchQuerier creates a new BatchedSQLQueries instance for testing
+// using a SQLite database.
+func newBatchQuerier(t testing.TB) BatchedSQLQueries {
+ return newBatchQuerierWithFixture(t, nil)
+}
+
+// newBatchQuerierWithFixture creates a new BatchedSQLQueries instance for
+// testing using a SQLite database.
+func newBatchQuerierWithFixture(t testing.TB,
+ _ *sqldb.TestPgFixture) BatchedSQLQueries {
+
+ db := sqldb.NewTestSqliteDB(t).BaseDB
+
+ return sqldb.NewTransactionExecutor(
+ db, func(tx *sql.Tx) SQLQueries {
+ return db.WithTx(tx)
+ },
+ )
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.