Skip to content

Commit 078cdb6

Browse files
committed
tapdb: add unit test multi db store handler
This structure will allow us to pass around the same db store handler to different helper functions which will aid in setting up a unit test db.
1 parent 9fbf274 commit 078cdb6

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

tapdb/sqlutils_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package tapdb
2+
3+
import (
4+
"database/sql"
5+
"testing"
6+
"time"
7+
8+
"github.com/lightninglabs/taproot-assets/tapdb/sqlc"
9+
"github.com/lightningnetwork/lnd/clock"
10+
)
11+
12+
// DbHandler is a helper struct that contains all the database stores.
13+
type DbHandler struct {
14+
// UniverseFederationStore is a handle to the universe federation store.
15+
UniverseFederationStore *UniverseFederationDB
16+
17+
// MultiverseStore is a handle to the multiverse store.
18+
MultiverseStore *MultiverseStore
19+
20+
// AssetMintingStore is a handle to the pending (minting) assets store.
21+
AssetMintingStore *AssetMintingStore
22+
23+
// AssetStore is a handle to the active assets store.
24+
AssetStore *AssetStore
25+
26+
// DirectQuery is a handle to the underlying database that can be used
27+
// to query the database directly.
28+
DirectQuery sqlc.Querier
29+
}
30+
31+
// NewDbHandle creates a new store and query handle to the test database.
32+
func NewDbHandle(t *testing.T) *DbHandler {
33+
// Create a new test database.
34+
db := NewTestDB(t)
35+
36+
testClock := clock.NewTestClock(time.Now())
37+
38+
// Gain a handle to the pending (minting) universe federation store.
39+
universeServerTxCreator := NewTransactionExecutor(
40+
db, func(tx *sql.Tx) UniverseServerStore {
41+
return db.WithTx(tx)
42+
},
43+
)
44+
fedStore := NewUniverseFederationDB(universeServerTxCreator, testClock)
45+
46+
// Gain a handle to the multiverse store.
47+
multiverseTxCreator := NewTransactionExecutor(db,
48+
func(tx *sql.Tx) BaseMultiverseStore {
49+
return db.WithTx(tx)
50+
},
51+
)
52+
multiverseStore := NewMultiverseStore(multiverseTxCreator)
53+
54+
// Gain a handle to the pending (minting) assets store.
55+
assetMintingDB := NewTransactionExecutor(
56+
db, func(tx *sql.Tx) PendingAssetStore {
57+
return db.WithTx(tx)
58+
},
59+
)
60+
assetMintingStore := NewAssetMintingStore(assetMintingDB)
61+
62+
// Gain a handle to the active assets store.
63+
assetsDB := NewTransactionExecutor(
64+
db, func(tx *sql.Tx) ActiveAssetsStore {
65+
return db.WithTx(tx)
66+
},
67+
)
68+
activeAssetsStore := NewAssetStore(assetsDB, testClock)
69+
70+
return &DbHandler{
71+
UniverseFederationStore: fedStore,
72+
MultiverseStore: multiverseStore,
73+
AssetMintingStore: assetMintingStore,
74+
AssetStore: activeAssetsStore,
75+
DirectQuery: db,
76+
}
77+
}

0 commit comments

Comments
 (0)