From c8bba0545b69660b9195859615de44b9f8b106c9 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Fri, 30 Mar 2018 20:23:24 +0200 Subject: [PATCH] Add back cool module tests to democoin --- examples/democoin/app/app.go | 7 ++++ examples/democoin/app/app_test.go | 55 +++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/examples/democoin/app/app.go b/examples/democoin/app/app.go index 9c77c4735..a15bea1dd 100644 --- a/examples/democoin/app/app.go +++ b/examples/democoin/app/app.go @@ -18,6 +18,8 @@ import ( "github.com/cosmos/cosmos-sdk/x/simplestake" "github.com/cosmos/cosmos-sdk/examples/democoin/types" + "github.com/cosmos/cosmos-sdk/examples/democoin/x/cool" + "github.com/cosmos/cosmos-sdk/examples/democoin/x/sketchy" ) const ( @@ -58,10 +60,13 @@ func NewDemocoinApp(logger log.Logger, dbs map[string]dbm.DB) *DemocoinApp { // add handlers coinKeeper := bank.NewCoinKeeper(app.accountMapper) + coolKeeper := cool.NewKeeper(app.capKeyMainStore, coinKeeper) ibcMapper := ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore) stakeKeeper := simplestake.NewKeeper(app.capKeyStakingStore, coinKeeper) app.Router(). AddRoute("bank", bank.NewHandler(coinKeeper), nil). + AddRoute("cool", cool.NewHandler(coolKeeper), coolKeeper.InitGenesis). + AddRoute("sketchy", sketchy.NewHandler(), nil). AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper), nil). AddRoute("simplestake", simplestake.NewHandler(stakeKeeper), nil) @@ -98,6 +103,8 @@ func MakeCodec() *wire.Codec { struct{ sdk.Msg }{}, oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend}, oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue}, + oldwire.ConcreteType{cool.QuizMsg{}, msgTypeQuiz}, + oldwire.ConcreteType{cool.SetTrendMsg{}, msgTypeSetTrend}, oldwire.ConcreteType{ibc.IBCTransferMsg{}, msgTypeIBCTransferMsg}, oldwire.ConcreteType{ibc.IBCReceiveMsg{}, msgTypeIBCReceiveMsg}, oldwire.ConcreteType{simplestake.BondMsg{}, msgTypeBondMsg}, diff --git a/examples/democoin/app/app_test.go b/examples/democoin/app/app_test.go index d09b856ae..bf2ddc232 100644 --- a/examples/democoin/app/app_test.go +++ b/examples/democoin/app/app_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/require" "github.com/cosmos/cosmos-sdk/examples/democoin/types" + "github.com/cosmos/cosmos-sdk/examples/democoin/x/cool" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/auth" "github.com/cosmos/cosmos-sdk/x/bank" @@ -38,6 +39,31 @@ var ( Inputs: []bank.Input{bank.NewInput(addr1, coins)}, Outputs: []bank.Output{bank.NewOutput(addr2, coins)}, } + + quizMsg1 = cool.QuizMsg{ + Sender: addr1, + CoolAnswer: "icecold", + } + + quizMsg2 = cool.QuizMsg{ + Sender: addr1, + CoolAnswer: "badvibesonly", + } + + setTrendMsg1 = cool.SetTrendMsg{ + Sender: addr1, + Cool: "icecold", + } + + setTrendMsg2 = cool.SetTrendMsg{ + Sender: addr1, + Cool: "badvibesonly", + } + + setTrendMsg3 = cool.SetTrendMsg{ + Sender: addr1, + Cool: "warmandkind", + } ) func loggerAndDBs() (log.Logger, map[string]dbm.DB) { @@ -65,6 +91,8 @@ func TestMsgs(t *testing.T) { msg sdk.Msg }{ {sendMsg}, + {quizMsg1}, + {setTrendMsg1}, } sequences := []int64{0} @@ -112,6 +140,9 @@ func TestGenesis(t *testing.T) { "accounts": []*types.GenesisAccount{ types.NewGenesisAccount(acc), }, + "cool": map[string]string{ + "trend": "ice-cold", + }, } stateBytes, err := json.MarshalIndent(genesisState, "", "\t") @@ -149,6 +180,9 @@ func TestSendMsgWithAccounts(t *testing.T) { "accounts": []*types.GenesisAccount{ types.NewGenesisAccount(acc1), }, + "cool": map[string]string{ + "trend": "ice-cold", + }, } stateBytes, err := json.MarshalIndent(genesisState, "", "\t") require.Nil(t, err) @@ -221,6 +255,9 @@ func TestQuizMsg(t *testing.T) { "accounts": []*types.GenesisAccount{ types.NewGenesisAccount(acc1), }, + "cool": map[string]string{ + "trend": "ice-cold", + }, } stateBytes, err := json.MarshalIndent(genesisState, "", "\t") require.Nil(t, err) @@ -235,6 +272,21 @@ func TestQuizMsg(t *testing.T) { res1 := bapp.accountMapper.GetAccount(ctxCheck, addr1) assert.Equal(t, acc1, res1) + // Set the trend, submit a really cool quiz and check for reward + SignCheckDeliver(t, bapp, setTrendMsg1, 0, true) + SignCheckDeliver(t, bapp, quizMsg1, 1, true) + CheckBalance(t, bapp, "69icecold") + SignCheckDeliver(t, bapp, quizMsg2, 2, false) // result without reward + CheckBalance(t, bapp, "69icecold") + SignCheckDeliver(t, bapp, quizMsg1, 3, true) + CheckBalance(t, bapp, "138icecold") + SignCheckDeliver(t, bapp, setTrendMsg2, 4, true) // reset the trend + SignCheckDeliver(t, bapp, quizMsg1, 5, false) // the same answer will nolonger do! + CheckBalance(t, bapp, "138icecold") + SignCheckDeliver(t, bapp, quizMsg2, 6, true) // earlier answer now relavent again + CheckBalance(t, bapp, "69badvibesonly,138icecold") + SignCheckDeliver(t, bapp, setTrendMsg3, 7, false) // expect to fail to set the trend to something which is not cool + } func TestHandler(t *testing.T) { @@ -253,6 +305,9 @@ func TestHandler(t *testing.T) { "accounts": []*types.GenesisAccount{ types.NewGenesisAccount(acc1), }, + "cool": map[string]string{ + "trend": "ice-cold", + }, } stateBytes, err := json.MarshalIndent(genesisState, "", "\t") require.Nil(t, err)