github.com/lino-network/lino@v0.6.11/x/vote/model/storage_test.go (about) 1 package model 2 3 import ( 4 "testing" 5 "time" 6 7 sdk "github.com/cosmos/cosmos-sdk/types" 8 "github.com/stretchr/testify/suite" 9 10 "github.com/lino-network/lino/testsuites" 11 "github.com/lino-network/lino/testutils" 12 linotypes "github.com/lino-network/lino/types" 13 "github.com/lino-network/lino/x/vote/types" 14 ) 15 16 var ( 17 storeKeyStr = "testVoterStore" 18 kvStoreKey = sdk.NewKVStoreKey(storeKeyStr) 19 ) 20 21 type VoteStoreDumper struct{} 22 23 func (dumper VoteStoreDumper) NewDumper() *testutils.Dumper { 24 return NewVoteDumper(NewVoteStorage(kvStoreKey)) 25 } 26 27 type voteStoreTestSuite struct { 28 testsuites.GoldenTestSuite 29 store VoteStorage 30 } 31 32 func NewVoteStoreTestSuite() *voteStoreTestSuite { 33 return &voteStoreTestSuite{ 34 GoldenTestSuite: testsuites.NewGoldenTestSuite(VoteStoreDumper{}, kvStoreKey), 35 } 36 } 37 38 func (suite *voteStoreTestSuite) SetupTest() { 39 suite.SetupCtx(0, time.Unix(0, 0), kvStoreKey) 40 suite.store = NewVoteStorage(kvStoreKey) 41 } 42 43 func TestVoteStoreSuite(t *testing.T) { 44 suite.Run(t, NewVoteStoreTestSuite()) 45 } 46 47 func (suite *voteStoreTestSuite) TestGetSetVoter() { 48 store := suite.store 49 ctx := suite.Ctx 50 user1 := linotypes.AccountKey("user1") 51 user2 := linotypes.AccountKey("user2") 52 voter1 := Voter{ 53 Username: user1, 54 LinoStake: linotypes.NewCoinFromInt64(123), 55 LastPowerChangeAt: 777, 56 Interest: linotypes.NewCoinFromInt64(234), 57 Duty: types.DutyValidator, 58 FrozenAmount: linotypes.NewCoinFromInt64(9), 59 } 60 voter2 := Voter{ 61 Username: user2, 62 LinoStake: linotypes.NewCoinFromInt64(345), 63 LastPowerChangeAt: 888, 64 Interest: linotypes.NewCoinFromInt64(456), 65 Duty: types.DutyValidator, 66 FrozenAmount: linotypes.NewCoinFromInt64(12), 67 } 68 69 suite.False(store.DoesVoterExist(ctx, user1)) 70 _, err := store.GetVoter(ctx, user1) 71 suite.Equal(types.ErrVoterNotFound(), err) 72 73 suite.store.SetVoter(ctx, &voter1) 74 suite.store.SetVoter(ctx, &voter2) 75 76 v1, err := store.GetVoter(ctx, user1) 77 suite.Nil(err) 78 suite.Equal(&voter1, v1) 79 80 v2, err := store.GetVoter(ctx, user2) 81 suite.Nil(err) 82 suite.Equal(&voter2, v2) 83 84 suite.Golden() 85 } 86 87 func (suite *voteStoreTestSuite) TestGetSetLinoStakeStats() { 88 store := suite.store 89 ctx := suite.Ctx 90 stats1 := LinoStakeStat{ 91 TotalConsumptionFriction: linotypes.NewCoinFromInt64(123), 92 UnclaimedFriction: linotypes.NewCoinFromInt64(456), 93 TotalLinoStake: linotypes.NewCoinFromInt64(789), 94 UnclaimedLinoStake: linotypes.NewCoinFromInt64(999), 95 } 96 stats2 := LinoStakeStat{ 97 TotalConsumptionFriction: linotypes.NewCoinFromInt64(888), 98 UnclaimedFriction: linotypes.NewCoinFromInt64(888), 99 TotalLinoStake: linotypes.NewCoinFromInt64(888), 100 UnclaimedLinoStake: linotypes.NewCoinFromInt64(888), 101 } 102 103 _, err := store.GetLinoStakeStat(ctx, 1) 104 suite.Equal(types.ErrStakeStatNotFound(1), err) 105 106 suite.store.SetLinoStakeStat(ctx, 1, &stats1) 107 suite.store.SetLinoStakeStat(ctx, 2, &stats2) 108 109 v1, err := store.GetLinoStakeStat(ctx, 1) 110 suite.Nil(err) 111 suite.Equal(&stats1, v1) 112 113 v2, err := store.GetLinoStakeStat(ctx, 2) 114 suite.Nil(err) 115 suite.Equal(&stats2, v2) 116 117 suite.Golden() 118 }