Skip to content

Commit

Permalink
Merge pull request lightninglabs#735 from ffranr/specify-sqlite-db-fi…
Browse files Browse the repository at this point in the history
…le-path

Add means to specify sqlite db file path for tapd test harness and db unit tests
  • Loading branch information
ffranr authored Dec 14, 2023
2 parents 7c0688e + 474183f commit 0a63d81
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
9 changes: 9 additions & 0 deletions itest/tapd_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ type harnessOpts struct {
// fedSyncTickerInterval is the interval at which the federation envoy
// sync ticker will fire.
fedSyncTickerInterval *time.Duration

// sqliteDatabaseFilePath is the path to the SQLite database file to
// use.
sqliteDatabaseFilePath *string
}

type harnessOption func(*harnessOpts)
Expand Down Expand Up @@ -187,6 +191,11 @@ func newTapdHarness(t *testing.T, ht *harnessTest, cfg tapdConfig,
// as well as RPC insert and query.
tapCfg.Universe.PublicAccess = true

// Set the SQLite database file path if it was specified.
if opts.sqliteDatabaseFilePath != nil {
tapCfg.Sqlite.DatabaseFileName = *opts.sqliteDatabaseFilePath
}

// Pass through the address asset syncer disable flag. If the option
// was not set, this will be false, which is the default.
tapCfg.AddrBook.DisableSyncer = opts.addrAssetSyncerDisable
Expand Down
10 changes: 8 additions & 2 deletions itest/test_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ type tapdHarnessParams struct {
// noDefaultUniverseSync indicates whether the default universe server
// should be added as a federation server or not.
noDefaultUniverseSync bool

// sqliteDatabaseFilePath is the path to the SQLite database file to
// use.
sqliteDatabaseFilePath *string
}

type Option func(*tapdHarnessParams)
Expand Down Expand Up @@ -407,12 +411,14 @@ func setupTapdHarness(t *testing.T, ht *harnessTest,
ho.custodianProofRetrievalDelay = params.custodianProofRetrievalDelay
ho.addrAssetSyncerDisable = params.addrAssetSyncerDisable
ho.fedSyncTickerInterval = params.fedSyncTickerInterval
ho.sqliteDatabaseFilePath = params.sqliteDatabaseFilePath
}

tapdHarness, err := newTapdHarness(t, ht, tapdConfig{
tapdCfg := tapdConfig{
NetParams: harnessNetParams,
LndNode: node,
}, harnessOpts)
}
tapdHarness, err := newTapdHarness(t, ht, tapdCfg, harnessOpts)
require.NoError(t, err)

// Start the tapd harness now.
Expand Down
16 changes: 12 additions & 4 deletions tapdb/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,21 @@ func (s *SqliteStore) ExecuteMigrations(target MigrationTarget) error {
func NewTestSqliteDB(t *testing.T) *SqliteStore {
t.Helper()

dbFileName := filepath.Join(t.TempDir(), "tmp.db")
t.Logf("Creating new SQLite DB for testing: %s", dbFileName)

// TODO(roasbeef): if we pass :memory: for the file name, then we get
// an in mem version to speed up tests
dbPath := filepath.Join(t.TempDir(), "tmp.db")
t.Logf("Creating new SQLite DB handle for testing: %s", dbPath)

return NewTestSqliteDbHandleFromPath(t, dbPath)
}

// NewTestSqliteDbHandleFromPath is a helper function that creates a SQLite
// database handle given a database file path.
func NewTestSqliteDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore {
t.Helper()

sqlDB, err := NewSqliteStore(&SqliteConfig{
DatabaseFileName: dbFileName,
DatabaseFileName: dbPath,
SkipMigrations: false,
})
require.NoError(t, err)
Expand Down
21 changes: 16 additions & 5 deletions tapdb/sqlutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,8 @@ func (d *DbHandler) AddRandomServerAddrs(t *testing.T,
return addrs
}

// NewDbHandle creates a new store and query handle to the test database.
func NewDbHandle(t *testing.T) *DbHandler {
// Create a new test database.
db := NewTestDB(t)

// newDbHandleFromDb creates a new database store handle given a database store.
func newDbHandleFromDb(db *BaseDB) *DbHandler {
testClock := clock.NewTestClock(time.Now())

// Gain a handle to the pending (minting) universe federation store.
Expand Down Expand Up @@ -268,3 +265,17 @@ func NewDbHandle(t *testing.T) *DbHandler {
DirectQuery: db,
}
}

// NewDbHandleFromPath creates a new database store handle given a database file
// path.
func NewDbHandleFromPath(t *testing.T, dbPath string) *DbHandler {
db := NewTestDbHandleFromPath(t, dbPath)
return newDbHandleFromDb(db.BaseDB)
}

// NewDbHandle creates a new database store handle.
func NewDbHandle(t *testing.T) *DbHandler {
// Create a new test database with the default database file path.
db := NewTestDB(t)
return newDbHandleFromDb(db.BaseDB)
}
6 changes: 6 additions & 0 deletions tapdb/test_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ func NewTestDB(t *testing.T) *PostgresStore {
return NewTestPostgresDB(t)
}

// NewTestDbHandleFromPath is a helper function that creates a new handle to an
// existing SQLite database for testing.
func NewTestDbHandleFromPath(t *testing.T, dbPath string) *PostgresStore {
return NewTestPostgresDB(t)
}

// NewTestDBWithVersion is a helper function that creates a Postgres database
// for testing and migrates it to the given version.
func NewTestDBWithVersion(t *testing.T, version uint) *PostgresStore {
Expand Down
6 changes: 6 additions & 0 deletions tapdb/test_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ func NewTestDB(t *testing.T) *SqliteStore {
return NewTestSqliteDB(t)
}

// NewTestDbHandleFromPath is a helper function that creates a new handle to an
// existing SQLite database for testing.
func NewTestDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore {
return NewTestSqliteDbHandleFromPath(t, dbPath)
}

// NewTestDBWithVersion is a helper function that creates an SQLite database for
// testing and migrates it to the given version.
func NewTestDBWithVersion(t *testing.T, version uint) *SqliteStore {
Expand Down

0 comments on commit 0a63d81

Please sign in to comment.