Added new RPC tests

This commit is contained in:
Jakub Matys 2018-07-10 17:04:07 +02:00
parent 414d51ef79
commit c6d6d4138d
8 changed files with 196 additions and 216 deletions

View File

@ -88,6 +88,18 @@ func TestBCashRPC_EstimateFee(t *testing.T) {
tests.mainnet.TestEstimateFee(t)
}
func TestBCashRPC_GetBestBlockHash(t *testing.T) {
tests.mainnet.TestGetBestBlockHash(t)
}
func TestBCashRPC_GetBestBlockHeight(t *testing.T) {
tests.mainnet.TestGetBestBlockHeight(t)
}
func TestBCashRPC_GetBlockHeader(t *testing.T) {
tests.mainnet.TestGetBlockHeader(t)
}
func TestBCashTestnetRPC_GetBlockHash(t *testing.T) {
tests.testnet.TestGetBlockHash(t)
}
@ -123,3 +135,15 @@ func TestBCashTestnetRPC_EstimateSmartFee(t *testing.T) {
func TestBCashTestnetRPC_EstimateFee(t *testing.T) {
tests.testnet.TestEstimateFee(t)
}
func TestBCashTestnetRPC_GetBestBlockHash(t *testing.T) {
tests.testnet.TestGetBestBlockHash(t)
}
func TestBCashTestnetRPC_GetBestBlockHeight(t *testing.T) {
tests.testnet.TestGetBestBlockHeight(t)
}
func TestBCashTestnetRPC_GetBlockHeader(t *testing.T) {
tests.testnet.TestGetBlockHeader(t)
}

View File

@ -87,6 +87,18 @@ func TestBitcoinRPC_EstimateFee(t *testing.T) {
tests.mainnet.TestEstimateFee(t)
}
func TestBitcoinRPC_GetBestBlockHash(t *testing.T) {
tests.mainnet.TestGetBestBlockHash(t)
}
func TestBitcoinRPC_GetBestBlockHeight(t *testing.T) {
tests.mainnet.TestGetBestBlockHeight(t)
}
func TestBitcoinRPC_GetBlockHeader(t *testing.T) {
tests.mainnet.TestGetBlockHeader(t)
}
func TestBitcoinTestnetRPC_GetBlockHash(t *testing.T) {
tests.testnet.TestGetBlockHash(t)
}
@ -122,3 +134,15 @@ func TestBitcoinTestnetRPC_EstimateSmartFee(t *testing.T) {
func TestBitcoinTestnetRPC_EstimateFee(t *testing.T) {
tests.testnet.TestEstimateFee(t)
}
func TestBitcoinTestnetRPC_GetBestBlockHash(t *testing.T) {
tests.testnet.TestGetBestBlockHash(t)
}
func TestBitcoinTestnetRPC_GetBestBlockHeight(t *testing.T) {
tests.testnet.TestGetBestBlockHeight(t)
}
func TestBitcoinTestnetRPC_GetBlockHeader(t *testing.T) {
tests.testnet.TestGetBlockHeader(t)
}

View File

@ -80,6 +80,18 @@ func TestDashRPC_EstimateFee(t *testing.T) {
tests.mainnet.TestEstimateFee(t)
}
func TestDashRPC_GetBestBlockHash(t *testing.T) {
tests.mainnet.TestGetBestBlockHash(t)
}
func TestDashRPC_GetBestBlockHeight(t *testing.T) {
tests.mainnet.TestGetBestBlockHeight(t)
}
func TestDashRPC_GetBlockHeader(t *testing.T) {
tests.mainnet.TestGetBlockHeader(t)
}
func TestDashTestnetRPC_GetBlockHash(t *testing.T) {
tests.testnet.TestGetBlockHash(t)
}
@ -111,3 +123,15 @@ func TestDashTestnetRPC_EstimateSmartFee(t *testing.T) {
func TestDashTestnetRPC_EstimateFee(t *testing.T) {
tests.testnet.TestEstimateFee(t)
}
func TestDashTestnetRPC_GetBestBlockHash(t *testing.T) {
tests.testnet.TestGetBestBlockHash(t)
}
func TestDashTestnetRPC_GetBestBlockHeight(t *testing.T) {
tests.testnet.TestGetBestBlockHeight(t)
}
func TestDashTestnetRPC_GetBlockHeader(t *testing.T) {
tests.testnet.TestGetBlockHeader(t)
}

View File

@ -8,10 +8,7 @@ import (
"encoding/json"
"flag"
"os"
"reflect"
"testing"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
func getRPCClient(cfg json.RawMessage) (bchain.BlockChain, error) {
@ -48,221 +45,18 @@ func TestEthRPC_GetTransaction(t *testing.T) {
rpcTest.TestGetTransaction(t)
}
func TestEthRPC_getBestHeader(t *testing.T) {
type fields struct {
b *EthereumRPC
}
tests := []struct {
name string
fields fields
want *ethtypes.Header
wantErr bool
}{
{
name: "1",
fields: fields{
b: rpcTest.Client.(*EthereumRPC),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.fields.b.getBestHeader()
if (err != nil) != tt.wantErr {
t.Errorf("EthRPC.getBestHeader() error = %v, wantErr %v", err, tt.wantErr)
return
}
// the header is always different, do not compare what we got
})
}
}
func TestEthRPC_GetBestBlockHash(t *testing.T) {
type fields struct {
b *EthereumRPC
}
tests := []struct {
name string
fields fields
want int
wantErr bool
}{
{
name: "1",
fields: fields{
b: rpcTest.Client.(*EthereumRPC),
},
want: 66,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.fields.b.GetBestBlockHash()
if (err != nil) != tt.wantErr {
t.Errorf("EthRPC.GetBestBlockHash() error = %v, wantErr %v", err, tt.wantErr)
return
}
// the hash is always different, compare only the length of hash
if len(got) != tt.want {
t.Errorf("EthRPC.GetBestBlockHash() = %v, len %v, want len %v", got, len(got), tt.want)
}
})
}
rpcTest.TestGetBestBlockHash(t)
}
func TestEthRPC_GetBestBlockHeight(t *testing.T) {
type fields struct {
b *EthereumRPC
}
tests := []struct {
name string
fields fields
want uint32
wantErr bool
}{
{
name: "1",
fields: fields{
b: rpcTest.Client.(*EthereumRPC),
},
want: 1000000,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.fields.b.GetBestBlockHeight()
if (err != nil) != tt.wantErr {
t.Errorf("EthRPC.GetBestBlockHeight() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got < tt.want {
t.Errorf("EthRPC.GetBestBlockHeight() = %v, want at least %v", got, tt.want)
}
})
}
rpcTest.TestGetBestBlockHeight(t)
}
func TestEthRPC_GetBlockHeader(t *testing.T) {
bh, err := rpcTest.Client.(*EthereumRPC).getBestHeader()
if err != nil {
panic(err)
}
type fields struct {
b *EthereumRPC
}
type args struct {
hash string
}
tests := []struct {
name string
fields fields
args args
want *bchain.BlockHeader
wantErr error
}{
{
name: "2870000",
fields: fields{
b: rpcTest.Client.(*EthereumRPC),
},
args: args{
hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
},
want: &bchain.BlockHeader{
Hash: "0xeccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
Height: 2870000,
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000 + 1),
},
},
{
name: "ErrBlockNotFound",
fields: fields{
b: rpcTest.Client.(*EthereumRPC),
},
args: args{
hash: "0xeccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903896",
},
wantErr: bchain.ErrBlockNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.fields.b.GetBlockHeader(tt.args.hash)
if err != tt.wantErr {
t.Errorf("EthRPC.GetBlockHeader() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("EthRPC.GetBlockHeader() = %v, want %v", got, tt.want)
}
})
}
rpcTest.TestGetBlockHeader(t)
}
func TestEthRPC_EstimateFee(t *testing.T) {
type fields struct {
b *EthereumRPC
}
type args struct {
blocks int
}
tests := []struct {
name string
fields fields
args args
want float64
wantErr bool
}{
{
name: "1",
fields: fields{
b: rpcTest.Client.(*EthereumRPC),
},
args: args{
blocks: 10,
},
want: 1., // check that there is some estimate
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.fields.b.EstimateFee(tt.args.blocks)
if (err != nil) != tt.wantErr {
t.Errorf("EthRPC.EstimateFee() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got < tt.want {
t.Errorf("EthRPC.EstimateFee() = %v, want %v", got, tt.want)
}
})
}
}
func TestEthRPC_GetMempool(t *testing.T) {
type fields struct {
b *EthereumRPC
}
tests := []struct {
name string
fields fields
want []string
wantErr bool
}{
{
name: "1",
fields: fields{
b: rpcTest.Client.(*EthereumRPC),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.fields.b.GetMempool()
if (err != nil) != tt.wantErr {
t.Errorf("EthRPC.GetMempool() error = %v, wantErr %v", err, tt.wantErr)
return
}
t.Logf("EthRPC.GetMempool() returned %v transactions", len(got))
})
}
rpcTest.TestEstimateFee(t)
}

View File

@ -76,3 +76,15 @@ func TestNamecoinRPC_EstimateSmartFee(t *testing.T) {
func TestNamecoinRPC_EstimateFee(t *testing.T) {
rpcTest.TestEstimateFee(t)
}
func TestNamecoinRPC_GetBestBlockHash(t *testing.T) {
rpcTest.TestGetBestBlockHash(t)
}
func TestNamecoinRPC_GetBestBlockHeight(t *testing.T) {
rpcTest.TestGetBestBlockHeight(t)
}
func TestNamecoinRPC_GetBlockHeader(t *testing.T) {
rpcTest.TestGetBlockHeader(t)
}

View File

@ -74,3 +74,15 @@ func TestVertcoinRPC_EstimateSmartFee(t *testing.T) {
func TestVertcoinRPC_EstimateFee(t *testing.T) {
rpcTest.TestEstimateFee(t)
}
func TestVertcoinRPC_GetBestBlockHash(t *testing.T) {
rpcTest.TestGetBestBlockHash(t)
}
func TestVertcoinRPC_GetBestBlockHeight(t *testing.T) {
rpcTest.TestGetBestBlockHeight(t)
}
func TestVertcoinRPC_GetBlockHeader(t *testing.T) {
rpcTest.TestGetBlockHeader(t)
}

View File

@ -79,6 +79,18 @@ func TestZCashRPC_EstimateFee(t *testing.T) {
tests.mainnet.TestEstimateFee(t)
}
func TestZCashRPC_GetBestBlockHash(t *testing.T) {
tests.mainnet.TestGetBestBlockHash(t)
}
func TestZCashRPC_GetBestBlockHeight(t *testing.T) {
tests.mainnet.TestGetBestBlockHeight(t)
}
func TestZCashRPC_GetBlockHeader(t *testing.T) {
tests.mainnet.TestGetBlockHeader(t)
}
func TestZCashTestnetRPC_GetBlockHash(t *testing.T) {
tests.testnet.TestGetBlockHash(t)
}
@ -110,3 +122,15 @@ func TestZCashTestnetRPC_EstimateSmartFee(t *testing.T) {
func TestZCashTestnetRPC_EstimateFee(t *testing.T) {
tests.testnet.TestEstimateFee(t)
}
func TestZCashTestnetRPC_GetBestBlockHash(t *testing.T) {
tests.mainnet.TestGetBestBlockHash(t)
}
func TestZCashTestnetRPC_GetBestBlockHeight(t *testing.T) {
tests.mainnet.TestGetBestBlockHeight(t)
}
func TestZCashTestnetRPC_GetBlockHeader(t *testing.T) {
tests.mainnet.TestGetBlockHeader(t)
}

View File

@ -155,12 +155,11 @@ func (rt *Test) TestGetTransaction(t *testing.T) {
return
}
// Confirmations is variable field, we just check if is set and reset it
if got.Confirmations > 0 {
got.Confirmations = 0
} else {
t.Errorf("GetTransaction() has empty Confirmations field")
if got.Confirmations <= 0 {
t.Errorf("GetTransaction() got struct with invalid Confirmations field")
continue
}
got.Confirmations = 0
if !reflect.DeepEqual(got, want) {
t.Errorf("GetTransaction() got %v, want %v", got, want)
@ -362,7 +361,7 @@ func (rt *Test) TestEstimateSmartFee(t *testing.T) {
if err != nil {
t.Error(err)
}
if fee != -1 && (fee < 0 || fee > 1) {
if fee != -1 && fee < 0 {
t.Errorf("EstimateSmartFee() returned unexpected fee rate: %f", fee)
}
}
@ -376,8 +375,75 @@ func (rt *Test) TestEstimateFee(t *testing.T) {
if err != nil {
t.Error(err)
}
if fee != -1 && (fee < 0 || fee > 1) {
if fee != -1 && fee < 0 {
t.Errorf("EstimateFee() returned unexpected fee rate: %f", fee)
}
}
}
func (rt *Test) TestGetBestBlockHash(t *testing.T) {
for i := 0; i < 3; i++ {
hash, err := rt.Client.GetBestBlockHash()
if err != nil {
t.Fatal(err)
}
blk, err := rt.Client.GetBlock(hash, 0)
if err != nil {
t.Fatal(err)
}
// we expect no next block
_, err = rt.Client.GetBlock("", blk.Height+1)
if err != nil {
if err != bchain.ErrBlockNotFound {
t.Error(err)
}
return
}
}
t.Error("GetBestBlockHash() didn't get the best hash")
}
func (rt *Test) TestGetBestBlockHeight(t *testing.T) {
for i := 0; i < 3; i++ {
height, err := rt.Client.GetBestBlockHeight()
if err != nil {
t.Fatal(err)
}
// we expect no next block
_, err = rt.Client.GetBlock("", height+1)
if err != nil {
if err != bchain.ErrBlockNotFound {
t.Error(err)
}
return
}
}
t.Error("GetBestBlockHeigh() didn't get the the best heigh")
}
func (rt *Test) TestGetBlockHeader(t *testing.T) {
want := &bchain.BlockHeader{
Hash: rt.TestData.BlockHash,
Height: rt.TestData.BlockHeight,
}
got, err := rt.Client.GetBlockHeader(rt.TestData.BlockHash)
if err != nil {
t.Fatal(err)
}
// Confirmations is variable field, we just check if is set and reset it
if got.Confirmations <= 0 {
t.Fatalf("GetBlockHeader() got struct with invalid Confirmations field")
}
got.Confirmations = 0
got.Prev, got.Next = "", ""
if !reflect.DeepEqual(got, want) {
t.Errorf("GetBlockHeader() got=%v, want=%v", got, want)
}
}