Skip to content

Commit

Permalink
Add testing harness for e2e application logic tests (#850)
Browse files Browse the repository at this point in the history
* Add testing harness for e2e go tests

* polish

* add mint/epoch/distribution tests/helpers

* dex test

* refactor

* rebase
  • Loading branch information
codchen authored Jun 13, 2023
1 parent 95da49b commit 1315ce5
Show file tree
Hide file tree
Showing 25 changed files with 1,442 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
minttypes "github.com/sei-protocol/sei-chain/x/mint/types"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
Expand Down Expand Up @@ -58,7 +59,7 @@ func NewTestWrapper(t *testing.T, tm time.Time, valPub crptotypes.PubKey) *TestW
Ctx: ctx,
}
wrapper.SetT(t)
wrapper.setupValidator(stakingtypes.Bonded, valPub)
wrapper.setupValidator(stakingtypes.Unbonded, valPub)
return wrapper
}

Expand Down Expand Up @@ -154,8 +155,8 @@ func Setup(isCheckTx bool) *App {
true,
map[int64]bool{},
DefaultNodeHome,
5,
nil,
1,
config.TestConfig(),
encodingConfig,
wasm.EnableAllProposals,
&cosmostestutil.TestAppOpts{},
Expand Down
100 changes: 100 additions & 0 deletions tests/dex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package tests

import (
"testing"

"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/sei-protocol/sei-chain/testutil/processblock"
"github.com/sei-protocol/sei-chain/testutil/processblock/verify"
)

func TestPlaceOrders(t *testing.T) {
app := processblock.NewTestApp()
p := processblock.DexPreset(app, 3, 2)
p.DoRegisterMarkets(app)

for _, testCase := range []TestCase{
{
description: "send a single market buy without counterparty on orderbook",
input: []signing.Tx{
app.Sign(p.SignableAccounts[0], 10000, p.AllDexMarkets[0].LongMarketOrder(p.SignableAccounts[0], "11", "2")),
},
verifier: []verify.Verifier{
verify.DexOrders,
},
expectedCodes: []uint32{0},
},
{
description: "send a single market sell without counterparty on orderbook",
input: []signing.Tx{
app.Sign(p.SignableAccounts[0], 10000, p.AllDexMarkets[0].ShortMarketOrder(p.SignableAccounts[0], "10.5", "4")),
},
verifier: []verify.Verifier{
verify.DexOrders,
},
expectedCodes: []uint32{0},
},
{
description: "send a single buy limit order",
input: []signing.Tx{
app.Sign(p.SignableAccounts[0], 10000, p.AllDexMarkets[0].LongLimitOrder(p.SignableAccounts[0], "10.5", "5")),
},
verifier: []verify.Verifier{
verify.DexOrders,
},
expectedCodes: []uint32{0},
},
{
description: "send a single sell limit order",
input: []signing.Tx{
app.Sign(p.SignableAccounts[1], 10000, p.AllDexMarkets[0].ShortLimitOrder(p.SignableAccounts[1], "11", "3")),
},
verifier: []verify.Verifier{
verify.DexOrders,
},
expectedCodes: []uint32{0},
},
{
description: "send a single market buy without exhausting the orderbook",
input: []signing.Tx{
app.Sign(p.SignableAccounts[2], 10000, p.AllDexMarkets[0].LongMarketOrder(p.SignableAccounts[2], "11", "2")),
},
verifier: []verify.Verifier{
verify.DexOrders,
},
expectedCodes: []uint32{0},
},
{
description: "send a single market sell without exhausting the orderbook",
input: []signing.Tx{
app.Sign(p.SignableAccounts[2], 10000, p.AllDexMarkets[0].ShortMarketOrder(p.SignableAccounts[2], "10.5", "4")),
},
verifier: []verify.Verifier{
verify.DexOrders,
},
expectedCodes: []uint32{0},
},
{
description: "send a single market buy exhausting the orderbook",
input: []signing.Tx{
app.Sign(p.SignableAccounts[2], 10000, p.AllDexMarkets[0].LongMarketOrder(p.SignableAccounts[2], "12", "2")),
},
verifier: []verify.Verifier{
verify.DexOrders,
},
expectedCodes: []uint32{0},
},
{
description: "send a single market sell exhausting the orderbook",
input: []signing.Tx{
app.Sign(p.SignableAccounts[2], 10000, p.AllDexMarkets[0].ShortMarketOrder(p.SignableAccounts[2], "10", "2")),
},
verifier: []verify.Verifier{
verify.DexOrders,
},
expectedCodes: []uint32{0},
},
} {
testCase.run(t, app)
}
}
35 changes: 35 additions & 0 deletions tests/distribution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tests

import (
"testing"

"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/sei-protocol/sei-chain/testutil/processblock"
"github.com/sei-protocol/sei-chain/testutil/processblock/msgs"
"github.com/sei-protocol/sei-chain/testutil/processblock/verify"
)

func TestDistribution(t *testing.T) {
app := processblock.NewTestApp()
p := processblock.CommonPreset(app)
for _, testCase := range []TestCase{
{
description: "send to accrue fee for next block",
input: []signing.Tx{
p.AdminSign(app, msgs.Send(p.Admin, p.AllAccounts[0], 1000)),
},
verifier: []verify.Verifier{},
expectedCodes: []uint32{0},
},
{
description: "check distribution",
input: []signing.Tx{},
verifier: []verify.Verifier{
verify.Allocation,
},
expectedCodes: []uint32{},
},
} {
testCase.run(t, app)
}
}
39 changes: 39 additions & 0 deletions tests/epoch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tests

import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/sei-protocol/sei-chain/testutil/processblock"
"github.com/sei-protocol/sei-chain/testutil/processblock/verify"
)

