Skip to content

Commit 0a63d81

Browse files
author
Robyn Ffrancon
authored
Merge pull request #735 from ffranr/specify-sqlite-db-file-path
Add means to specify sqlite db file path for tapd test harness and db unit tests
2 parents 7c0688e + 474183f commit 0a63d81

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed

itest/tapd_harness.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ type harnessOpts struct {
104104
// fedSyncTickerInterval is the interval at which the federation envoy
105105
// sync ticker will fire.
106106
fedSyncTickerInterval *time.Duration
107+
108+
// sqliteDatabaseFilePath is the path to the SQLite database file to
109+
// use.
110+
sqliteDatabaseFilePath *string
107111
}
108112

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

194+
// Set the SQLite database file path if it was specified.
195+
if opts.sqliteDatabaseFilePath != nil {
196+
tapCfg.Sqlite.DatabaseFileName = *opts.sqliteDatabaseFilePath
197+
}
198+
190199
// Pass through the address asset syncer disable flag. If the option
191200
// was not set, this will be false, which is the default.
192201
tapCfg.AddrBook.DisableSyncer = opts.addrAssetSyncerDisable

itest/test_harness.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ type tapdHarnessParams struct {
373373
// noDefaultUniverseSync indicates whether the default universe server
374374
// should be added as a federation server or not.
375375
noDefaultUniverseSync bool
376+
377+
// sqliteDatabaseFilePath is the path to the SQLite database file to
378+
// use.
379+
sqliteDatabaseFilePath *string
376380
}
377381

378382
type Option func(*tapdHarnessParams)
@@ -407,12 +411,14 @@ func setupTapdHarness(t *testing.T, ht *harnessTest,
407411
ho.custodianProofRetrievalDelay = params.custodianProofRetrievalDelay
408412
ho.addrAssetSyncerDisable = params.addrAssetSyncerDisable
409413
ho.fedSyncTickerInterval = params.fedSyncTickerInterval
414+
ho.sqliteDatabaseFilePath = params.sqliteDatabaseFilePath
410415
}
411416

412-
tapdHarness, err := newTapdHarness(t, ht, tapdConfig{
417+
tapdCfg := tapdConfig{
413418
NetParams: harnessNetParams,
414419
LndNode: node,
415-
}, harnessOpts)
420+
}
421+
tapdHarness, err := newTapdHarness(t, ht, tapdCfg, harnessOpts)
416422
require.NoError(t, err)
417423

418424
// Start the tapd harness now.

tapdb/sqlite.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,21 @@ func (s *SqliteStore) ExecuteMigrations(target MigrationTarget) error {
170170
func NewTestSqliteDB(t *testing.T) *SqliteStore {
171171
t.Helper()
172172

173-
dbFileName := filepath.Join(t.TempDir(), "tmp.db")
174-
t.Logf("Creating new SQLite DB for testing: %s", dbFileName)
175-
176173
// TODO(roasbeef): if we pass :memory: for the file name, then we get
177174
// an in mem version to speed up tests
175+
dbPath := filepath.Join(t.TempDir(), "tmp.db")
176+
t.Logf("Creating new SQLite DB handle for testing: %s", dbPath)
177+
178+
return NewTestSqliteDbHandleFromPath(t, dbPath)
179+
}
180+
181+
// NewTestSqliteDbHandleFromPath is a helper function that creates a SQLite
182+
// database handle given a database file path.
183+
func NewTestSqliteDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore {
184+
t.Helper()
185+
178186
sqlDB, err := NewSqliteStore(&SqliteConfig{
179-
DatabaseFileName: dbFileName,
187+
DatabaseFileName: dbPath,
180188
SkipMigrations: false,
181189
})
182190
require.NoError(t, err)

tapdb/sqlutils_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,8 @@ func (d *DbHandler) AddRandomServerAddrs(t *testing.T,
221221
return addrs
222222
}
223223

224-
// NewDbHandle creates a new store and query handle to the test database.
225-
func NewDbHandle(t *testing.T) *DbHandler {
226-
// Create a new test database.
227-
db := NewTestDB(t)
228-
224+
// newDbHandleFromDb creates a new database store handle given a database store.
225+
func newDbHandleFromDb(db *BaseDB) *DbHandler {
229226
testClock := clock.NewTestClock(time.Now())
230227

231228
// Gain a handle to the pending (minting) universe federation store.
@@ -268,3 +265,17 @@ func NewDbHandle(t *testing.T) *DbHandler {
268265
DirectQuery: db,
269266
}
270267
}
268+
269+
// NewDbHandleFromPath creates a new database store handle given a database file
270+
// path.
271+
func NewDbHandleFromPath(t *testing.T, dbPath string) *DbHandler {
272+
db := NewTestDbHandleFromPath(t, dbPath)
273+
return newDbHandleFromDb(db.BaseDB)
274+
}
275+
276+
// NewDbHandle creates a new database store handle.
277+
func NewDbHandle(t *testing.T) *DbHandler {
278+
// Create a new test database with the default database file path.
279+
db := NewTestDB(t)
280+
return newDbHandleFromDb(db.BaseDB)
281+
}

tapdb/test_postgres.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ func NewTestDB(t *testing.T) *PostgresStore {
1111
return NewTestPostgresDB(t)
1212
}
1313

14+
// NewTestDbHandleFromPath is a helper function that creates a new handle to an
15+
// existing SQLite database for testing.
16+
func NewTestDbHandleFromPath(t *testing.T, dbPath string) *PostgresStore {
17+
return NewTestPostgresDB(t)
18+
}
19+
1420
// NewTestDBWithVersion is a helper function that creates a Postgres database
1521
// for testing and migrates it to the given version.
1622
func NewTestDBWithVersion(t *testing.T, version uint) *PostgresStore {

tapdb/test_sqlite.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ func NewTestDB(t *testing.T) *SqliteStore {
1111
return NewTestSqliteDB(t)
1212
}
1313

14+
// NewTestDbHandleFromPath is a helper function that creates a new handle to an
15+
// existing SQLite database for testing.
16+
func NewTestDbHandleFromPath(t *testing.T, dbPath string) *SqliteStore {
17+
return NewTestSqliteDbHandleFromPath(t, dbPath)
18+
}
19+
1420
// NewTestDBWithVersion is a helper function that creates an SQLite database for
1521
// testing and migrates it to the given version.
1622
func NewTestDBWithVersion(t *testing.T, version uint) *SqliteStore {

0 commit comments

Comments
 (0)