Skip to content

Commit 3c42778

Browse files
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.
1 parent cad67fc commit 3c42778

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

accounts/sql_migration_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ func TestAccountStoreMigration(t *testing.T) {
3939
*db.TransactionExecutor[SQLQueries]) {
4040

4141
testDBStore := NewTestDB(t, clock)
42-
t.Cleanup(func() {
43-
require.NoError(t, testDBStore.Close())
44-
})
4542

4643
store, ok := testDBStore.(*SQLStore)
4744
require.True(t, ok)

accounts/test_postgres.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ var ErrDBClosed = errors.New("database is closed")
1616

1717
// NewTestDB is a helper function that creates an SQLStore database for testing.
1818
func NewTestDB(t *testing.T, clock clock.Clock) Store {
19-
return NewSQLStore(db.NewTestPostgresDB(t).BaseDB, clock)
19+
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
2020
}
2121

2222
// NewTestDBFromPath is a helper function that creates a new SQLStore with a
2323
// connection to an existing postgres database for testing.
2424
func NewTestDBFromPath(t *testing.T, dbPath string,
2525
clock clock.Clock) Store {
2626

27-
return NewSQLStore(db.NewTestPostgresDB(t).BaseDB, clock)
27+
return createStore(t, db.NewTestPostgresDB(t).BaseDB, clock)
2828
}

accounts/test_sql.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build test_db_postgres || test_db_sqlite
2+
3+
package accounts
4+
5+
import (
6+
"testing"
7+
8+
"github.com/lightninglabs/lightning-terminal/db"
9+
"github.com/lightningnetwork/lnd/clock"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
// createStore is a helper function that creates a new SQLStore and ensure that
14+
// it is closed when during the test cleanup.
15+
func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLStore {
16+
store := NewSQLStore(sqlDB, clock)
17+
t.Cleanup(func() {
18+
require.NoError(t, store.Close())
19+
})
20+
21+
return store
22+
}

accounts/test_sqlite.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ var ErrDBClosed = errors.New("database is closed")
1616

1717
// NewTestDB is a helper function that creates an SQLStore database for testing.
1818
func NewTestDB(t *testing.T, clock clock.Clock) Store {
19-
return NewSQLStore(db.NewTestSqliteDB(t).BaseDB, clock)
19+
return createStore(t, db.NewTestSqliteDB(t).BaseDB, clock)
2020
}
2121

2222
// NewTestDBFromPath is a helper function that creates a new SQLStore with a
2323
// connection to an existing SQL database for testing.
2424
func NewTestDBFromPath(t *testing.T, dbPath string,
2525
clock clock.Clock) Store {
2626

27-
return NewSQLStore(
28-
db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
27+
return createStore(
28+
t, db.NewTestSqliteDbHandleFromPath(t, dbPath).BaseDB, clock,
2929
)
3030
}

0 commit comments

Comments
 (0)