2025-04-11 13:23:07 +02:00
|
|
|
//go:build test_db_postgres || test_db_sqlite
|
|
|
|
|
|
|
|
|
|
package firewalldb
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
2025-05-13 14:29:24 +02:00
|
|
|
"time"
|
2025-04-11 13:23:07 +02:00
|
|
|
|
2026-01-22 11:53:18 +01:00
|
|
|
"github.com/lightninglabs/lightning-terminal/db/sqlc"
|
|
|
|
|
|
2025-05-13 14:29:24 +02:00
|
|
|
"github.com/lightninglabs/lightning-terminal/accounts"
|
2025-04-11 13:23:07 +02:00
|
|
|
"github.com/lightninglabs/lightning-terminal/session"
|
2025-05-12 13:37:53 +02:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
2026-01-22 11:53:18 +01:00
|
|
|
"github.com/lightningnetwork/lnd/sqldb/v2"
|
2025-04-11 13:23:07 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// NewTestDBWithSessions creates a new test SQLDB Store with access to an
|
|
|
|
|
// existing sessions DB.
|
2025-05-12 13:37:53 +02:00
|
|
|
func NewTestDBWithSessions(t *testing.T, sessionStore session.Store,
|
2025-05-19 13:58:38 +02:00
|
|
|
clock clock.Clock) FirewallDBs {
|
2025-04-11 13:23:07 +02:00
|
|
|
sessions, ok := sessionStore.(*session.SQLStore)
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
2025-06-09 11:31:19 +02:00
|
|
|
return createStore(t, sessions.BaseDB, clock)
|
2025-04-11 13:23:07 +02:00
|
|
|
}
|
2025-05-13 14:29:24 +02:00
|
|
|
|
|
|
|
|
// NewTestDBWithSessionsAndAccounts creates a new test SQLDB Store with access
|
|
|
|
|
// to an existing sessions DB and accounts DB.
|
|
|
|
|
func NewTestDBWithSessionsAndAccounts(t *testing.T, sessionStore SessionDB,
|
2025-05-19 13:58:38 +02:00
|
|
|
acctStore AccountsDB, clock clock.Clock) FirewallDBs {
|
2025-05-13 14:29:24 +02:00
|
|
|
|
|
|
|
|
sessions, ok := sessionStore.(*session.SQLStore)
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
|
|
accounts, ok := acctStore.(*accounts.SQLStore)
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, accounts.BaseDB, sessions.BaseDB)
|
|
|
|
|
|
2025-06-09 11:31:19 +02:00
|
|
|
return createStore(t, sessions.BaseDB, clock)
|
2025-05-13 14:29:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertEqualActions(t *testing.T, expected, got *Action) {
|
|
|
|
|
expectedAttemptedAt := expected.AttemptedAt
|
|
|
|
|
actualAttemptedAt := got.AttemptedAt
|
|
|
|
|
|
|
|
|
|
expected.AttemptedAt = time.Time{}
|
|
|
|
|
got.AttemptedAt = time.Time{}
|
|
|
|
|
|
|
|
|
|
require.Equal(t, expected, got)
|
|
|
|
|
require.Equal(t, expectedAttemptedAt.Unix(), actualAttemptedAt.Unix())
|
|
|
|
|
|
|
|
|
|
expected.AttemptedAt = expectedAttemptedAt
|
|
|
|
|
got.AttemptedAt = actualAttemptedAt
|
|
|
|
|
}
|
2025-06-09 11:31:19 +02:00
|
|
|
|
|
|
|
|
// createStore is a helper function that creates a new SQLDB and ensure that
|
|
|
|
|
// it is closed when during the test cleanup.
|
2026-01-22 11:53:18 +01:00
|
|
|
func createStore(t *testing.T, sqlDB *sqldb.BaseDB, clock clock.Clock) *SQLDB {
|
|
|
|
|
queries := sqlc.NewForType(sqlDB, sqlDB.BackendType)
|
|
|
|
|
|
|
|
|
|
store := NewSQLDB(sqlDB, queries, clock)
|
2025-06-09 11:31:19 +02:00
|
|
|
t.Cleanup(func() {
|
|
|
|
|
require.NoError(t, store.Close())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return store
|
|
|
|
|
}
|