prysm-pulse/beacon-chain/db/kv/validators_test.go
Raul Jordan 8f967d26d7
Allow Nil Return from DB Methods (#3247)
* allow nil return and add proper tests

* pass tests
2019-08-20 12:24:29 -05:00

75 lines
1.9 KiB
Go

package kv
import (
"context"
"testing"
"github.com/gogo/protobuf/proto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)
func TestStore_ValidatorIndexCRUD(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
validatorIdx := uint64(100)
pubKey := [48]byte{1, 2, 3, 4}
ctx := context.Background()
_, ok, err := db.ValidatorIndex(ctx, pubKey)
if err != nil {
t.Fatal(err)
}
if ok {
t.Fatal("Expected validator index to not exist")
}
if err := db.SaveValidatorIndex(ctx, pubKey, validatorIdx); err != nil {
t.Fatal(err)
}
retrievedIdx, ok, err := db.ValidatorIndex(ctx, pubKey)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("Expected validator index to have been properly retrieved")
}
if retrievedIdx != validatorIdx {
t.Errorf("Wanted %d, received %d", validatorIdx, retrievedIdx)
}
if err := db.DeleteValidatorIndex(ctx, pubKey); err != nil {
t.Fatal(err)
}
if db.HasValidatorIndex(ctx, pubKey) {
t.Error("Expected validator index to have been deleted from the db")
}
}
func TestStore_ValidatorLatestVoteCRUD(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
ctx := context.Background()
validatorIdx := uint64(100)
latestVote := &pb.ValidatorLatestVote{
Epoch: 1,
Root: []byte("root"),
}
retrievedVote, err := db.ValidatorLatestVote(ctx, validatorIdx)
if err != nil {
t.Fatal(err)
}
if retrievedVote != nil {
t.Errorf("Expected nil validator latest vote, received %v", retrievedVote)
}
if err := db.SaveValidatorLatestVote(ctx, validatorIdx, latestVote); err != nil {
t.Fatal(err)
}
if !db.HasValidatorLatestVote(ctx, validatorIdx) {
t.Error("Expected validator latest vote to exist in the db")
}
retrievedVote, err = db.ValidatorLatestVote(ctx, validatorIdx)
if err != nil {
t.Fatal(err)
}
if !proto.Equal(latestVote, retrievedVote) {
t.Errorf("Wanted %d, received %d", latestVote, retrievedVote)
}
}