Skip to content

Commit

Permalink
fix(epochs): fix epoch tests broken by adding month epoch into defaul…
Browse files Browse the repository at this point in the history
…t genesis
  • Loading branch information
k-yang committed Nov 6, 2023
1 parent 0a74da1 commit 0d4e8de
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 39 deletions.
73 changes: 41 additions & 32 deletions x/epochs/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/app"
Expand All @@ -26,32 +27,40 @@ func TestEpochsExportGenesis(t *testing.T) {
ctx := testapp.NewContext(app).WithBlockTime(chainStartTime)

genesis := epochs.ExportGenesis(ctx, app.EpochsKeeper)
require.Len(t, genesis.Epochs, 3)
require.Len(t, genesis.Epochs, 4)

errMsg := fmt.Sprintf("app.EpochsKeeper.AllEpochInfos(ctx): %v\n", app.EpochsKeeper.AllEpochInfos(ctx))
require.Equal(t, genesis.Epochs[0].Identifier, "30 min")
require.Equal(t, genesis.Epochs[0].StartTime, chainStartTime, errMsg)
require.Equal(t, genesis.Epochs[0].Duration, time.Minute*30, errMsg)
require.Equal(t, genesis.Epochs[0].CurrentEpoch, uint64(0))
require.Equal(t, genesis.Epochs[0].CurrentEpochStartHeight, int64(0))
require.Equal(t, genesis.Epochs[0].CurrentEpochStartTime, chainStartTime)
require.Equal(t, genesis.Epochs[0].EpochCountingStarted, false)

require.Equal(t, genesis.Epochs[1].Identifier, "day")
require.Equal(t, genesis.Epochs[1].StartTime, chainStartTime, errMsg)
require.Equal(t, genesis.Epochs[1].Duration, time.Hour*24)
require.Equal(t, genesis.Epochs[1].CurrentEpoch, uint64(0))
require.Equal(t, genesis.Epochs[1].CurrentEpochStartHeight, int64(0))
require.Equal(t, genesis.Epochs[1].CurrentEpochStartTime, chainStartTime, errMsg)
require.Equal(t, genesis.Epochs[1].EpochCountingStarted, false)

require.Equal(t, genesis.Epochs[2].Identifier, "week")
require.Equal(t, genesis.Epochs[2].StartTime, chainStartTime, errMsg)
require.Equal(t, genesis.Epochs[2].Duration, time.Hour*24*7)
require.Equal(t, genesis.Epochs[2].CurrentEpoch, uint64(0))
require.Equal(t, genesis.Epochs[2].CurrentEpochStartHeight, int64(0))
require.Equal(t, genesis.Epochs[2].CurrentEpochStartTime, chainStartTime, errMsg)
require.Equal(t, genesis.Epochs[2].EpochCountingStarted, false)
assert.EqualValues(t, genesis.Epochs[0].Identifier, "30 min")
assert.EqualValues(t, genesis.Epochs[0].StartTime, chainStartTime, errMsg)
assert.EqualValues(t, genesis.Epochs[0].Duration, time.Minute*30, errMsg)
assert.EqualValues(t, genesis.Epochs[0].CurrentEpoch, uint64(0))
assert.EqualValues(t, genesis.Epochs[0].CurrentEpochStartHeight, int64(0))
assert.EqualValues(t, genesis.Epochs[0].CurrentEpochStartTime, chainStartTime)
assert.EqualValues(t, genesis.Epochs[0].EpochCountingStarted, false)

assert.EqualValues(t, genesis.Epochs[1].Identifier, "day")
assert.EqualValues(t, genesis.Epochs[1].StartTime, chainStartTime, errMsg)
assert.EqualValues(t, genesis.Epochs[1].Duration, time.Hour*24)
assert.EqualValues(t, genesis.Epochs[1].CurrentEpoch, uint64(0))
assert.EqualValues(t, genesis.Epochs[1].CurrentEpochStartHeight, int64(0))
assert.EqualValues(t, genesis.Epochs[1].CurrentEpochStartTime, chainStartTime, errMsg)
assert.EqualValues(t, genesis.Epochs[1].EpochCountingStarted, false)

assert.EqualValues(t, genesis.Epochs[2].Identifier, "month")
assert.EqualValues(t, genesis.Epochs[2].StartTime, chainStartTime, errMsg)
assert.EqualValues(t, genesis.Epochs[2].Duration, time.Hour*24*30)
assert.EqualValues(t, genesis.Epochs[2].CurrentEpoch, uint64(0))
assert.EqualValues(t, genesis.Epochs[2].CurrentEpochStartHeight, int64(0))
assert.EqualValues(t, genesis.Epochs[2].CurrentEpochStartTime, chainStartTime, errMsg)
assert.EqualValues(t, genesis.Epochs[2].EpochCountingStarted, false)

assert.EqualValues(t, genesis.Epochs[3].Identifier, "week")
assert.EqualValues(t, genesis.Epochs[3].StartTime, chainStartTime, errMsg)
assert.EqualValues(t, genesis.Epochs[3].Duration, time.Hour*24*7)
assert.EqualValues(t, genesis.Epochs[3].CurrentEpoch, uint64(0))
assert.EqualValues(t, genesis.Epochs[3].CurrentEpochStartHeight, int64(0))
assert.EqualValues(t, genesis.Epochs[3].CurrentEpochStartTime, chainStartTime, errMsg)
assert.EqualValues(t, genesis.Epochs[3].EpochCountingStarted, false)
}

