github.com/lino-network/lino@v0.6.11/x/reputation/repv2/store_test.go (about) 1 package repv2 2 3 import ( 4 "testing" 5 6 "github.com/lino-network/lino/x/reputation/repv2/internal" 7 "github.com/stretchr/testify/suite" 8 ) 9 10 type StoreTestSuite struct { 11 suite.Suite 12 mockDB *internal.MockStore 13 } 14 15 func TestStoreTestSuite(t *testing.T) { 16 suite.Run(t, &StoreTestSuite{}) 17 } 18 19 func (suite *StoreTestSuite) SetupTest() { 20 suite.mockDB = internal.NewMockStore() 21 } 22 23 func (suite *StoreTestSuite) TestPrefix() { 24 suite.Equal(append(repUserMetaPrefix, []byte("qwe")...), getUserMetaKey("qwe")) 25 suite.Equal(append(repRoundMetaPrefix, []byte("3")...), getRoundMetaKey(3)) 26 suite.Equal(append(repRoundPostMetaPrefix, 27 []byte{byte('b'), byte('/'), byte('x'), byte('y')}...), getRoundPostMetaKey(11, "xy")) 28 suite.Equal(append(repRoundPostMetaPrefix, 29 []byte{byte('z'), byte('/'), byte('x'), byte('y')}...), getRoundPostMetaKey(35, "xy")) 30 suite.Equal(append(repRoundPostMetaPrefix, 31 []byte{byte('2'), byte('f'), byte('/'), byte('a'), byte('b'), byte('c'), byte('d')}...), 32 getRoundPostMetaKey(87, "abcd")) 33 suite.Equal(repGameMetaPrefix, getGameKey()) 34 } 35 36 func (suite *StoreTestSuite) TestInitValues() { 37 store := NewReputationStore(suite.mockDB, DefaultInitialReputation) 38 39 // user 40 user := store.GetUserMeta("no") 41 suite.Equal(NewInt(DefaultInitialReputation), user.Reputation) 42 suite.Equal(RoundId(0), user.LastSettledRound) 43 suite.Equal(RoundId(0), user.LastDonationRound) 44 suite.Empty(user.Unsettled) 45 46 // round meta 47 round := store.GetRoundMeta(333) 48 suite.Empty(round.Result) 49 suite.Equal(NewInt(0), round.SumIF) 50 suite.Equal(Time(0), round.StartAt) 51 suite.Empty(round.TopN) 52 53 // current round 54 suite.Equal(RoundId(1), store.GetCurrentRound()) 55 56 // post 57 post := store.GetRoundPostMeta(33, "xxx") 58 suite.Equal(NewInt(0), post.SumIF) 59 60 // game 61 game := store.GetGameMeta() 62 suite.Equal(RoundId(1), game.CurrentRound) 63 } 64 65 func (suite *StoreTestSuite) TestStoreGetSet() { 66 store := NewReputationStore(suite.mockDB, DefaultInitialReputation) 67 68 var user1 Uid = "test" 69 var user2 Uid = "test2" 70 var post1 Pid = "post1" 71 var post2 Pid = "post2" 72 73 u1 := &userMeta{ 74 Consumption: NewInt(0), 75 Hold: NewInt(0), 76 Reputation: NewInt(123), 77 LastSettledRound: 3, 78 LastDonationRound: 3, 79 Unsettled: []Donation{ 80 {Pid: post1, Amount: NewInt(3), Impact: NewInt(2)}, 81 {Pid: post2, Amount: NewInt(4), Impact: NewInt(7)}, 82 }} 83 u2 := &userMeta{ 84 Consumption: NewInt(0), 85 Hold: NewInt(0), 86 Reputation: NewInt(456), 87 LastSettledRound: 4, 88 LastDonationRound: 5, 89 Unsettled: []Donation{ 90 {Pid: post2, Amount: NewInt(6), Impact: NewInt(11)}, 91 }} 92 store.SetUserMeta(user1, u2) 93 store.SetUserMeta(user1, u1) 94 store.SetUserMeta(user2, u2) 95 defer func() { 96 suite.Equal(u1, store.GetUserMeta(user1)) 97 suite.Equal(u2, store.GetUserMeta(user2)) 98 }() 99 100 round1 := &roundMeta{ 101 Result: []Pid{"123", "4ed6", "xzz"}, 102 SumIF: NewInt(33333), 103 StartAt: 324324, 104 TopN: nil, 105 } 106 round2 := &roundMeta{ 107 Result: []Pid{"xzz"}, 108 SumIF: NewInt(234134), 109 StartAt: 342, 110 TopN: []PostIFPair{ 111 { 112 Pid: post1, 113 SumIF: NewInt(234235311), 114 }, 115 }, 116 } 117 store.SetRoundMeta(3, round2) 118 store.SetRoundMeta(3, round1) 119 store.SetRoundMeta(4, round2) 120 defer func() { 121 suite.Equal(round1, store.GetRoundMeta(3)) 122 suite.Equal(round2, store.GetRoundMeta(4)) 123 }() 124 125 rp1 := &roundPostMeta{ 126 SumIF: NewInt(342), 127 } 128 rp2 := &roundPostMeta{ 129 SumIF: NewInt(666), 130 } 131 132 store.SetRoundPostMeta(123, post1, rp2) 133 store.SetRoundPostMeta(123, post1, rp1) 134 store.SetRoundPostMeta(342, post1, rp2) 135 defer func() { 136 suite.Equal(rp1, store.GetRoundPostMeta(123, post1)) 137 suite.Equal(rp2, store.GetRoundPostMeta(342, post1)) 138 }() 139 140 store.SetGameMeta(&gameMeta{CurrentRound: 33}) 141 store.SetGameMeta(&gameMeta{CurrentRound: 443}) 142 defer func() { 143 suite.Equal(RoundId(443), store.GetCurrentRound()) 144 suite.Equal(&gameMeta{CurrentRound: 443}, store.GetGameMeta()) 145 }() 146 } 147 148 func (suite *StoreTestSuite) TestStoreImportExporter() { 149 store := NewReputationStore(suite.mockDB, DefaultInitialReputation) 150 151 var user1 Uid = "test" 152 var user2 Uid = "test2" 153 var post1 Pid = "post1" 154 var post2 Pid = "post2" 155 156 u1 := &userMeta{ 157 Reputation: NewInt(123), 158 LastSettledRound: 3, 159 LastDonationRound: 3, 160 Unsettled: []Donation{ 161 {Pid: post1, Amount: NewInt(3), Impact: NewInt(2)}, 162 {Pid: post2, Amount: NewInt(4), Impact: NewInt(7)}, 163 }} 164 u2 := &userMeta{ 165 Reputation: NewInt(456), 166 LastSettledRound: 4, 167 LastDonationRound: 5, 168 Unsettled: []Donation{ 169 {Pid: post2, Amount: NewInt(6), Impact: NewInt(11)}, 170 }} 171 store.SetUserMeta(user1, u1) 172 store.SetUserMeta(user2, u2) 173 174 // export data 175 data := store.Export() 176 db2 := internal.NewMockStore() 177 store2 := NewReputationStore(db2, DefaultInitialReputation) 178 store2.Import(data) 179 suite.Equal(u1.Reputation, store2.GetUserMeta(user1).Reputation) 180 suite.Equal(u2.Reputation, store2.GetUserMeta(user2).Reputation) 181 suite.Equal(RoundId(0), store2.GetUserMeta(user1).LastDonationRound) 182 suite.Equal(RoundId(0), store2.GetUserMeta(user2).LastDonationRound) 183 suite.Equal(RoundId(0), store2.GetUserMeta(user1).LastSettledRound) 184 suite.Equal(RoundId(0), store2.GetUserMeta(user2).LastSettledRound) 185 suite.Empty(store2.GetUserMeta(user1).Unsettled) 186 suite.Empty(store2.GetUserMeta(user2).Unsettled) 187 } 188 189 func (suite *StoreTestSuite) TestStoreImportExporterFromUpgrade1() { 190 store := NewReputationStore(suite.mockDB, DefaultInitialReputation) 191 192 var user1 Uid = "test" 193 u1 := &userMeta{ 194 Reputation: NewInt(100000), 195 LastSettledRound: 3, 196 LastDonationRound: 3, 197 Unsettled: []Donation{}} 198 store.SetUserMeta(user1, u1) 199 200 // export data 201 data := store.Export() 202 data.Reputations[0].IsMiniDollar = false 203 204 db2 := internal.NewMockStore() 205 store2 := NewReputationStore(db2, DefaultInitialReputation) 206 store2.Import(data) 207 u1.Reputation.Mul(NewInt(1200)) 208 suite.Equal(u1.Reputation, store2.GetUserMeta(user1).Reputation) 209 }