From 7feb7f4ae9381862ee4d5995ad2bfa04f2a05a35 Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Fri, 7 Apr 2023 16:47:40 +0900 Subject: [PATCH] Update the test --- x/foundation/keeper/internal/abci_test.go | 48 +++++++++-------- x/foundation/keeper/internal/treasury_test.go | 51 +++++++++++++++++++ 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/x/foundation/keeper/internal/abci_test.go b/x/foundation/keeper/internal/abci_test.go index 274c5e0375..0af7903346 100644 --- a/x/foundation/keeper/internal/abci_test.go +++ b/x/foundation/keeper/internal/abci_test.go @@ -7,28 +7,36 @@ import ( ) func (s *KeeperTestSuite) TestBeginBlocker() { - ctx, _ := s.ctx.CacheContext() - - taxRatio := sdk.MustNewDecFromStr("0.123456789") - s.impl.SetParams(ctx, foundation.Params{ - FoundationTax: taxRatio, - }) - - before := s.impl.GetTreasury(ctx) - s.Require().Equal(1, len(before)) - s.Require().Equal(sdk.NewDecFromInt(s.balance), before[0].Amount) - - // collect - internal.BeginBlocker(ctx, s.impl) + for name, tc := range map[string]struct { + taxRatio sdk.Dec + valid bool + }{ + "valid ratio": { + taxRatio: sdk.OneDec(), + valid: true, + }, + "ratio > 1": { + taxRatio: sdk.MustNewDecFromStr("1.00000001"), + }, + } { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() - tax := sdk.NewDecFromInt(s.balance).MulTruncate(taxRatio).TruncateInt() - // ensure the behavior does not change - s.Require().Equal(sdk.NewInt(121932631), tax) + s.impl.SetParams(ctx, foundation.Params{ + FoundationTax: tc.taxRatio, + }) - expectedAfter := s.balance.Add(tax) - after := s.impl.GetTreasury(ctx) - s.Require().Equal(1, len(after)) - s.Require().Equal(sdk.NewDecFromInt(expectedAfter), after[0].Amount) + // collect + testing := func() { + internal.BeginBlocker(ctx, s.impl) + } + if tc.valid { + s.Require().NotPanics(testing) + } else { + s.Require().Panics(testing) + } + }) + } } func (s *KeeperTestSuite) TestEndBlocker() { diff --git a/x/foundation/keeper/internal/treasury_test.go b/x/foundation/keeper/internal/treasury_test.go index 59e729e375..3609fe3c5c 100644 --- a/x/foundation/keeper/internal/treasury_test.go +++ b/x/foundation/keeper/internal/treasury_test.go @@ -6,6 +6,57 @@ import ( "github.com/line/lbm-sdk/x/foundation" ) +func (s *KeeperTestSuite) TestCollectFoundationTax() { + for name, tc := range map[string]struct { + taxRatio sdk.Dec + tax sdk.Int + valid bool + }{ + "common": { + taxRatio: sdk.MustNewDecFromStr("0.123456789"), + tax: sdk.NewInt(121932631), + valid: true, + }, + "zero ratio": { + taxRatio: sdk.ZeroDec(), + tax: sdk.ZeroInt(), + valid: true, + }, + "send fails": { + taxRatio: sdk.MustNewDecFromStr("1.00000001"), + tax: sdk.NewInt(987654330), + }, + } { + s.Run(name, func() { + ctx, _ := s.ctx.CacheContext() + + s.impl.SetParams(ctx, foundation.Params{ + FoundationTax: tc.taxRatio, + }) + + before := s.impl.GetTreasury(ctx) + s.Require().Equal(1, len(before)) + s.Require().Equal(sdk.NewDecFromInt(s.balance), before[0].Amount) + + tax := sdk.NewDecFromInt(s.balance).MulTruncate(tc.taxRatio).TruncateInt() + // ensure the behavior does not change + s.Require().Equal(tc.tax, tax) + + err := s.impl.CollectFoundationTax(ctx) + if !tc.valid { + s.Require().Error(err) + return + } + s.Require().NoError(err) + + expectedAfter := s.balance.Add(tax) + after := s.impl.GetTreasury(ctx) + s.Require().Equal(1, len(after)) + s.Require().Equal(sdk.NewDecFromInt(expectedAfter), after[0].Amount) + }) + } +} + func (s *KeeperTestSuite) TestFundTreasury() { testCases := map[string]struct { amount sdk.Int