mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
110 lines
2.7 KiB
Go
110 lines
2.7 KiB
Go
|
package auction_test
|
||
|
|
||
|
import (
|
||
|
"sort"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/stretchr/testify/require"
|
||
|
|
||
|
abci "github.com/tendermint/tendermint/abci/types"
|
||
|
|
||
|
"github.com/kava-labs/kava/app"
|
||
|
"github.com/kava-labs/kava/x/auction"
|
||
|
)
|
||
|
|
||
|
var testTime = time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC)
|
||
|
var testAuction = auction.NewCollateralAuction(
|
||
|
"seller",
|
||
|
c("lotdenom", 10),
|
||
|
testTime,
|
||
|
c("biddenom", 1000),
|
||
|
auction.WeightedAddresses{},
|
||
|
c("debt", 1000),
|
||
|
).WithID(3).(auction.GenesisAuction)
|
||
|
|
||
|
func TestInitGenesis(t *testing.T) {
|
||
|
t.Run("valid", func(t *testing.T) {
|
||
|
// setup keepers
|
||
|
tApp := app.NewTestApp()
|
||
|
keeper := tApp.GetAuctionKeeper()
|
||
|
ctx := tApp.NewContext(true, abci.Header{})
|
||
|
// create genesis
|
||
|
gs := auction.NewGenesisState(
|
||
|
10,
|
||
|
auction.DefaultParams(),
|
||
|
auction.GenesisAuctions{testAuction},
|
||
|
)
|
||
|
|
||
|
// run init
|
||
|
require.NotPanics(t, func() {
|
||
|
auction.InitGenesis(ctx, keeper, tApp.GetSupplyKeeper(), gs)
|
||
|
})
|
||
|
|
||
|
// check state is as expected
|
||
|
actualID, err := keeper.GetNextAuctionID(ctx)
|
||
|
require.NoError(t, err)
|
||
|
require.Equal(t, gs.NextAuctionID, actualID)
|
||
|
|
||
|
require.Equal(t, gs.Params, keeper.GetParams(ctx))
|
||
|
|
||
|
// TODO is there a nicer way of comparing state?
|
||
|
sort.Slice(gs.Auctions, func(i, j int) bool {
|
||
|
return gs.Auctions[i].GetID() > gs.Auctions[j].GetID()
|
||
|
})
|
||
|
i := 0
|
||
|
keeper.IterateAuctions(ctx, func(a auction.Auction) bool {
|
||
|
require.Equal(t, gs.Auctions[i], a)
|
||
|
i++
|
||
|
return false
|
||
|
})
|
||
|
})
|
||
|
t.Run("invalid", func(t *testing.T) {
|
||
|
// setup keepers
|
||
|
tApp := app.NewTestApp()
|
||
|
ctx := tApp.NewContext(true, abci.Header{})
|
||
|
|
||
|
// create invalid genesis
|
||
|
gs := auction.NewGenesisState(
|
||
|
0, // next id < testAuction ID
|
||
|
auction.DefaultParams(),
|
||
|
auction.GenesisAuctions{testAuction},
|
||
|
)
|
||
|
|
||
|
// check init fails
|
||
|
require.Panics(t, func() {
|
||
|
auction.InitGenesis(ctx, tApp.GetAuctionKeeper(), tApp.GetSupplyKeeper(), gs)
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestExportGenesis(t *testing.T) {
|
||
|
t.Run("default", func(t *testing.T) {
|
||
|
// setup state
|
||
|
tApp := app.NewTestApp()
|
||
|
ctx := tApp.NewContext(true, abci.Header{})
|
||
|
tApp.InitializeFromGenesisStates()
|
||
|
|
||
|
// export
|
||
|
gs := auction.ExportGenesis(ctx, tApp.GetAuctionKeeper())
|
||
|
|
||
|
// check state matches
|
||
|
require.Equal(t, auction.DefaultGenesisState(), gs)
|
||
|
})
|
||
|
t.Run("one auction", func(t *testing.T) {
|
||
|
// setup state
|
||
|
tApp := app.NewTestApp()
|
||
|
ctx := tApp.NewContext(true, abci.Header{})
|
||
|
tApp.InitializeFromGenesisStates()
|
||
|
tApp.GetAuctionKeeper().SetAuction(ctx, testAuction)
|
||
|
|
||
|
// export
|
||
|
gs := auction.ExportGenesis(ctx, tApp.GetAuctionKeeper())
|
||
|
|
||
|
// check state matches
|
||
|
expectedGenesisState := auction.DefaultGenesisState()
|
||
|
expectedGenesisState.Auctions = append(expectedGenesisState.Auctions, testAuction)
|
||
|
require.Equal(t, expectedGenesisState, gs)
|
||
|
})
|
||
|
}
|