Skip to content

Commit

Permalink
test: test code per each module of the core package
Browse files Browse the repository at this point in the history
  • Loading branch information
Moonyongjung committed Sep 6, 2023
1 parent 15b554e commit 5e7e0f0
Show file tree
Hide file tree
Showing 16 changed files with 964 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/auth/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package auth_test

import (
"github.com/xpladev/xpla.go/core/auth"
)

func (s *IntegrationTestSuite) TestCoreModule() {
c := auth.NewCoreModule()

// test get name
s.Require().Equal(auth.AuthModule, c.Name())

// test tx
_, err := c.NewTxRouter(nil, "", nil)
s.Require().Error(err)
}
90 changes: 90 additions & 0 deletions core/authz/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package authz_test

import (
"math/rand"

"github.com/xpladev/xpla.go/client"
"github.com/xpladev/xpla.go/core/authz"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/util/testutil"
)

func (s *IntegrationTestSuite) TestCoreModule() {
src := rand.NewSource(1)
r := rand.New(src)
accounts := testutil.RandomAccounts(r, 2)
s.xplac.WithPrivateKey(accounts[0].PrivKey)

c := authz.NewCoreModule()

// test get name
s.Require().Equal(authz.AuthzModule, c.Name())

// test tx
var testMsg interface{}
txBuilder := s.xplac.GetEncoding().TxConfig.NewTxBuilder()

// authz grant
authzGrantMsg := types.AuthzGrantMsg{
Granter: accounts[0].Address.String(),
Grantee: accounts[1].Address.String(),
AuthorizationType: "send",
SpendLimit: "1000",
}

makeAuthzGrantMsg, err := authz.MakeAuthzGrantMsg(authzGrantMsg, s.xplac.GetPrivateKey())
s.Require().NoError(err)

testMsg = makeAuthzGrantMsg
txBuilder, err = c.NewTxRouter(txBuilder, authz.AuthzGrantMsgType, testMsg)
s.Require().NoError(err)
s.Require().Equal(&makeAuthzGrantMsg, txBuilder.GetTx().GetMsgs()[0])

// authz revoke
authzRevokeMsg := types.AuthzRevokeMsg{
Granter: accounts[0].Address.String(),
Grantee: accounts[1].Address.String(),
MsgType: "/cosmos.bank.v1beta1.MsgSend",
}

makeAuthzRevokeMsg, err := authz.MakeAuthzRevokeMsg(authzRevokeMsg, s.xplac.GetPrivateKey())
s.Require().NoError(err)

testMsg = makeAuthzRevokeMsg
txBuilder, err = c.NewTxRouter(txBuilder, authz.AuthzRevokeMsgType, testMsg)
s.Require().NoError(err)
s.Require().Equal(&makeAuthzRevokeMsg, txBuilder.GetTx().GetMsgs()[0])

// authz exec
// e.g. bank send
bankSendMsg := types.BankSendMsg{
FromAddress: accounts[0].Address.String(),
ToAddress: accounts[1].Address.String(),
Amount: "1000",
}

txbytesBankSend, err := s.xplac.BankSend(bankSendMsg).CreateAndSignTx()
s.Require().NoError(err)

bankSendJsonTxbytes, err := s.xplac.EncodedTxbytesToJsonTx(txbytesBankSend)
s.Require().NoError(err)

authzExecMsg := types.AuthzExecMsg{
Grantee: accounts[1].Address.String(),
ExecTxString: string(bankSendJsonTxbytes),
}

makeAuthzExecMsg, err := authz.MakeAuthzExecMsg(authzExecMsg, s.xplac.GetEncoding())
s.Require().NoError(err)

testMsg = makeAuthzExecMsg
txBuilder, err = c.NewTxRouter(txBuilder, authz.AuthzExecMsgType, testMsg)
s.Require().NoError(err)
s.Require().Equal(&makeAuthzExecMsg, txBuilder.GetTx().GetMsgs()[0])

// invalid tx msg type
_, err = c.NewTxRouter(nil, "invalid message type", nil)
s.Require().Error(err)

s.xplac = client.ResetXplac(s.xplac)
}
47 changes: 47 additions & 0 deletions core/bank/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package bank_test

import (
"math/rand"

"github.com/xpladev/xpla.go/client"
"github.com/xpladev/xpla.go/core/bank"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/util/testutil"
)