func TestEpochsInitGenesis(t *testing.T) {
Expand Down Expand Up @@ -94,7 +103,7 @@ func TestEpochsInitGenesis(t *testing.T) {
err := epochs.InitGenesis(ctx, app.EpochsKeeper, genesisState)
require.Error(t, err)

require.EqualError(t, genesisState.Validate(), "epoch identifier should be unique")
assert.EqualError(t, genesisState.Validate(), "epoch identifier should be unique")

genesisState = types.GenesisState{
Epochs: []types.EpochInfo{
Expand All @@ -114,11 +123,11 @@ func TestEpochsInitGenesis(t *testing.T) {
require.NoError(t, err)
epochInfo, err := app.EpochsKeeper.GetEpochInfo(ctx, "monthly")
require.NoError(t, err)
require.Equal(t, epochInfo.Identifier, "monthly")
require.Equal(t, epochInfo.StartTime.UTC().String(), now.UTC().String())
require.Equal(t, epochInfo.Duration, time.Hour*24)
require.Equal(t, epochInfo.CurrentEpoch, uint64(0))
require.Equal(t, epochInfo.CurrentEpochStartHeight, ctx.BlockHeight())
require.Equal(t, epochInfo.CurrentEpochStartTime.UTC().String(), time.Time{}.String())
require.Equal(t, epochInfo.EpochCountingStarted, true)
assert.EqualValues(t, epochInfo.Identifier, "monthly")
assert.EqualValues(t, epochInfo.StartTime.UTC().String(), now.UTC().String())
assert.EqualValues(t, epochInfo.Duration, time.Hour*24)
assert.EqualValues(t, epochInfo.CurrentEpoch, uint64(0))
assert.EqualValues(t, epochInfo.CurrentEpochStartHeight, ctx.BlockHeight())
assert.EqualValues(t, epochInfo.CurrentEpochStartTime.UTC().String(), time.Time{}.String())
assert.EqualValues(t, epochInfo.EpochCountingStarted, true)
}
2 changes: 1 addition & 1 deletion x/epochs/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestQueryEpochInfos(t *testing.T) {
gocontext.Background(), &epochstypes.QueryEpochInfosRequest{},
)
require.NoError(t, err, errMsg)
require.Len(t, epochInfosResponse.Epochs, 3)
require.Len(t, epochInfosResponse.Epochs, 4)

// check if EpochInfos are correct
require.Equal(t, epochInfosResponse.Epochs[0].StartTime, chainStartTime, errMsg)
Expand Down
14 changes: 8 additions & 6 deletions x/epochs/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/x/common/testutil/testapp"
Expand All @@ -14,9 +15,9 @@ func TestUpsertEpochInfo_HappyPath(t *testing.T) {
nibiruApp, ctx := testapp.NewNibiruTestAppAndContext()

epochInfo := types.EpochInfo{
Identifier: "monthly",
Identifier: "year",
StartTime: time.Time{},
Duration: time.Hour * 24 * 30,
Duration: time.Hour * 24 * 365,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
Expand All @@ -25,18 +26,19 @@ func TestUpsertEpochInfo_HappyPath(t *testing.T) {

nibiruApp.EpochsKeeper.Epochs.Insert(ctx, epochInfo.Identifier, epochInfo)

epochInfoSaved, err := nibiruApp.EpochsKeeper.GetEpochInfo(ctx, "monthly")
epochInfoSaved, err := nibiruApp.EpochsKeeper.GetEpochInfo(ctx, "year")
require.NoError(t, err)
require.Equal(t, epochInfo, epochInfoSaved)
assert.EqualValues(t, epochInfo, epochInfoSaved)

allEpochs := nibiruApp.EpochsKeeper.AllEpochInfos(ctx)

require.Len(t, allEpochs, 4)
require.Len(t, allEpochs, 5)
// Epochs are ordered in alphabetical order
require.Equal(t, "30 min", allEpochs[0].Identifier)
require.Equal(t, "day", allEpochs[1].Identifier)
require.Equal(t, "monthly", allEpochs[2].Identifier)
require.Equal(t, "month", allEpochs[2].Identifier)
require.Equal(t, "week", allEpochs[3].Identifier)
require.Equal(t, "year", allEpochs[4].Identifier)
}

func TestEpochExists(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions x/epochs/types/epochinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func TestDefaultGenesis(t *testing.T) {
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
},
{
Identifier: MonthEpochID,
StartTime: time.Time{},
Duration: 30 * 24 * time.Hour,
CurrentEpoch: 0,
CurrentEpochStartHeight: 0,
CurrentEpochStartTime: time.Time{},
EpochCountingStarted: false,
},
}

// Ensure that genState and expectedEpochs are the same
Expand Down

0 comments on commit 0d4e8de

Please sign in to comment.