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

142 lines
3.9 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
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
2019-12-05 13:53:10 +00:00
"github.com/kava-labs/kava/app"
"github.com/kava-labs/kava/x/auction/keeper"
"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, abci.Header{})
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
2019-11-25 19:46:02 +00:00
id := types.ID(5)
2019-12-28 18:46:53 +00:00
auction := types.NewForwardAuction("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)
2019-11-25 19:46:02 +00:00
// check auction is in queue
2019-12-21 01:04:04 +00:00
// iter := keeper.GetQueueIterator(ctx, 100000)
// require.Equal(t, 1, len(convertIteratorToSlice(keeper, iter)))
// iter.Close()
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 queue
2019-12-21 01:04:04 +00:00
// iter = keeper.GetQueueIterator(ctx, 100000)
// require.Equal(t, 0, len(convertIteratorToSlice(keeper, iter)))
// iter.Close()
}
func TestIncrementNextAuctionID(t *testing.T) {
// setup keeper
tApp := app.NewTestApp()
keeper := tApp.GetAuctionKeeper()
ctx := tApp.NewContext(true, abci.Header{})
// store id
id := types.ID(123456)
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
}
2019-12-21 01:04:04 +00:00
// func TestIterateAuctions(t *testing.T) {
// // setup keeper
// tApp := app.NewTestApp()
// keeper := tApp.GetAuctionKeeper()
// ctx := tApp.NewContext(true, abci.Header{})
// auctions := []types.Auction{
// &types.ForwardAuction{},
// }
// for _, a := range auctions {
// keeper.SetAuction(ctx, a)
// }
// var readAuctions []types.Auction
// keeper.IterateAuctions(ctx, func(a types.Auction) bool {
// readAuctions = append(readAuctions, a)
// return false
// })
// require.Equal(t, auctions, readAuctions)
// }
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, abci.Header{})
2019-12-21 01:04:04 +00:00
// create a list of times
queue := []struct {
endTime time.Time
2019-11-25 19:46:02 +00:00
auctionID types.ID
2019-12-21 01:04:04 +00:00
}{
{time.Date(84, time.January, 1, 0, 0, 0, 0, time.UTC), 34345345},
{time.Date(98, time.January, 2, 0, 0, 0, 0, time.UTC), 5},
{time.Date(98, time.January, 2, 13, 5, 0, 0, time.UTC), 6},
{time.Date(98, time.January, 2, 16, 0, 0, 0, time.UTC), 1},
{time.Date(98, time.January, 2, 16, 0, 0, 0, time.UTC), 3},
{time.Date(98, time.January, 2, 16, 0, 0, 0, time.UTC), 4},
{time.Date(98, time.January, 2, 16, 0, 0, 1, time.UTC), 0}, // TODO tidy up redundant entries
}
cutoffTime := time.Date(98, time.January, 2, 16, 0, 0, 0, time.UTC)
var expectedQueue []types.ID
for _, i := range queue {
if i.endTime.After(cutoffTime) { // only append items where endTime ≤ cutoffTime
break
}
expectedQueue = append(expectedQueue, i.auctionID)
2019-11-25 19:46:02 +00:00
}
// write and read queue
2019-12-21 01:04:04 +00:00
for _, v := range queue {
2019-12-05 13:53:10 +00:00
keeper.InsertIntoQueue(ctx, v.endTime, v.auctionID)
2019-11-25 19:46:02 +00:00
}
2019-12-21 01:04:04 +00:00
var readQueue []types.ID
keeper.IterateAuctionsByTime(ctx, cutoffTime, func(id types.ID) bool {
readQueue = append(readQueue, id)
return false
})
2019-11-25 19:46:02 +00:00
2019-12-21 01:04:04 +00:00
require.Equal(t, expectedQueue, readQueue)
2019-11-25 19:46:02 +00:00
}
2019-12-05 13:53:10 +00:00
func convertIteratorToSlice(keeper keeper.Keeper, iterator sdk.Iterator) []types.ID {
2019-11-25 19:46:02 +00:00
var queue []types.ID
for ; iterator.Valid(); iterator.Next() {
var auctionID types.ID
2019-12-05 13:53:10 +00:00
types.ModuleCdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &auctionID)
2019-11-25 19:46:02 +00:00
queue = append(queue, auctionID)
}
return queue
}