Skip to content

Commit

Permalink
wallet: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEk committed Oct 20, 2023
1 parent a1dc2d1 commit 5c3b5d8
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/btcsuite/btcwallet/walletdb"
"github.com/btcsuite/btcwallet/wtxmgr"
"github.com/stretchr/testify/require"

"github.com/btcsuite/btcd/btcutil"
)
Expand Down Expand Up @@ -205,3 +206,89 @@ func TestLabelTransaction(t *testing.T) {
})
}
}

// TestGetTransaction tests if we can fetch a mined, an existing
// and a non-existing transaction from the wallet like we expect.
func TestGetTransaction(t *testing.T) {

tests := []struct {
name string

// Whether the transaction should be known to the wallet.
txKnown bool

// Whether the transaction is mined.
minedTx bool

// Block meta. Used to emulate mined transactions.
blockMeta *wtxmgr.BlockMeta

// The error we expect to be returned.
expectedErr error
}{
{
name: "existing transaction, not mined",
txKnown: true,
blockMeta: nil,
expectedErr: nil,
},
{
name: "non-existing transaction",
txKnown: false,
blockMeta: nil,
expectedErr: ErrNoTx,
},
{
name: "existing confirmed transaction",
txKnown: true,
blockMeta: &wtxmgr.BlockMeta{
Block: wtxmgr.Block{
Height: 100,
},
},
expectedErr: nil,
},
}
for _, test := range tests {
test := test

t.Run(test.name, func(t *testing.T) {
w, cleanup := testWallet(t)
defer cleanup()

// If the transaction should be known to the store, we
// write txdetail to disk.
if test.txKnown {
rec, err := wtxmgr.NewTxRecord(
TstSerializedTx, time.Now(),
)
require.NoError(t, err)

// Write the tx record to disk.
err = walletdb.Update(w.db,
func(tx walletdb.ReadWriteTx) error {

ns := tx.ReadWriteBucket(
wtxmgrNamespaceKey,
)

return w.TxStore.InsertTx(ns, rec, test.blockMeta)

})
require.NoError(t, err, "could not insert tx")
}

tx, err := w.GetTransaction(*TstTxHash)
if err != test.expectedErr {
t.Fatalf("expected: %v, got: %v", test.expectedErr, err)
}
if test.txKnown {
require.True(t, tx.Summary.Hash.IsEqual(TstTxHash))
}
// Test if the transaction is mined.
if test.blockMeta != nil {
require.Equal(t, test.blockMeta.Height, tx.Height)
}
})
}
}

0 comments on commit 5c3b5d8

Please sign in to comment.