Skip to content

Commit 0cdc9de

Browse files
committed
add migrate unit test
1 parent 0a53f23 commit 0cdc9de

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

action/protocol/staking/contractstaking/bucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type (
2323
StakedDuration uint64 // in seconds if timestamped, in block number if not
2424
// CreatedAt is the time when the bucket was created.
2525
CreatedAt uint64 // in unix timestamp if timestamped, in block height if not
26-
// UnlockedAt is the time when the bucket can be unlocked.
26+
// UnlockedAt is the time when the bucket was unlocked.
2727
UnlockedAt uint64 // in unix timestamp if timestamped, in block height if not
2828
// UnstakedAt is the time when the bucket was unstaked.
2929
UnstakedAt uint64 // in unix timestamp if timestamped, in block height if not
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package stakingindex
2+
3+
import (
4+
"context"
5+
"math/big"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
"go.uber.org/mock/gomock"
10+
11+
"github.com/iotexproject/iotex-address/address"
12+
13+
"github.com/iotexproject/iotex-core/v2/action/protocol"
14+
"github.com/iotexproject/iotex-core/v2/action/protocol/staking"
15+
"github.com/iotexproject/iotex-core/v2/action/protocol/staking/contractstaking"
16+
"github.com/iotexproject/iotex-core/v2/test/identityset"
17+
)
18+
19+
func TestVoteView(t *testing.T) {
20+
require := require.New(t)
21+
ctrl := gomock.NewController(t)
22+
defer ctrl.Finish()
23+
24+
mockIndexer := staking.NewMockContractStakingIndexer(ctrl)
25+
vv := NewVoteView(
26+
mockIndexer,
27+
&VoteViewConfig{
28+
ContractAddr: identityset.Address(0),
29+
},
30+
100,
31+
nil,
32+
nil,
33+
func(b *contractstaking.Bucket, h uint64) *big.Int {
34+
return big.NewInt(1)
35+
},
36+
)
37+
mockHandler := staking.NewMockEventHandler(ctrl)
38+
t.Run("Migrate", func(t *testing.T) {
39+
buckets := make(map[uint64]*contractstaking.Bucket)
40+
buckets[1] = &contractstaking.Bucket{
41+
Candidate: identityset.Address(1),
42+
Owner: identityset.Address(11),
43+
StakedAmount: big.NewInt(1000000000000000000),
44+
StakedDuration: 10,
45+
CreatedAt: 123456,
46+
UnlockedAt: 0,
47+
UnstakedAt: 0,
48+
IsTimestampBased: false,
49+
Muted: false,
50+
}
51+
buckets[2] = &contractstaking.Bucket{
52+
Candidate: identityset.Address(2),
53+
Owner: identityset.Address(12),
54+
StakedAmount: big.NewInt(1000000000000000000),
55+
StakedDuration: 10,
56+
CreatedAt: 123456,
57+
UnlockedAt: 234567,
58+
UnstakedAt: 0,
59+
IsTimestampBased: true,
60+
Muted: false,
61+
}
62+
migratedBuckets := make(map[uint64]*contractstaking.Bucket)
63+
ctx := protocol.WithBlockCtx(context.Background(), protocol.BlockCtx{
64+
BlockHeight: 100,
65+
})
66+
mockIndexer.EXPECT().StartHeight().Return(uint64(0)).Times(1)
67+
mockIndexer.EXPECT().ContractStakingBuckets().Return(uint64(99), buckets, nil)
68+
mockHandler.EXPECT().PutBucket(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(addr address.Address, id uint64, bucket *contractstaking.Bucket) error {
69+
require.Equal(identityset.Address(0), addr)
70+
migratedBuckets[id] = bucket
71+
return nil
72+
}).Times(len(buckets))
73+
require.NoError(vv.Migrate(ctx, mockHandler))
74+
require.Equal(buckets, migratedBuckets)
75+
})
76+
}

0 commit comments

Comments
 (0)