cosmos-sdk/examples/democoin/x/pow/types_test.go

81 lines
2.2 KiB
Go
Raw Normal View History

package pow
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
sdk "github.com/cosmos/cosmos-sdk/types"
)
2018-04-18 21:49:24 -07:00
func TestNewMsgMine(t *testing.T) {
addr := sdk.Address([]byte("sender"))
2018-04-18 21:49:24 -07:00
msg := MsgMine{addr, 0, 0, 0, []byte("")}
equiv := NewMsgMine(addr, 0, 0, 0, []byte(""))
assert.Equal(t, msg, equiv, "%s != %s", msg, equiv)
}
2018-04-18 21:49:24 -07:00
func TestMsgMineType(t *testing.T) {
addr := sdk.Address([]byte("sender"))
2018-04-18 21:49:24 -07:00
msg := MsgMine{addr, 0, 0, 0, []byte("")}
2018-04-05 05:13:11 -07:00
assert.Equal(t, msg.Type(), "pow")
}
2018-04-18 21:49:24 -07:00
func TestMsgMineValidation(t *testing.T) {
addr := sdk.Address([]byte("sender"))
otherAddr := sdk.Address([]byte("another"))
count := uint64(0)
2018-04-18 21:49:24 -07:00
for difficulty := uint64(1); difficulty < 1000; difficulty += 100 {
2018-04-18 21:49:24 -07:00
count++
nonce, proof := mine(addr, count, difficulty)
2018-04-18 21:49:24 -07:00
msg := MsgMine{addr, difficulty, count, nonce, proof}
err := msg.ValidateBasic()
assert.Nil(t, err, "error with difficulty %d - %+v", difficulty, err)
2018-04-18 21:49:24 -07:00
msg.Count++
err = msg.ValidateBasic()
assert.NotNil(t, err, "count was wrong, should have thrown error with msg %s", msg)
2018-04-18 21:49:24 -07:00
msg.Count--
msg.Nonce++
err = msg.ValidateBasic()
assert.NotNil(t, err, "nonce was wrong, should have thrown error with msg %s", msg)
2018-04-18 21:49:24 -07:00
msg.Nonce--
msg.Sender = otherAddr
err = msg.ValidateBasic()
assert.NotNil(t, err, "sender was wrong, should have thrown error with msg %s", msg)
}
}
2018-04-18 21:49:24 -07:00
func TestMsgMineString(t *testing.T) {
addr := sdk.Address([]byte("sender"))
2018-04-18 21:49:24 -07:00
msg := MsgMine{addr, 0, 0, 0, []byte("abc")}
res := msg.String()
2018-04-18 21:49:24 -07:00
assert.Equal(t, res, "MsgMine{Sender: 73656E646572, Difficulty: 0, Count: 0, Nonce: 0, Proof: abc}")
}
2018-04-18 21:49:24 -07:00
func TestMsgMineGet(t *testing.T) {
addr := sdk.Address([]byte("sender"))
2018-04-18 21:49:24 -07:00
msg := MsgMine{addr, 0, 0, 0, []byte("")}
res := msg.Get(nil)
assert.Nil(t, res)
}
2018-04-18 21:49:24 -07:00
func TestMsgMineGetSignBytes(t *testing.T) {
addr := sdk.Address([]byte("sender"))
2018-04-18 21:49:24 -07:00
msg := MsgMine{addr, 1, 1, 1, []byte("abc")}
res := msg.GetSignBytes()
assert.Equal(t, string(res), `{"sender":"73656E646572","difficulty":1,"count":1,"nonce":1,"proof":"YWJj"}`)
}
2018-04-18 21:49:24 -07:00
func TestMsgMineGetSigners(t *testing.T) {
addr := sdk.Address([]byte("sender"))
2018-04-18 21:49:24 -07:00
msg := MsgMine{addr, 1, 1, 1, []byte("abc")}
res := msg.GetSigners()
assert.Equal(t, fmt.Sprintf("%v", res), "[73656E646572]")
}