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 18, 2023
1 parent a1dc2d1 commit 67495ab
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 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,95 @@ func TestLabelTransaction(t *testing.T) {
})
}
}

// TestGetTransaction writes a tx to disk and then compares
// the hash to the response from the GetTransaction function.
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 height.
blockHeight int32

// The error we expect to be returned.
expectedErr error
}{
{
name: "existing transaction, not mined",
txKnown: true,
minedTx: false,
expectedErr: nil,
},
{
name: "non-existing transaction",
txKnown: false,
minedTx: false,
expectedErr: ErrNoTx,
},
{
name: "existing confirmed transaction",
txKnown: true,
minedTx: true,
blockHeight: 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,
)
if test.minedTx {
return w.TxStore.InsertTx(
ns,
rec,
&wtxmgr.BlockMeta{
Block: wtxmgr.Block{
Height: test.blockHeight,
},
},
)
}
return w.TxStore.InsertTx(ns, rec, nil)

})
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))
}
if test.minedTx {
require.Equal(t, test.blockHeight, tx.Height)
}
})
}
}

0 comments on commit 67495ab

Please sign in to comment.