prysm-pulse/beacon-chain/db/kv/archived_point_test.go
2020-04-11 13:54:19 -07:00

71 lines
1.4 KiB
Go

package kv
import (
"context"
"testing"
)
func TestArchivedPointIndexRoot_CanSaveRetrieve(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
ctx := context.Background()
i1 := uint64(100)
r1 := [32]byte{'A'}
received := db.ArchivedPointRoot(ctx, i1)
if r1 == received {
t.Fatal("Should not have been saved")
}
if err := db.SaveArchivedPointRoot(ctx, r1, i1); err != nil {
t.Fatal(err)
}
received = db.ArchivedPointRoot(ctx, i1)
if r1 != received {
t.Error("Should have been saved")
}
}
func TestLastArchivedPoint_CanRetrieve(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
ctx := context.Background()
i, err := db.LastArchivedIndex(ctx)
if err != nil {
t.Fatal(err)
}
if i != 0 {
t.Error("Did not get correct index")
}
if err := db.SaveArchivedPointRoot(ctx, [32]byte{'A'}, 1); err != nil {
t.Fatal(err)
}
if err := db.SaveArchivedPointRoot(ctx, [32]byte{'B'}, 3); err != nil {
t.Fatal(err)
}
if err := db.SaveLastArchivedIndex(ctx, 1); err != nil {
t.Fatal(err)
}
if db.LastArchivedIndexRoot(ctx) != [32]byte{'A'} {
t.Error("Did not get wanted root")
}
if err := db.SaveLastArchivedIndex(ctx, 3); err != nil {
t.Fatal(err)
}
if db.LastArchivedIndexRoot(ctx) != [32]byte{'B'} {
t.Error("Did not get wanted root")
}
i, err = db.LastArchivedIndex(ctx)
if err != nil {
t.Fatal(err)
}
if i != 3 {
t.Error("Did not get correct index")
}
}