mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-14 12:43:32 +02:00
In the upcoming migration of the firewall database to SQL, the helper functions that creates the test databases of different types, need to return a unified interface in order to not have to control the migration tests file by build tags. Therefore, we update the `NewTestDB` functions to return the `FirewallDBs` interface instead of the specific store implementation type.
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
//go:build test_db_postgres || test_db_sqlite
|
|
|
|
package firewalldb
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/lightninglabs/lightning-terminal/accounts"
|
|
"github.com/lightninglabs/lightning-terminal/db"
|
|
"github.com/lightninglabs/lightning-terminal/session"
|
|
"github.com/lightningnetwork/lnd/clock"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// NewTestDBWithSessions creates a new test SQLDB Store with access to an
|
|
// existing sessions DB.
|
|
func NewTestDBWithSessions(t *testing.T, sessionStore session.Store,
|
|
clock clock.Clock) FirewallDBs {
|
|
sessions, ok := sessionStore.(*session.SQLStore)
|
|
require.True(t, ok)
|
|
|
|
return createStore(t, sessions.BaseDB, clock)
|
|
}
|
|
|
|
// NewTestDBWithSessionsAndAccounts creates a new test SQLDB Store with access
|
|
// to an existing sessions DB and accounts DB.
|
|
func NewTestDBWithSessionsAndAccounts(t *testing.T, sessionStore SessionDB,
|
|
acctStore AccountsDB, clock clock.Clock) FirewallDBs {
|
|
|
|
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)
|
|
|
|
return createStore(t, sessions.BaseDB, clock)
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// createStore is a helper function that creates a new SQLDB and ensure that
|
|
// it is closed when during the test cleanup.
|
|
func createStore(t *testing.T, sqlDB *db.BaseDB, clock clock.Clock) *SQLDB {
|
|
store := NewSQLDB(sqlDB, clock)
|
|
t.Cleanup(func() {
|
|
require.NoError(t, store.Close())
|
|
})
|
|
|
|
return store
|
|
}
|