From 67495abbc9dc0c294331eaba65045056195cc877 Mon Sep 17 00:00:00 2001 From: ErikEk Date: Thu, 12 Oct 2023 09:46:48 +0200 Subject: [PATCH] wallet: add test --- wallet/wallet_test.go | 93 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index 521756a4a1..4738280133 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -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" ) @@ -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) + } + }) + } +}