func (s *IntegrationTestSuite) TestCoreModule() {
src := rand.NewSource(1)
r := rand.New(src)
accounts := testutil.RandomAccounts(r, 2)
s.xplac.WithPrivateKey(accounts[0].PrivKey)

c := bank.NewCoreModule()

// test get name
s.Require().Equal(bank.BankModule, c.Name())

// test tx
var testMsg interface{}
txBuilder := s.xplac.GetEncoding().TxConfig.NewTxBuilder()

// bank send
bankSendMsg := types.BankSendMsg{
FromAddress: accounts[0].Address.String(),
ToAddress: accounts[1].Address.String(),
Amount: "1000",
}

makeBankSendMsg, err := bank.MakeBankSendMsg(bankSendMsg, s.xplac.GetPrivateKey())
s.Require().NoError(err)

testMsg = makeBankSendMsg
txBuilder, err = c.NewTxRouter(txBuilder, bank.BankSendMsgType, testMsg)
s.Require().NoError(err)
s.Require().Equal(&makeBankSendMsg, txBuilder.GetTx().GetMsgs()[0])

// invalid tx msg type
_, err = c.NewTxRouter(nil, "invalid message type", nil)
s.Require().Error(err)

s.xplac = client.ResetXplac(s.xplac)
}
16 changes: 16 additions & 0 deletions core/base/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package base_test

import (
"github.com/xpladev/xpla.go/core/base"
)

func (s *IntegrationTestSuite) TestCoreModule() {
c := base.NewCoreModule()

// test get name
s.Require().Equal(base.Base, c.Name())

// test tx
_, err := c.NewTxRouter(nil, "", nil)
s.Require().Error(err)
}
99 changes: 99 additions & 0 deletions core/crisis/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package crisis_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/suite"
"github.com/xpladev/xpla.go/client"
"github.com/xpladev/xpla.go/core/crisis"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/util/testutil"
"github.com/xpladev/xpla.go/util/testutil/network"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

type IntegrationTestSuite struct {
suite.Suite

xplac *client.XplaClient
apis []string

cfg network.Config
network *network.Network
}

func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite {
return &IntegrationTestSuite{cfg: cfg}
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

genesisState := s.cfg.GenesisState
var bankGenesis banktypes.GenesisState
s.Require().NoError(s.cfg.Codec.UnmarshalJSON(genesisState[banktypes.ModuleName], &bankGenesis))

bankGenesisBz, err := s.cfg.Codec.MarshalJSON(&bankGenesis)
s.Require().NoError(err)
genesisState[banktypes.ModuleName] = bankGenesisBz
s.cfg.GenesisState = genesisState

s.network = network.New(s.T(), s.cfg)
s.Require().NoError(s.network.WaitForNextBlock())

s.xplac = client.NewTestXplaClient()
s.apis = []string{
s.network.Validators[0].APIAddress,
s.network.Validators[0].AppConfig.GRPC.Address,
}
}

func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}

func (s *IntegrationTestSuite) TestCoreModule() {
src := rand.NewSource(1)
r := rand.New(src)
accounts := testutil.RandomAccounts(r, 2)
s.xplac.WithPrivateKey(accounts[0].PrivKey)

c := crisis.NewCoreModule()

// test get name
s.Require().Equal(crisis.CrisisModule, c.Name())

// test tx
var testMsg interface{}
txBuilder := s.xplac.GetEncoding().TxConfig.NewTxBuilder()

// invariant broken
invariantBrokenMsg := types.InvariantBrokenMsg{
ModuleName: "bank",
InvariantRoute: "total-supply",
}
s.xplac.InvariantBroken(invariantBrokenMsg)

makeInvariantRouteMsg, err := crisis.MakeInvariantRouteMsg(invariantBrokenMsg, s.xplac.GetPrivateKey())
s.Require().NoError(err)

testMsg = makeInvariantRouteMsg
txBuilder, err = c.NewTxRouter(txBuilder, crisis.CrisisInvariantBrokenMsgType, testMsg)
s.Require().NoError(err)
s.Require().Equal(&makeInvariantRouteMsg, txBuilder.GetTx().GetMsgs()[0])

// invalid tx msg type
_, err = c.NewTxRouter(nil, "invalid message type", nil)
s.Require().Error(err)

s.xplac = client.ResetXplac(s.xplac)
}

func TestIntegrationTestSuite(t *testing.T) {
cfg := network.DefaultConfig()
cfg.NumValidators = 2
suite.Run(t, NewIntegrationTestSuite(cfg))
}
90 changes: 90 additions & 0 deletions core/distribution/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package distribution_test

import (
"math/rand"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/xpladev/xpla.go/client"
"github.com/xpladev/xpla.go/core/distribution"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/util/testutil"
)

