0g-chain/x/auction/keeper/keeper_test.go

136 lines
4.4 KiB
Go
Raw Normal View History

2019-12-05 13:53:10 +00:00
package keeper_test
2019-11-25 19:46:02 +00:00
import (
"testing"
2019-12-21 01:04:04 +00:00
"time"
2019-11-25 19:46:02 +00:00
"github.com/stretchr/testify/require"
2020-04-30 14:23:41 +00:00
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
2019-12-05 13:53:10 +00:00
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/auction/types"
2019-11-25 19:46:02 +00:00
)
2019-12-21 01:04:04 +00:00
func SetGetDeleteAuction(t *testing.T) {
2019-11-25 19:46:02 +00:00
// setup keeper, create auction
2019-12-05 13:53:10 +00:00
tApp := app.NewTestApp()
keeper := tApp.GetAuctionKeeper()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
2019-12-21 01:04:04 +00:00
someTime := time.Date(43, time.January, 1, 0, 0, 0, 0, time.UTC) // need to specify UTC as tz info is lost on unmarshal
2020-01-01 14:11:19 +00:00
var id uint64 = 5
2020-01-09 16:09:19 +00:00
auction := types.NewSurplusAuction("some_module", c("usdx", 100), "kava", someTime).WithID(id)
2019-11-25 19:46:02 +00:00
// write and read from store
2019-12-28 18:46:53 +00:00
keeper.SetAuction(ctx, auction)
2019-11-25 19:46:02 +00:00
readAuction, found := keeper.GetAuction(ctx, id)
// check before and after match
require.True(t, found)
2019-12-28 18:46:53 +00:00
require.Equal(t, auction, readAuction)
// check auction is in the index
2020-01-01 14:11:19 +00:00
keeper.IterateAuctionsByTime(ctx, auction.GetEndTime(), func(readID uint64) bool {
require.Equal(t, auction.GetID(), readID)
return false
})
2019-11-25 19:46:02 +00:00
// delete auction
2019-12-05 13:53:10 +00:00
keeper.DeleteAuction(ctx, id)
2019-11-25 19:46:02 +00:00
// check auction does not exist
_, found = keeper.GetAuction(ctx, id)
require.False(t, found)
// check auction not in index
2020-01-01 14:11:19 +00:00
keeper.IterateAuctionsByTime(ctx, time.Unix(999999999, 0), func(readID uint64) bool {
require.Fail(t, "index should be empty", " found auction ID '%s", readID)
return false
})
2019-12-21 01:04:04 +00:00
}
func TestIncrementNextAuctionID(t *testing.T) {
// setup keeper
tApp := app.NewTestApp()
keeper := tApp.GetAuctionKeeper()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
2019-12-21 01:04:04 +00:00
// store id
2020-01-01 14:11:19 +00:00
var id uint64 = 123456
2019-12-21 01:04:04 +00:00
keeper.SetNextAuctionID(ctx, id)
require.NoError(t, keeper.IncrementNextAuctionID(ctx))
// check id was incremented
readID, err := keeper.GetNextAuctionID(ctx)
require.NoError(t, err)
require.Equal(t, id+1, readID)
2019-11-25 19:46:02 +00:00
}
func TestIterateAuctions(t *testing.T) {
// setup
tApp := app.NewTestApp()
tApp.InitializeFromGenesisStates()
keeper := tApp.GetAuctionKeeper()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
2019-12-21 01:04:04 +00:00
auctions := []types.Auction{
2020-01-09 16:09:19 +00:00
types.NewSurplusAuction("sellerMod", c("denom", 12345678), "anotherdenom", time.Date(1998, time.January, 1, 0, 0, 0, 0, time.UTC)).WithID(0),
2020-01-12 14:17:47 +00:00
types.NewDebtAuction("buyerMod", c("denom", 12345678), c("anotherdenom", 12345678), time.Date(1998, time.January, 1, 0, 0, 0, 0, time.UTC), c("debt", 12345678)).WithID(1),
types.NewCollateralAuction("sellerMod", c("denom", 12345678), time.Date(1998, time.January, 1, 0, 0, 0, 0, time.UTC), c("anotherdenom", 12345678), types.WeightedAddresses{}, c("debt", 12345678)).WithID(2),
}
for _, a := range auctions {
keeper.SetAuction(ctx, a)
}
2019-12-21 01:04:04 +00:00
// run
var readAuctions []types.Auction
keeper.IterateAuctions(ctx, func(a types.Auction) bool {
readAuctions = append(readAuctions, a)
return false
})
2019-12-21 01:04:04 +00:00
// check
require.Equal(t, auctions, readAuctions)
}
2019-12-21 01:04:04 +00:00
func TestIterateAuctionsByTime(t *testing.T) {
2019-11-25 19:46:02 +00:00
// setup keeper
2019-12-05 13:53:10 +00:00
tApp := app.NewTestApp()
keeper := tApp.GetAuctionKeeper()
ctx := tApp.NewContext(true, tmproto.Header{Height: 1})
2019-12-05 13:53:10 +00:00
2019-12-28 22:00:04 +00:00
// setup byTime index
byTimeIndex := []struct {
2019-12-21 01:04:04 +00:00
endTime time.Time
2020-01-01 14:11:19 +00:00
auctionID uint64
2019-12-21 01:04:04 +00:00
}{
2019-12-28 22:00:04 +00:00
{time.Date(0, time.January, 1, 0, 0, 0, 0, time.UTC), 9999}, // distant past
{time.Date(1998, time.January, 1, 11, 59, 59, 999999999, time.UTC), 1}, // just before cutoff
{time.Date(1998, time.January, 1, 11, 59, 59, 999999999, time.UTC), 2}, //
{time.Date(1998, time.January, 1, 12, 0, 0, 0, time.UTC), 3}, // equal to cutoff
{time.Date(1998, time.January, 1, 12, 0, 0, 0, time.UTC), 4}, //
{time.Date(1998, time.January, 1, 12, 0, 0, 1, time.UTC), 5}, // just after cutoff
{time.Date(1998, time.January, 1, 12, 0, 0, 1, time.UTC), 6}, //
{time.Date(9999, time.January, 1, 0, 0, 0, 0, time.UTC), 0}, // distant future
}
for _, v := range byTimeIndex {
2020-01-10 14:08:47 +00:00
keeper.InsertIntoByTimeIndex(ctx, v.endTime, v.auctionID)
2019-12-21 01:04:04 +00:00
}
2019-12-28 22:00:04 +00:00
// read out values from index up to a cutoff time and check they are as expected
cutoffTime := time.Date(1998, time.January, 1, 12, 0, 0, 0, time.UTC)
2020-01-01 14:11:19 +00:00
var expectedIndex []uint64
2019-12-28 22:00:04 +00:00
for _, v := range byTimeIndex {
if v.endTime.Before(cutoffTime) || v.endTime.Equal(cutoffTime) { // endTime ≤ cutoffTime
expectedIndex = append(expectedIndex, v.auctionID)
2019-12-21 01:04:04 +00:00
}
2019-11-25 19:46:02 +00:00
}
2020-01-01 14:11:19 +00:00
var readIndex []uint64
keeper.IterateAuctionsByTime(ctx, cutoffTime, func(id uint64) bool {
2019-12-28 22:00:04 +00:00
readIndex = append(readIndex, id)
2019-12-21 01:04:04 +00:00
return false
})
2019-11-25 19:46:02 +00:00
2019-12-28 22:00:04 +00:00
require.Equal(t, expectedIndex, readIndex)
2019-11-25 19:46:02 +00:00
}