github.com/lino-network/lino@v0.6.11/x/price/model/store_test.go (about) 1 package model 2 3 import ( 4 // "fmt" 5 "testing" 6 "time" 7 8 sdk "github.com/cosmos/cosmos-sdk/types" 9 "github.com/stretchr/testify/suite" 10 11 "github.com/lino-network/lino/testsuites" 12 "github.com/lino-network/lino/testutils" 13 linotypes "github.com/lino-network/lino/types" 14 ) 15 16 var ( 17 storeKeyStr = "testPriceStore" 18 kvStoreKey = sdk.NewKVStoreKey(storeKeyStr) 19 ) 20 21 type PriceStoreDumper struct{} 22 23 func (dumper PriceStoreDumper) NewDumper() *testutils.Dumper { 24 return NewPriceDumper(NewPriceStorage(kvStoreKey)) 25 } 26 27 type priceStoreTestSuite struct { 28 testsuites.GoldenTestSuite 29 store PriceStorage 30 } 31 32 func NewPriceStoreTestSuite() *priceStoreTestSuite { 33 return &priceStoreTestSuite{ 34 GoldenTestSuite: testsuites.NewGoldenTestSuite(PriceStoreDumper{}, kvStoreKey), 35 } 36 } 37 38 func (suite *priceStoreTestSuite) SetupTest() { 39 suite.SetupCtx(0, time.Unix(0, 0), kvStoreKey) 40 suite.store = NewPriceStorage(kvStoreKey) 41 } 42 43 func TestPriceStoreSuite(t *testing.T) { 44 suite.Run(t, NewPriceStoreTestSuite()) 45 } 46 47 func (suite *priceStoreTestSuite) TestGetSetFedPrice() { 48 store := suite.store 49 ctx := suite.Ctx 50 user1 := linotypes.AccountKey("user1") 51 user2 := linotypes.AccountKey("user2") 52 time1 := time.Unix(1000, 0) 53 time2 := time.Unix(2000, 0) 54 fed1 := &FedPrice{ 55 Validator: user1, 56 Price: linotypes.NewMiniDollar(123), 57 UpdateAt: time1.Unix(), 58 } 59 fed2 := &FedPrice{ 60 Validator: user2, 61 Price: linotypes.NewMiniDollar(456), 62 UpdateAt: time2.Unix(), 63 } 64 _, err := store.GetFedPrice(ctx, user1) 65 suite.Require().NotNil(err) 66 67 store.SetFedPrice(ctx, fed1) 68 store.SetFedPrice(ctx, fed2) 69 70 price, err := store.GetFedPrice(ctx, user1) 71 suite.Require().Nil(err) 72 suite.Equal(fed1, price) 73 price, err = store.GetFedPrice(ctx, user2) 74 suite.Require().Nil(err) 75 suite.Equal(fed2, price) 76 77 suite.Golden() 78 } 79 80 func (suite *priceStoreTestSuite) TestGetSetPriceHistory() { 81 store := suite.store 82 ctx := suite.Ctx 83 time1 := time.Unix(1000, 0) 84 time2 := time.Unix(2000, 0) 85 time3 := time.Unix(3000, 0) 86 tp1 := TimePrice{ 87 Price: linotypes.NewMiniDollar(123), 88 UpdateAt: time1.Unix(), 89 } 90 tp2 := TimePrice{ 91 Price: linotypes.NewMiniDollar(456), 92 UpdateAt: time2.Unix(), 93 } 94 tp3 := TimePrice{ 95 Price: linotypes.NewMiniDollar(789), 96 UpdateAt: time3.Unix(), 97 } 98 99 history1 := []TimePrice{tp1, tp2} 100 history2 := []TimePrice{tp1, tp2, tp3} 101 history3 := []TimePrice{tp2, tp3} 102 103 rst := store.GetPriceHistory(ctx) 104 suite.Require().Equal(0, len(rst)) 105 106 store.SetPriceHistory(ctx, history1) 107 rst = store.GetPriceHistory(ctx) 108 suite.Equal(history1, rst) 109 110 store.SetPriceHistory(ctx, history2) 111 rst = store.GetPriceHistory(ctx) 112 suite.Equal(history2, rst) 113 114 store.SetPriceHistory(ctx, history3) 115 rst = store.GetPriceHistory(ctx) 116 suite.Equal(history3, rst) 117 118 suite.Golden() 119 } 120 121 func (suite *priceStoreTestSuite) TestGetSetCurrentPrice() { 122 store := suite.store 123 ctx := suite.Ctx 124 time1 := time.Unix(1000, 0) 125 time2 := time.Unix(2000, 0) 126 tp1 := &TimePrice{ 127 Price: linotypes.NewMiniDollar(123), 128 UpdateAt: time1.Unix(), 129 } 130 tp2 := &TimePrice{ 131 Price: linotypes.NewMiniDollar(456), 132 UpdateAt: time2.Unix(), 133 } 134 135 _, err := store.GetCurrentPrice(ctx) 136 suite.Require().NotNil(err) 137 138 store.SetCurrentPrice(ctx, tp1) 139 rst, err := store.GetCurrentPrice(ctx) 140 suite.Require().Nil(err) 141 suite.Require().Equal(tp1, rst) 142 143 store.SetCurrentPrice(ctx, tp2) 144 rst, err = store.GetCurrentPrice(ctx) 145 suite.Require().Nil(err) 146 suite.Require().Equal(tp2, rst) 147 148 suite.Golden() 149 } 150 151 func (suite *priceStoreTestSuite) TestGetSetLastValidators() { 152 store := suite.store 153 ctx := suite.Ctx 154 eg1 := []linotypes.AccountKey{"user1", "user2"} 155 eg2 := []linotypes.AccountKey{"user3", "user4"} 156 157 last := store.GetLastValidators(ctx) 158 suite.Equal(0, len(last)) 159 160 store.SetLastValidators(ctx, eg1) 161 suite.Equal(eg1, store.GetLastValidators(ctx)) 162 store.SetLastValidators(ctx, eg2) 163 suite.Equal(eg2, store.GetLastValidators(ctx)) 164 165 suite.Golden() 166 } 167 168 func (suite *priceStoreTestSuite) TestGetSetFeedHistory() { 169 store := suite.store 170 ctx := suite.Ctx 171 record1 := FeedHistory{ 172 Price: linotypes.NewMiniDollar(1235), 173 Feeded: []FedRecord{ 174 { 175 Validator: "val1", 176 Price: linotypes.NewMiniDollar(4567), 177 Power: linotypes.NewCoinFromInt64(1000), 178 UpdateAt: 1234, 179 }, 180 { 181 Validator: "val2", 182 Price: linotypes.NewMiniDollar(1235), 183 Power: linotypes.NewCoinFromInt64(1000), 184 UpdateAt: 1255, 185 }, 186 { 187 Validator: "val3", 188 Price: linotypes.NewMiniDollar(9999), 189 Power: linotypes.NewCoinFromInt64(1000), 190 UpdateAt: 1299, 191 }, 192 }, 193 UpdateAt: 12345, 194 } 195 record2 := record1 196 record2.Feeded[1].Price = linotypes.NewMiniDollar(7777) 197 record2.Price = linotypes.NewMiniDollar(7777) 198 record2.UpdateAt = 567989 199 200 history1 := []FeedHistory{record1} 201 history2 := []FeedHistory{record1, record2} 202 203 history := store.GetFeedHistory(ctx) 204 suite.Equal([]FeedHistory(nil), history) 205 206 store.SetFeedHistory(ctx, history1) 207 suite.Equal(history1, store.GetFeedHistory(ctx)) 208 store.SetFeedHistory(ctx, history2) 209 suite.Equal(history2, store.GetFeedHistory(ctx)) 210 211 suite.Golden() 212 }