func (s *IntegrationTestSuite) TestCoreModule() {
src := rand.NewSource(1)
r := rand.New(src)
accounts := testutil.RandomAccounts(r, 2)
s.xplac.WithPrivateKey(accounts[0].PrivKey)

c := distribution.NewCoreModule()

// test get name
s.Require().Equal(distribution.DistributionModule, c.Name())

// test tx
var testMsg interface{}
txBuilder := s.xplac.GetEncoding().TxConfig.NewTxBuilder()

// fund community pool
fundCommunityPoolMsg := types.FundCommunityPoolMsg{
Amount: "1000",
}

makeFundCommunityPoolMsg, err := distribution.MakeFundCommunityPoolMsg(fundCommunityPoolMsg, s.xplac.GetPrivateKey())
s.Require().NoError(err)

testMsg = makeFundCommunityPoolMsg
txBuilder, err = c.NewTxRouter(txBuilder, distribution.DistributionFundCommunityPoolMsgType, testMsg)
s.Require().NoError(err)
s.Require().Equal(&makeFundCommunityPoolMsg, txBuilder.GetTx().GetMsgs()[0])

// community pool spend
communityPoolSpendMsg := types.CommunityPoolSpendMsg{
Title: "community pool spend",
Description: "pay me",
Recipient: accounts[0].Address.String(),
Amount: "1000",
Deposit: "1000",
}

makeProposalCommunityPoolSpendMsg, err := distribution.MakeProposalCommunityPoolSpendMsg(communityPoolSpendMsg, s.xplac.GetPrivateKey(), s.xplac.GetEncoding())
s.Require().NoError(err)

testMsg = makeProposalCommunityPoolSpendMsg
txBuilder, err = c.NewTxRouter(txBuilder, distribution.DistributionProposalCommunityPoolSpendMsgType, testMsg)
s.Require().NoError(err)
s.Require().Equal(&makeProposalCommunityPoolSpendMsg, txBuilder.GetTx().GetMsgs()[0])

// withdraw rewards
withdrawRewardsMsg := types.WithdrawRewardsMsg{
DelegatorAddr: accounts[0].Address.String(),
ValidatorAddr: sdk.ValAddress(accounts[0].Address).String(),
Commission: true,
}

makeWithdrawRewardsMsg, err := distribution.MakeWithdrawRewardsMsg(withdrawRewardsMsg, s.xplac.GetPrivateKey())
s.Require().NoError(err)

testMsg = makeWithdrawRewardsMsg
txBuilder, err = c.NewTxRouter(txBuilder, distribution.DistributionWithdrawRewardsMsgType, testMsg)
s.Require().NoError(err)

// set withdraw address
setWithdrawAddrMsg := types.SetWithdrawAddrMsg{
WithdrawAddr: accounts[0].Address.String(),
}

makeSetWithdrawAddrMsg, err := distribution.MakeSetWithdrawAddrMsg(setWithdrawAddrMsg, s.xplac.GetPrivateKey())
s.Require().NoError(err)

testMsg = makeSetWithdrawAddrMsg
txBuilder, err = c.NewTxRouter(txBuilder, distribution.DistributionSetWithdrawAddrMsgType, testMsg)
s.Require().NoError(err)
s.Require().Equal(&makeSetWithdrawAddrMsg, txBuilder.GetTx().GetMsgs()[0])

// invalid tx msg type
_, err = c.NewTxRouter(nil, "invalid message type", nil)
s.Require().Error(err)

s.xplac = client.ResetXplac(s.xplac)
}
16 changes: 16 additions & 0 deletions core/evidence/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package evidence_test

import (
"github.com/xpladev/xpla.go/core/evidence"
)

func (s *IntegrationTestSuite) TestCoreModule() {
c := evidence.NewCoreModule()

// test get name
s.Require().Equal(evidence.EvidenceModule, c.Name())

// test tx
_, err := c.NewTxRouter(nil, "", nil)
s.Require().Error(err)
}
16 changes: 16 additions & 0 deletions core/evm/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package evm_test

import (
"github.com/xpladev/xpla.go/core/evm"
)

func (s *IntegrationTestSuite) TestCoreModule() {
c := evm.NewCoreModule()

// test get name
s.Require().Equal(evm.EvmModule, c.Name())

// test tx
_, err := c.NewTxRouter(nil, "", nil)
s.Require().Error(err)
}
Loading

0 comments on commit 5e7e0f0

Please sign in to comment.