Skip to content

Commit

Permalink
Add message tests (#136)
Browse files Browse the repository at this point in the history
* Add message tests

- step 1 of adding testing to the tutorial

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* minor changes

* more test cases
  • Loading branch information
tac0turtle authored Sep 13, 2019
1 parent da67f75 commit d637005
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 2 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
PACKAGES=$(shell go list ./... | grep -v '/simulation')

include Makefile.ledger
all: lint install

Expand All @@ -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
go mod verify

test:
@go test -mod=readonly $(PACKAGES)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
153 changes: 153 additions & 0 deletions x/nameservice/internal/types/msgs_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit d637005

Please sign in to comment.