|
| 1 | +package epochs_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + storetypes "cosmossdk.io/store/types" |
| 10 | + |
| 11 | + "github.com/cosmos/cosmos-sdk/runtime" |
| 12 | + "github.com/cosmos/cosmos-sdk/types/module/testutil" |
| 13 | + "github.com/cosmos/cosmos-sdk/x/epochs" |
| 14 | + "github.com/cosmos/cosmos-sdk/x/epochs/keeper" |
| 15 | + "github.com/cosmos/cosmos-sdk/x/epochs/types" |
| 16 | +) |
| 17 | + |
| 18 | +type testEpochHooks struct{} |
| 19 | + |
| 20 | +func (h testEpochHooks) AfterEpochEnd(ctx context.Context, epochIdentifier string, epochNumber int64) error { |
| 21 | + return nil |
| 22 | +} |
| 23 | + |
| 24 | +func (h testEpochHooks) BeforeEpochStart(ctx context.Context, epochIdentifier string, epochNumber int64) error { |
| 25 | + return nil |
| 26 | +} |
| 27 | + |
| 28 | +func TestInvokeSetHooks(t *testing.T) { |
| 29 | + // Create a mock keeper |
| 30 | + key := storetypes.NewKVStoreKey(types.StoreKey) |
| 31 | + storeService := runtime.NewKVStoreService(key) |
| 32 | + encCfg := testutil.MakeTestEncodingConfig() |
| 33 | + mockKeeper := keeper.NewKeeper(storeService, encCfg.Codec) |
| 34 | + |
| 35 | + // Create mock hooks |
| 36 | + hook1 := types.EpochHooksWrapper{ |
| 37 | + EpochHooks: testEpochHooks{}, |
| 38 | + } |
| 39 | + hook2 := types.EpochHooksWrapper{ |
| 40 | + EpochHooks: testEpochHooks{}, |
| 41 | + } |
| 42 | + hooks := map[string]types.EpochHooksWrapper{ |
| 43 | + "moduleA": hook1, |
| 44 | + "moduleB": hook2, |
| 45 | + } |
| 46 | + |
| 47 | + // Call InvokeSetHooks |
| 48 | + err := epochs.InvokeSetHooks(&mockKeeper, hooks) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + // Verify that hooks were set correctly |
| 52 | + require.NotNil(t, mockKeeper.Hooks()) |
| 53 | + require.IsType(t, types.MultiEpochHooks{}, mockKeeper.Hooks()) |
| 54 | + |
| 55 | + // Verify the order of hooks (lexical order by module name) |
| 56 | + multiHooks := mockKeeper.Hooks().(types.MultiEpochHooks) |
| 57 | + require.Equal(t, 2, len(multiHooks)) |
| 58 | + require.Equal(t, hook1, multiHooks[0]) |
| 59 | + require.Equal(t, hook2, multiHooks[1]) |
| 60 | +} |
0 commit comments