func TestEpoch(t *testing.T) {
app := processblock.NewTestApp()
_ = processblock.CommonPreset(app)
app.FastEpoch()
for i, testCase := range []TestCase{
{
description: "first epoch",
input: []signing.Tx{},
verifier: []verify.Verifier{
verify.Epoch,
},
expectedCodes: []uint32{},
},
{
description: "second epoch",
input: []signing.Tx{},
verifier: []verify.Verifier{
verify.Epoch,
},
expectedCodes: []uint32{},
},
} {
if i > 0 {
time.Sleep(6 * time.Second)
}
testCase.run(t, app)
}
}
Binary file added tests/mars.wasm
Binary file not shown.
40 changes: 40 additions & 0 deletions tests/mint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tests

import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/sei-protocol/sei-chain/testutil/processblock"
"github.com/sei-protocol/sei-chain/testutil/processblock/verify"
)

func TestMint(t *testing.T) {
app := processblock.NewTestApp()
_ = processblock.CommonPreset(app)
app.NewMinter(1000000)
app.FastEpoch()
for i, testCase := range []TestCase{
{
description: "first epoch",
input: []signing.Tx{},
verifier: []verify.Verifier{
verify.MintRelease,
},
expectedCodes: []uint32{},
},
{
description: "second epoch",
input: []signing.Tx{},
verifier: []verify.Verifier{
verify.MintRelease,
},
expectedCodes: []uint32{},
},
} {
if i > 0 {
time.Sleep(6 * time.Second)
}
testCase.run(t, app)
}
}
56 changes: 56 additions & 0 deletions tests/template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tests

import (
"testing"

"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/sei-protocol/sei-chain/testutil/processblock"
"github.com/sei-protocol/sei-chain/testutil/processblock/msgs"
"github.com/sei-protocol/sei-chain/testutil/processblock/verify"
"github.com/stretchr/testify/require"
)

type TestCase struct {
description string
input []signing.Tx
verifier []verify.Verifier
expectedCodes []uint32
}

func (c *TestCase) run(t *testing.T, app *processblock.App) {
blockRunner := func() []uint32 { return app.RunBlock(c.input) }
for _, v := range c.verifier {
blockRunner = v(t, app, blockRunner, c.input)
}
require.Equal(t, c.expectedCodes, blockRunner(), c.description)
}

func TestTemplate(t *testing.T) {
app := processblock.NewTestApp()
p := processblock.CommonPreset(app) // choose a preset
for _, testCase := range []TestCase{
{
description: "simple send 1",
input: []signing.Tx{
p.AdminSign(app, msgs.Send(p.Admin, p.AllAccounts[0], 1000)),
},
verifier: []verify.Verifier{
verify.Balance,
},
expectedCodes: []uint32{0},
},
{
description: "simple send 2",
input: []signing.Tx{
p.AdminSign(app, msgs.Send(p.Admin, p.AllAccounts[1], 2000)),
p.AdminSign(app, msgs.Send(p.Admin, p.AllAccounts[2], 3000)),
},
verifier: []verify.Verifier{
verify.Balance,
},
expectedCodes: []uint32{0, 0},
},
} {
testCase.run(t, app)
}
}
Loading

0 comments on commit 1315ce5

Please sign in to comment.