accounts: ensure that test SQL store is closed

We add a helper function to the functions that creates the test SQL
stores, in order to ensure that the store is properly closed when the
test is cleaned up.
This commit is contained in:
Viktor Tigerström 2025-06-09 12:13:09 +02:00
parent 8986d41616
commit 12343cf4ba
No known key found for this signature in database
GPG key ID: B984570980684DCC
4 changed files with 27 additions and 8 deletions

View file

@ -39,9 +39,6 @@ func TestAccountStoreMigration(t *testing.T) {
*db.TransactionExecutor[SQLQueries]) {
testDBStore := NewTestDB(t, clock)
t.Cleanup(func() {
require.NoError(t, testDBStore.Close())
})
store, ok := testDBStore.(*SQLStore)
require.True(t, ok)

View file

@ -16,7 +16,7 @@ var ErrDBClosed = errors.New("database is closed")
// NewTestDB is a helper function that creates an SQLStore database for testing.
func NewTestDB(t *testing.T, clock clock.Clock) Store {
return NewSQLStore(db.NewTestPostgresDB(t).BaseDB, clock)
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
}
// NewTestDBFromPath is a helper function that creates a new SQLStore with a
@ -24,5 +24,5 @@ func NewTestDB(t *testing.T, clock clock.Clock) Store {
func NewTestDBFromPath(t *testing.T, dbPath string,
clock clock.Clock) Store {
return NewSQLStore(db.NewTestPostgresDB(t).BaseDB, clock)
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
}

22
accounts/test_sql.go Normal file
View file

@ -0,0 +1,22 @@
//go:build test_db_postgres || test_db_sqlite
package accounts
import (
"testing"
"github.com/lightninglabs/lightning-terminal/db"
"github.com/lightningnetwork/lnd/clock"
"github.com/stretchr/testify/require"
)
// createStore is a helper function that creates a new SQLStore and ensure that
// it is closed when during the test cleanup.
func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLStore {
store := NewSQLStore(sqlDB, clock)
t.Cleanup(func() {
require.NoError(t, store.Close())
})
return store
}

View file

@ -16,7 +16,7 @@ var ErrDBClosed = errors.New("database is closed")
// NewTestDB is a helper function that creates an SQLStore database for testing.
func NewTestDB(t *testing.T, clock clock.Clock) Store {
return NewSQLStore(db.NewTestSqliteDB(t).BaseDB, clock)
return createStore(t, db.NewTestSqliteDB(t).BaseDB, clock)
}
// NewTestDBFromPath is a helper function that creates a new SQLStore with a
@ -24,7 +24,7 @@ func NewTestDB(t *testing.T, clock clock.Clock) Store {
func NewTestDBFromPath(t *testing.T, dbPath string,
clock clock.Clock) Store {
return NewSQLStore(
db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
return createStore(
t, db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
)
}