From 0d4e8ded76dcde18bc8d4552a3188bcf5b69c0c5 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Mon, 6 Nov 2023 14:12:33 -0600 Subject: [PATCH] fix(epochs): fix epoch tests broken by adding month epoch into default genesis --- x/epochs/genesis_test.go | 73 +++++++++++++++++------------- x/epochs/keeper/grpc_query_test.go | 2 +- x/epochs/keeper/keeper_test.go | 14 +++--- x/epochs/types/epochinfo_test.go | 9 ++++ 4 files changed, 59 insertions(+), 39 deletions(-) diff --git a/x/epochs/genesis_test.go b/x/epochs/genesis_test.go index 1f4b14cb2..2fc851929 100644 --- a/x/epochs/genesis_test.go +++ b/x/epochs/genesis_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/NibiruChain/nibiru/app" @@ -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) { @@ -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{ @@ -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) } diff --git a/x/epochs/keeper/grpc_query_test.go b/x/epochs/keeper/grpc_query_test.go index d8d820757..c54e66fd7 100644 --- a/x/epochs/keeper/grpc_query_test.go +++ b/x/epochs/keeper/grpc_query_test.go @@ -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) diff --git a/x/epochs/keeper/keeper_test.go b/x/epochs/keeper/keeper_test.go index 74e11e5dd..046e6d630 100644 --- a/x/epochs/keeper/keeper_test.go +++ b/x/epochs/keeper/keeper_test.go @@ -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" @@ -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{}, @@ -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) { diff --git a/x/epochs/types/epochinfo_test.go b/x/epochs/types/epochinfo_test.go index f6e5d3d6f..a3624337a 100644 --- a/x/epochs/types/epochinfo_test.go +++ b/x/epochs/types/epochinfo_test.go @@ -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