Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(x/ecocredit): add tests for basket events #1408

Merged
merged 7 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion types/testutil/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ func MatchEvent(expected proto.Message, emitted sdk.Event) error {
// GetEvent searches through the sdk.Events and attempts to return the sdk.Event whose type matches
// the proto.MessageName of the passed msg.
func GetEvent(msg proto.Message, events []sdk.Event) (e sdk.Event, found bool) {
eventName := proto.MessageName(msg)
for _, e := range events {
if proto.MessageName(msg) == e.Type {
if eventName == e.Type {
return e, true
}
}
Expand Down
14 changes: 14 additions & 0 deletions x/ecocredit/server/basket/features/msg_create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,17 @@ Feature: Msg/Create
"""

# no failing scenario - response should always be empty when message execution fails

Rule: Event is emitted

Scenario: EventCreate is emitted
Given a credit type with abbreviation "C" and precision "6"
And alice's address "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6"
When alice attempts to create a basket with name "NCT"
Then expect event with properties
"""
{
"curator": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"basket_denom": "eco.uC.NCT"
}
"""
38 changes: 38 additions & 0 deletions x/ecocredit/server/basket/features/msg_put.feature
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,41 @@ Feature: Msg/Put
| precision non-zero, amount decimal | 6 | 2.5 | 2500000 |

# no failing scenario - response should always be empty when message execution fails

Rule: Events are emitted

Background:
Given a credit type with abbreviation "C" and precision "6"
And a basket with credit type "C"
And Alice's address "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6"
And Ecocredit module's address "regen1k82wewrfkhdmegw6uxrgwwzrsd7593t8tej2d5"
And Alice owns credit amount "2"

Scenario: EventTransfer is emitted
When alice attempts to put credit amount "2" into the basket
Then expect event transfer with properties
"""
{
"sender": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"recipient": "regen1k82wewrfkhdmegw6uxrgwwzrsd7593t8tej2d5",
"batch_denom": "C01-001-20200101-20210101-001",
"tradable_amount": "2"
}
"""

Scenario: EventPut is emitted
When alice attempts to put credit amount "2" into the basket
Then expect event put with properties
"""
{
"owner": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"basket_denom": "eco.uC.NCT",
"credits": [
{
"batch_denom": "C01-001-20200101-20210101-001",
"amount": "2"
}
],
"amount": "2000000"
}
"""
60 changes: 60 additions & 0 deletions x/ecocredit/server/basket/features/msg_take.feature
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,63 @@ Feature: Msg/Take
| precision non-zero, credits decimal | 6 | 2500000 | 2.500000 |

# no failing scenario - response should always be empty when message execution fails

Rule: Events are emitted

Background:
Given a credit type with abbreviation "C" and precision "6"
And a basket with credit type "C" and disable auto retire "true"
And basket token supply amount "2000000"
And Alice's address "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp"
And Ecocredit module's address "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6"
And Alice owns basket token amount "2000000"

Scenario: EventTake is emitted
When alice attempts to take credits with basket token amount "2000000"
Then expect event take with properties
"""
{
"owner": "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp",
"basket_denom": "eco.uC.NCT",
"credits": [
{"batch_denom": "C01-001-20200101-20210101-001", "amount": "2.000000"}
],
"amount": "2000000"
}
"""

Scenario: EventRetire is emitted
When alice attempts to take credits with basket token amount "2000000" and retire on take "true" from "US-WA"
Then expect event retire with properties
"""
{
"owner": "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp",
"batch_denom": "C01-001-20200101-20210101-001",
"amount": "2.000000",
"jurisdiction": "US-WA"
}
"""

Scenario: EventTransfer is emitted when retire on take is true
When alice attempts to take credits with basket token amount "2000000" and retire on take "true" from "US-WA"
Then expect event transfer with properties
"""
{
"sender": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"recipient": "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp",
"batch_denom": "C01-001-20200101-20210101-001",
"retired_amount": "2.000000"
}
"""

Scenario: EventTransfer is emitted when retire on take is false
When alice attempts to take credits with basket token amount "2000000" and retire on take "false"
Then expect event transfer with properties
"""
{
"sender": "regen1depk54cuajgkzea6zpgkq36tnjwdzv4ak663u6",
"recipient": "regen10z82e5ztmrm4pujgummvmr7aqjzwlp6gz8k8xp",
"batch_denom": "C01-001-20200101-20210101-001",
"tradable_amount": "2.000000"
}
"""
5 changes: 2 additions & 3 deletions x/ecocredit/server/basket/invariants_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package basket_test
package basket
Copy link
Contributor Author

@technicallyty technicallyty Aug 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to set the module address within some of the tests, so had to switch the test package to access the unexported keeper fields.


import (
"testing"
Expand All @@ -9,7 +9,6 @@ import (

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
"github.com/regen-network/regen-ledger/types/math"
"github.com/regen-network/regen-ledger/x/ecocredit/server/basket"
)

type BasketWithSupply struct {
Expand Down Expand Up @@ -69,7 +68,7 @@ func TestBasketSupplyInvarint(t *testing.T) {
for _, tc := range tcs {
tc.bank.GetSupply(s.sdkCtx, "abc")

msg, _ := basket.SupplyInvariant(s.sdkCtx, store, tc.bank, basketBalances)
msg, _ := SupplyInvariant(s.sdkCtx, store, tc.bank, basketBalances)
if tc.msg != "" {
require.Contains(msg, tc.msg, tc.name)
} else {
Expand Down
7 changes: 3 additions & 4 deletions x/ecocredit/server/basket/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package basket_test
package basket

import (
"context"
Expand All @@ -24,7 +24,6 @@ import (
ecoApi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/x/ecocredit"
"github.com/regen-network/regen-ledger/x/ecocredit/mocks"
"github.com/regen-network/regen-ledger/x/ecocredit/server/basket"
)

const (
Expand All @@ -36,7 +35,7 @@ type baseSuite struct {
t gocuke.TestingT
db ormdb.ModuleDB
ctx context.Context
k basket.Keeper
k Keeper
ctrl *gomock.Controller
addrs []sdk.AccAddress
stateStore api.StateStore
Expand Down Expand Up @@ -77,7 +76,7 @@ func setupBase(t gocuke.TestingT) *baseSuite {
authority, err := sdk.AccAddressFromBech32("regen1nzh226hxrsvf4k69sa8v0nfuzx5vgwkczk8j68")
assert.NilError(t, err)

s.k = basket.NewKeeper(s.stateStore, s.coreStore, s.bankKeeper, s.paramsKeeper, moduleAddress, authority)
s.k = NewKeeper(s.stateStore, s.coreStore, s.bankKeeper, s.paramsKeeper, moduleAddress, authority)
s.coreStore, err = ecoApi.NewStateStore(s.db)
assert.NilError(t, err)

Expand Down
22 changes: 21 additions & 1 deletion x/ecocredit/server/basket/msg_create_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//nolint:revive,stylecheck
package basket_test
package basket

import (
"encoding/json"
"strconv"
"testing"

Expand All @@ -14,6 +15,7 @@ import (

api "github.com/regen-network/regen-ledger/api/regen/ecocredit/basket/v1"
coreapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types/testutil"
"github.com/regen-network/regen-ledger/x/ecocredit/basket"
"github.com/regen-network/regen-ledger/x/ecocredit/core"
)
Expand Down Expand Up @@ -97,6 +99,12 @@ func (s *createSuite) ACreditTypeWithAbbreviationAndPrecision(a string, b string
require.NoError(s.t, err)
}

func (s *createSuite) AlicesAddress(a string) {
addr, err := sdk.AccAddressFromBech32(a)
require.NoError(s.t, err)
s.alice = addr
}

func (s *createSuite) ACreditClassWithId(a string) {
creditTypeAbbrev := core.GetCreditTypeAbbrevFromClassID(a)

Expand Down Expand Up @@ -222,6 +230,18 @@ func (s *createSuite) ExpectTheResponse(a gocuke.DocString) {
require.Equal(s.t, res, s.res)
}

func (s *createSuite) ExpectEventWithProperties(a gocuke.DocString) {
var event basket.EventCreate
err := json.Unmarshal([]byte(a.Content), &event)
require.NoError(s.t, err)

sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events())
require.True(s.t, found)

err = testutil.MatchEvent(&event, sdkEvent)
require.NoError(s.t, err)
}

func (s *createSuite) createExpectCalls() {

var expectedFee sdk.Coin
Expand Down
40 changes: 39 additions & 1 deletion x/ecocredit/server/basket/msg_put_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package basket_test
package basket

import (
"encoding/json"
"strconv"
"strings"
"testing"
Expand All @@ -18,6 +19,7 @@ import (
coreapi "github.com/regen-network/regen-ledger/api/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types"
"github.com/regen-network/regen-ledger/types/math"
"github.com/regen-network/regen-ledger/types/testutil"
"github.com/regen-network/regen-ledger/x/ecocredit/basket"
"github.com/regen-network/regen-ledger/x/ecocredit/core"
)
Expand Down Expand Up @@ -236,6 +238,18 @@ func (s *putSuite) ACreditBatchWithDenom(a string) {
require.NoError(s.t, err)
}

func (s *putSuite) AlicesAddress(a string) {
addr, err := sdk.AccAddressFromBech32(a)
require.NoError(s.t, err)
s.alice = addr
}

func (s *putSuite) EcocreditModulesAddress(a string) {
addr, err := sdk.AccAddressFromBech32(a)
require.NoError(s.t, err)
s.k.moduleAddress = addr
}

func (s *putSuite) AliceOwnsCredits() {
classID := core.GetClassIDFromBatchDenom(s.batchDenom)
creditTypeAbbrev := core.GetCreditTypeAbbrevFromClassID(classID)
Expand Down Expand Up @@ -504,6 +518,30 @@ func (s *putSuite) ExpectTheResponse(a gocuke.DocString) {
require.Equal(s.t, res, s.res)
}

func (s *putSuite) ExpectEventTransferWithProperties(a gocuke.DocString) {
var event core.EventTransfer
err := json.Unmarshal([]byte(a.Content), &event)
require.NoError(s.t, err)

sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events())
require.True(s.t, found)

err = testutil.MatchEvent(&event, sdkEvent)
require.NoError(s.t, err)
}

func (s *putSuite) ExpectEventPutWithProperties(a gocuke.DocString) {
var event basket.EventPut
err := json.Unmarshal([]byte(a.Content), &event)
require.NoError(s.t, err)

sdkEvent, found := testutil.GetEvent(&event, s.sdkCtx.EventManager().Events())
require.True(s.t, found)

err = testutil.MatchEvent(&event, sdkEvent)
require.NoError(s.t, err)
}

func (s *putSuite) putExpectCalls() {
sendCoin := s.calculateExpectedCoin(s.tradableCredits)
sendCoins := sdk.NewCoins(sendCoin)
Expand Down
Loading