Skip to content

Commit

Permalink
Update the test
Browse files Browse the repository at this point in the history
  • Loading branch information
0Tech committed Apr 7, 2023
1 parent 5c5bbd8 commit 7feb7f4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 20 deletions.
48 changes: 28 additions & 20 deletions x/foundation/keeper/internal/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
51 changes: 51 additions & 0 deletions x/foundation/keeper/internal/treasury_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7feb7f4

Please sign in to comment.