From d637005f852c97e3aede93e8d1b7322d45d2fc9b Mon Sep 17 00:00:00 2001 From: Marko Date: Fri, 13 Sep 2019 14:12:13 +0200 Subject: [PATCH] Add message tests (#136) * Add message tests - step 1 of adding testing to the tutorial Signed-off-by: Marko Baricevic * minor changes * more test cases --- Makefile | 7 +- go.mod | 2 +- x/nameservice/internal/types/msgs_test.go | 153 ++++++++++++++++++++++ 3 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 x/nameservice/internal/types/msgs_test.go diff --git a/Makefile b/Makefile index 253a461660..74fdc496dc 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +PACKAGES=$(shell go list ./... | grep -v '/simulation') + include Makefile.ledger all: lint install @@ -12,4 +14,7 @@ go.sum: go.mod lint: golangci-lint run @find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" | xargs gofmt -d -s - go mod verify \ No newline at end of file + go mod verify + +test: + @go test -mod=readonly $(PACKAGES) \ No newline at end of file diff --git a/go.mod b/go.mod index b36849011f..aed13b500c 100644 --- a/go.mod +++ b/go.mod @@ -11,10 +11,10 @@ require ( github.com/spf13/afero v1.2.2 // indirect github.com/spf13/cobra v0.0.5 github.com/spf13/viper v1.3.2 + github.com/stretchr/testify v1.3.0 github.com/tendermint/go-amino v0.15.0 github.com/tendermint/tendermint v0.32.2 github.com/tendermint/tm-db v0.1.1 golang.org/x/sys v0.0.0-20190329044733-9eb1bfa1ce65 // indirect - google.golang.org/appengine v1.4.0 // indirect google.golang.org/genproto v0.0.0-20190327125643-d831d65fe17d // indirect ) diff --git a/x/nameservice/internal/types/msgs_test.go b/x/nameservice/internal/types/msgs_test.go new file mode 100644 index 0000000000..1d1a018f1b --- /dev/null +++ b/x/nameservice/internal/types/msgs_test.go @@ -0,0 +1,153 @@ +package types + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" +) + +func TestMsgSetName(t *testing.T) { + name := "maTurtle" + value := "1" + acc := sdk.AccAddress([]byte("me")) + var msg = NewMsgSetName(name, value, acc) + + require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Type(), "set_name") +} + +func TestMsgSetNameValidation(t *testing.T) { + name := "maTurtle" + value := "1" + acc := sdk.AccAddress([]byte("me")) + name2 := "a" + value2 := "2" + acc2 := sdk.AccAddress([]byte("you")) + + cases := []struct { + valid bool + tx MsgSetName + }{ + {true, NewMsgSetName(name, value, acc)}, + {true, NewMsgSetName(name2, value2, acc2)}, + {true, NewMsgSetName(name2, value, acc2)}, + {true, NewMsgSetName(name2, value2, acc)}, + {false, NewMsgSetName(name, value2, nil)}, + {false, NewMsgSetName("", value2, acc2)}, + {false, NewMsgSetName(name, "", acc2)}, + } + + for _, tc := range cases { + err := tc.tx.ValidateBasic() + if tc.valid { + require.Nil(t, err) + } else { + require.NotNil(t, err) + } + } +} + +func TestMsgSetNameGetSignBytes(t *testing.T) { + name := "maTurtle" + value := "1" + acc := sdk.AccAddress([]byte("me")) + + var msg = NewMsgSetName(name, value, acc) + res := msg.GetSignBytes() + + expected := `{"type":"nameservice/SetName","value":{"name":"maTurtle","owner":"cosmos1d4js690r9j","value":"1"}}` + + require.Equal(t, expected, string(res)) +} + +func TestMsgBuyName(t *testing.T) { + name := "maTurtle" + coins := sdk.NewCoins(sdk.NewInt64Coin("atom", 10)) + acc := sdk.AccAddress([]byte("me")) + var msg = NewMsgBuyName(name, coins, acc) + + require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Type(), "buy_name") +} + +func TestMsgBuyNameValidation(t *testing.T) { + name := "maTurtle" + acc := sdk.AccAddress([]byte("me")) + name2 := "a" + acc2 := sdk.AccAddress([]byte("you")) + coins := sdk.NewCoins(sdk.NewInt64Coin("atom", 10)) + + cases := []struct { + valid bool + tx MsgBuyName + }{ + {true, NewMsgBuyName(name, coins, acc)}, + {true, NewMsgBuyName(name2, coins, acc2)}, + } + + for _, tc := range cases { + err := tc.tx.ValidateBasic() + if tc.valid { + require.Nil(t, err) + } else { + require.NotNil(t, err) + } + } +} + +func TestMsgBuyNameGetSignBytes(t *testing.T) { + name := "maTurtle" + acc := sdk.AccAddress([]byte("me")) + coins := sdk.NewCoins(sdk.NewInt64Coin("atom", 10)) + var msg = NewMsgBuyName(name, coins, acc) + res := msg.GetSignBytes() + + expected := `{"type":"nameservice/BuyName","value":{"bid":[{"amount":"10","denom":"atom"}],"buyer":"cosmos1d4js690r9j","name":"maTurtle"}}` + + require.Equal(t, expected, string(res)) +} + +func TestMsgDeleteName(t *testing.T) { + name := "maTurtle" + acc := sdk.AccAddress([]byte("me")) + var msg = NewMsgDeleteName(name, acc) + + require.Equal(t, msg.Route(), RouterKey) + require.Equal(t, msg.Type(), "delete_name") +} + +func TestMsgDeleteNameValidation(t *testing.T) { + name := "maTurtle" + acc := sdk.AccAddress([]byte("me")) + name2 := "a" + acc2 := sdk.AccAddress([]byte("you")) + + cases := []struct { + valid bool + tx MsgDeleteName + }{ + {true, NewMsgDeleteName(name, acc)}, + {true, NewMsgDeleteName(name2, acc2)}, + } + + for _, tc := range cases { + err := tc.tx.ValidateBasic() + if tc.valid { + require.Nil(t, err) + } else { + require.NotNil(t, err) + } + } +} + +func TestMsgDeleteNameGetSignBytes(t *testing.T) { + name := "maTurtle" + acc := sdk.AccAddress([]byte("me")) + var msg = NewMsgDeleteName(name, acc) + res := msg.GetSignBytes() + + expected := `{"type":"nameservice/DeleteName","value":{"name":"maTurtle","owner":"cosmos1d4js690r9j"}}` + + require.Equal(t, expected, string(res)) +}