github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/staking/endorsement_statemanager_test.go (about) 1 package staking 2 3 import ( 4 "testing" 5 6 "github.com/golang/mock/gomock" 7 "github.com/stretchr/testify/require" 8 9 "github.com/iotexproject/iotex-core/action/protocol" 10 "github.com/iotexproject/iotex-core/state" 11 "github.com/iotexproject/iotex-core/testutil/testdb" 12 ) 13 14 func TestEndorsementStateManager_Put(t *testing.T) { 15 r := require.New(t) 16 ctrl := gomock.NewController(t) 17 sm := testdb.NewMockStateManager(ctrl) 18 esm := NewEndorsementStateManager(sm) 19 20 // insert endorsement 21 bucketIndex := uint64(123) 22 endorse := &Endorsement{ 23 ExpireHeight: 456, 24 } 25 err := esm.Put(bucketIndex, endorse) 26 r.NoError(err) 27 expect := Endorsement{} 28 _, err = esm.State(&expect, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(endorsementKey(bucketIndex))) 29 r.NoError(err) 30 r.EqualValues(expect.ExpireHeight, 456) 31 32 // update endorsement 33 endorse.ExpireHeight = 789 34 err = esm.Put(bucketIndex, endorse) 35 r.NoError(err) 36 _, err = esm.State(&expect, protocol.NamespaceOption(_stakingNameSpace), protocol.KeyOption(endorsementKey(bucketIndex))) 37 r.NoError(err) 38 r.EqualValues(expect.ExpireHeight, 789) 39 } 40 41 func TestEndorsementStateReader_Get(t *testing.T) { 42 r := require.New(t) 43 ctrl := gomock.NewController(t) 44 sm := testdb.NewMockStateManager(ctrl) 45 esm := NewEndorsementStateManager(sm) 46 esr := NewEndorsementStateReader(sm) 47 48 // get non-exist endorsement 49 bucketIndex := uint64(123) 50 _, err := esr.Get(bucketIndex) 51 r.ErrorIs(err, state.ErrStateNotExist) 52 _, err = esm.Get(bucketIndex) 53 r.ErrorIs(err, state.ErrStateNotExist) 54 55 // get exist endorsement 56 endorse := &Endorsement{ 57 ExpireHeight: 456, 58 } 59 err = esm.Put(bucketIndex, endorse) 60 r.NoError(err) 61 expect, err := esr.Get(bucketIndex) 62 r.NoError(err) 63 r.EqualValues(expect.ExpireHeight, 456) 64 expect, err = esm.Get(bucketIndex) 65 r.NoError(err) 66 r.EqualValues(expect.ExpireHeight, 456) 67 }