From 4724e403a31f5f2aee2a67665a06af53e82764d8 Mon Sep 17 00:00:00 2001 From: Larry Ruane Date: Wed, 16 Oct 2019 16:11:36 -0600 Subject: [PATCH] add tests for GetTx* methods --- parser/transaction_test.go | 4 ++ storage/sqlite3.go | 2 +- storage/sqlite3_test.go | 105 ++++++++++++++++++++++++++++++++++++- 3 files changed, 109 insertions(+), 2 deletions(-) diff --git a/parser/transaction_test.go b/parser/transaction_test.go index cdd5603..2dbbc5b 100644 --- a/parser/transaction_test.go +++ b/parser/transaction_test.go @@ -760,6 +760,10 @@ func TestSaplingTransactionParser(t *testing.T) { if hex.EncodeToString(tx.GetDisplayHash()) != tt.txid { t.Errorf("Test %d: incorrect txid", i) } + // test caching + if hex.EncodeToString(tx.GetDisplayHash()) != tt.txid { + t.Errorf("Test %d: incorrect cached txid", i) + } } } diff --git a/storage/sqlite3.go b/storage/sqlite3.go index 5ed2a36..788c292 100644 --- a/storage/sqlite3.go +++ b/storage/sqlite3.go @@ -69,7 +69,7 @@ func GetBlock(ctx context.Context, db *sql.DB, height int) ([]byte, error) { query := "SELECT compact_encoding from blocks WHERE block_height = ?" err := db.QueryRowContext(ctx, query, height).Scan(&blockBytes) if err != nil { - return nil, err + return nil, errors.Wrap(err, fmt.Sprintf("getting block with height %d", height)) } return blockBytes, err } diff --git a/storage/sqlite3_test.go b/storage/sqlite3_test.go index 51b6e5e..3a8adb2 100644 --- a/storage/sqlite3_test.go +++ b/storage/sqlite3_test.go @@ -8,6 +8,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "strings" "testing" "time" @@ -83,6 +84,17 @@ func TestSqliteStorage(t *testing.T) { if !bytes.Equal(blockLookup, marshaled) { t.Errorf("GetBlockByHash unexpected result, block %d", test.BlockHeight) } + // nonexistent hash + _, err = GetBlockByHash(ctx, db, "4ff234f7b51971cbeb7719a1c32d1c7e1ed92afafed266a7b1ae235717df0501") + if err == nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("GetBlockByHash unexpected success block %d", test.BlockHeight))) + continue + } + if !strings.Contains(err.Error(), "getting block with hash") || + !strings.Contains(err.Error(), "no rows in result set") { + t.Error(errors.Wrap(err, fmt.Sprintf("GetBlockByHash wrong error block %d", test.BlockHeight))) + continue + } } } @@ -117,6 +129,15 @@ func TestSqliteStorage(t *testing.T) { if err != nil { t.Error(errors.Wrap(err, "retrieving stored block")) } + _, err = GetBlock(ctx, db, blockHeight+1) + if err == nil { + t.Fatal(errors.Wrap(err, "GetBlock unexpected success")) + } + if !strings.Contains(err.Error(), "getting block with height") || + !strings.Contains(err.Error(), "no rows in result set") { + t.Error(errors.Wrap(err, fmt.Sprintf("GetBlock wrong error string: %s", err.Error()))) + } + cblock := &walletrpc.CompactBlock{} err = proto.Unmarshal(storedBlock, cblock) if err != nil { @@ -205,6 +226,26 @@ func TestSqliteStorage(t *testing.T) { if count > 0 { t.Errorf("got some blocks that shouldn't be there") } + + // Test requesting range that's too large + count = 0 + go GetBlockRange(timeout, db, blockOut, errOut, 279465, 289465) + recvLoop4: + for { + select { + case <-blockOut: + count++ + case err := <-errOut: + if err != ErrLotsOfBlocks { + t.Error(errors.Wrap(err, "in too-large blockrange")) + } + break recvLoop4 + } + } + + if count > 0 { + t.Errorf("got some blocks that shouldn't be there") + } } // Transaction storage @@ -239,7 +280,69 @@ func TestSqliteStorage(t *testing.T) { if len(storedBytes) != len(tx.Bytes()) { t.Errorf("Wrong tx size, want %d got %d", len(tx.Bytes()), storedBytes) } - + { + r, err := GetTxByHash(ctx, db, txHash) + if err != nil || !bytes.Equal(r, tx.Bytes()) { + t.Error("GetTxByHash() incorrect return") + } + // nonexistent tx hash + _, err = GetTxByHash(ctx, db, "42") + if err == nil { + t.Fatal(errors.Wrap(err, "GetTxByHash unexpected success")) + } + if !strings.Contains(err.Error(), "getting tx with hash") || + !strings.Contains(err.Error(), "no rows in result set") { + t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHash wrong error string: %s", err.Error()))) + } + } + { + r, err := GetTxByHeightAndIndex(ctx, db, block.GetHeight(), 0) + if err != nil || !bytes.Equal(r, tx.Bytes()) { + t.Error("GetTxByHeightAndIndex() incorrect return") + } + // nonexistent height + _, err = GetTxByHeightAndIndex(ctx, db, 47, 0) + if err == nil { + t.Fatal(errors.Wrap(err, "GetTxByHeightAndIndex unexpected success")) + } + if !strings.Contains(err.Error(), "getting tx (") || + !strings.Contains(err.Error(), "no rows in result set") { + t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHeightAndIndex wrong error string: %s", err.Error()))) + } + // nonexistent index + _, err = GetTxByHeightAndIndex(ctx, db, block.GetHeight(), 1) + if err == nil { + t.Fatal(errors.Wrap(err, "GetTxByHeightAndIndex unexpected success")) + } + if !strings.Contains(err.Error(), "getting tx (") || + !strings.Contains(err.Error(), "no rows in result set") { + t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHeightAndIndex wrong error string: %s", err.Error()))) + } + } + { + r, err := GetTxByHashAndIndex(ctx, db, blockHash, 0) + if err != nil || !bytes.Equal(r, tx.Bytes()) { + t.Error("GetTxByHashAndIndex() incorrect return") + } + // nonexistent block hash + _, err = GetTxByHashAndIndex(ctx, db, "43", 0) + if err == nil { + t.Fatal(errors.Wrap(err, "GetTxByHashAndIndex unexpected success")) + } + if !strings.Contains(err.Error(), "getting tx (") || + !strings.Contains(err.Error(), "no rows in result set") { + t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHashAndIndex wrong error string: %s", err.Error()))) + } + // nonexistent index + _, err = GetTxByHashAndIndex(ctx, db, blockHash, 1) + if err == nil { + t.Fatal(errors.Wrap(err, "GetTxByHashAndIndex unexpected success")) + } + if !strings.Contains(err.Error(), "getting tx (") || + !strings.Contains(err.Error(), "no rows in result set") { + t.Error(errors.Wrap(err, fmt.Sprintf("GetTxByHashAndIndex wrong error string: %s", err.Error()))) + } + } } }