Add interface tests for FetchTxByShaList.

This commit is contained in:
Dave Collins 2013-10-14 13:49:10 -05:00
parent fb5f9c0b5a
commit 2ab3e0a382

View file

@ -189,6 +189,99 @@ func testFetchTxBySha(tc *testContext) bool {
return true return true
} }
// testFetchTxByShaList ensures FetchTxByShaList conforms to the interface
// contract.
func testFetchTxByShaList(tc *testContext) bool {
txHashes, err := tc.block.TxShas()
if err != nil {
tc.t.Errorf("block.TxShas: %v", err)
return false
}
txReplyList := tc.db.FetchTxByShaList(txHashes)
if len(txReplyList) != len(txHashes) {
tc.t.Errorf("FetchTxByShaList (%s): tx reply list for "+
"block #%d (%v) does not match expected length "+
"- got: %v, want: %v", tc.dbType, tc.blockHeight,
tc.blockHash, len(txReplyList), len(txHashes))
return false
}
for i, tx := range tc.block.MsgBlock().Transactions {
txHash := txHashes[i]
txD := txReplyList[i]
// The transaction hash in the reply must be the expected value.
if !txD.Sha.IsEqual(txHash) {
tc.t.Errorf("FetchTxByShaList (%s): tx #%d hash "+
"does not match expected - got %v, "+
"want %v", tc.dbType, i, txD.Sha, txHashes[i])
return false
}
// The reply must not indicate any errors.
if txD.Err != nil {
tc.t.Errorf("FetchTxByShaList (%s): tx #%d (%v) "+
"returned unexpected error - got %v, "+
"want nil", tc.dbType, i, txD.Sha, txD.Err)
return false
}
// The transaction in the reply fetched from the database must
// be the same MsgTx that was stored.
if !reflect.DeepEqual(tx, txD.Tx) {
tc.t.Errorf("FetchTxByShaList (%s): tx #%d (%v) from "+
"database does not match stored tx\n"+
"got: %v\nwant: %v", tc.dbType, i, txHash,
spew.Sdump(txD.Tx), spew.Sdump(tx))
return false
}
// The block hash in the reply from the database must be the
// expected value.
if txD.BlkSha == nil {
tc.t.Errorf("FetchTxByShaList (%s): tx #%d (%v) "+
"returned nil block hash", tc.dbType, i, txD.Sha)
return false
}
if !txD.BlkSha.IsEqual(tc.blockHash) {
tc.t.Errorf("FetchTxByShaList (%s): tx #%d (%v) "+
"returned unexpected block hash - got %v, "+
"want %v", tc.dbType, i, txD.Sha, txD.BlkSha,
tc.blockHash)
return false
}
// The block height in the reply from the database must be the
// expected value.
if txD.Height != tc.blockHeight {
tc.t.Errorf("FetchTxByShaList (%s): tx #%d (%v) "+
"returned unexpected block height - got %v, "+
"want %v", tc.dbType, i, txD.Sha, txD.Height,
tc.blockHeight)
return false
}
// The spend data in the reply from the database must not
// indicate any of the transactions that were just inserted are
// spent.
if txD.TxSpent == nil {
tc.t.Errorf("FetchTxByShaList (%s): tx #%d (%v) "+
"returned nil spend data", tc.dbType, i, txD.Sha)
return false
}
noSpends := make([]bool, len(tx.TxOut))
if !reflect.DeepEqual(txD.TxSpent, noSpends) {
tc.t.Errorf("FetchTxByShaList (%s): tx #%d (%v) "+
"returned unexpected spend data - got %v, "+
"want %v", tc.dbType, i, txD.Sha, txD.TxSpent,
noSpends)
return false
}
}
return true
}
// testInterface tests performs tests for the various interfaces of btcdb which // testInterface tests performs tests for the various interfaces of btcdb which
// require state in the database for the given database type. // require state in the database for the given database type.
func testInterface(t *testing.T, dbType string) { func testInterface(t *testing.T, dbType string) {
@ -258,6 +351,13 @@ func testInterface(t *testing.T, dbType string) {
if !testFetchTxBySha(&context) { if !testFetchTxBySha(&context) {
return return
} }
// All of the transactions in the block must be fetchable via
// FetchTxByShaList and all of the list replies must have the
// expected values.
if !testFetchTxByShaList(&context) {
return
}
} }
// TODO(davec): Need to figure out how to handle the special checks // TODO(davec): Need to figure out how to handle the special checks
@ -274,7 +374,7 @@ func testInterface(t *testing.T, dbType string) {
FetchHeightRange(startHeight, endHeight int64) (rshalist []btcwire.ShaHash, err error) FetchHeightRange(startHeight, endHeight int64) (rshalist []btcwire.ShaHash, err error)
- ExistsTxSha(sha *btcwire.ShaHash) (exists bool) - ExistsTxSha(sha *btcwire.ShaHash) (exists bool)
- FetchTxBySha(txsha *btcwire.ShaHash) ([]*TxListReply, error) - FetchTxBySha(txsha *btcwire.ShaHash) ([]*TxListReply, error)
FetchTxByShaList(txShaList []*btcwire.ShaHash) []*TxListReply - FetchTxByShaList(txShaList []*btcwire.ShaHash) []*TxListReply
FetchUnSpentTxByShaList(txShaList []*btcwire.ShaHash) []*TxListReply FetchUnSpentTxByShaList(txShaList []*btcwire.ShaHash) []*TxListReply
- InsertBlock(block *btcutil.Block) (height int64, err error) - InsertBlock(block *btcutil.Block) (height int64, err error)
InvalidateBlockCache() InvalidateBlockCache()