Browse Source

types: add tests for blockmeta (#5013)

## Description

add tests for to/from proto for blockmeta and add validate basic test case.

Closes: #XXX
pull/5017/head
Marko 4 years ago
committed by GitHub
parent
commit
8149bf517f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 90 additions and 3 deletions
  1. +90
    -3
      types/block_meta_test.go

+ 90
- 3
types/block_meta_test.go View File

@ -1,7 +1,94 @@
package types package types
import "testing"
import (
"testing"
func TestBlockMetaValidateBasic(t *testing.T) {
// TODO
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/tmhash"
tmrand "github.com/tendermint/tendermint/libs/rand"
)
func TestBlockMeta_ToProto(t *testing.T) {
h := makeRandHeader()
bi := BlockID{Hash: h.Hash(), PartsHeader: PartSetHeader{Total: 123, Hash: tmrand.Bytes(tmhash.Size)}}
bm := &BlockMeta{
BlockID: bi,
BlockSize: 200,
Header: h,
NumTxs: 0,
}
tests := []struct {
testName string
bm *BlockMeta
expErr bool
}{
{"success", bm, false},
{"failure nil", nil, true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.testName, func(t *testing.T) {
pb := tt.bm.ToProto()
bm, err := BlockMetaFromProto(pb)
if !tt.expErr {
require.NoError(t, err, tt.testName)
require.Equal(t, tt.bm, bm, tt.testName)
} else {
require.Error(t, err, tt.testName)
}
})
}
}
func TestBlockMeta_ValidateBasic(t *testing.T) {
h := makeRandHeader()
bi := BlockID{Hash: h.Hash(), PartsHeader: PartSetHeader{Total: 123, Hash: tmrand.Bytes(tmhash.Size)}}
bi2 := BlockID{Hash: tmrand.Bytes(tmhash.Size),
PartsHeader: PartSetHeader{Total: 123, Hash: tmrand.Bytes(tmhash.Size)}}
bi3 := BlockID{Hash: []byte("incorrect hash"),
PartsHeader: PartSetHeader{Total: 123, Hash: []byte("incorrect hash")}}
bm := &BlockMeta{
BlockID: bi,
BlockSize: 200,
Header: h,
NumTxs: 0,
}
bm2 := &BlockMeta{
BlockID: bi2,
BlockSize: 200,
Header: h,
NumTxs: 0,
}
bm3 := &BlockMeta{
BlockID: bi3,
BlockSize: 200,
Header: h,
NumTxs: 0,
}
tests := []struct {
name string
bm *BlockMeta
wantErr bool
}{
{"success", bm, false},
{"failure wrong blockID hash", bm2, true},
{"failure wrong length blockID hash", bm3, true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if err := tt.bm.ValidateBasic(); (err != nil) != tt.wantErr {
t.Errorf("BlockMeta.ValidateBasic() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
} }

Loading…
Cancel
Save