Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman committed Dec 10, 2017
1 parent efbc4b9 commit f00ce7a
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 61 deletions.
14 changes: 7 additions & 7 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (at *appTest) reset() {
at.initAccount(at.acctOut)

resabci := at.app.Commit()
require.True(at.t, resabci.Code.IsOK(), resabci)
require.True(at.t, resabci.IsOK(), resabci)
}

func getBalance(key sdk.Actor, store state.SimpleDB) (coin.Coins, error) {
Expand All @@ -145,7 +145,7 @@ func (at *appTest) execDeliver(t *testing.T, tx sdk.Tx) (res abci.ResponseDelive
res = at.app.DeliverTx(txBytes)

// check the tags
if res.Code.IsOK() {
if res.IsOK() {
tags := res.Tags
require.NotEmpty(tags)
require.Equal("height", tags[0].Key)
Expand Down Expand Up @@ -270,13 +270,13 @@ func TestTx(t *testing.T) {
//Regular CheckTx
at.reset()
cres, _, _ = at.execCheck(t, at.getTx(coin.Coins{{"mycoin", 5}}, 1))
assert.True(cres.Code.IsOK(), "ExecTx/Good CheckTx: Expected OK return from ExecTx, Error: %v", cres)
assert.True(cres.IsOK(), "ExecTx/Good CheckTx: Expected OK return from ExecTx, Error: %v", cres)

//Regular DeliverTx
at.reset()
amt := coin.Coins{{"mycoin", 3}}
dres, diffIn, diffOut = at.execDeliver(t, at.getTx(amt, 1))
assert.True(dres.Code.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", dres)
assert.True(dres.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", dres)
assert.Equal(amt.Negative(), diffIn)
assert.Equal(amt, diffOut)

Expand All @@ -285,7 +285,7 @@ func TestTx(t *testing.T) {
amt = coin.Coins{{"mycoin", 4}}
toll := coin.Coin{"mycoin", 1}
dres, diffIn, diffOut = at.execDeliver(t, at.feeTx(amt, toll, 1))
assert.True(dres.Code.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", dres)
assert.True(dres.IsOK(), "ExecTx/Good DeliverTx: Expected OK return from ExecTx, Error: %v", dres)
payment := amt.Plus(coin.Coins{toll}).Negative()
assert.Equal(payment, diffIn)
assert.Equal(amt, diffOut)
Expand All @@ -297,15 +297,15 @@ func TestQuery(t *testing.T) {
at := newAppTest(t)

dres, _, _ := at.execDeliver(t, at.getTx(coin.Coins{{"mycoin", 5}}, 1))
assert.True(dres.Code.IsOK(), "Commit, DeliverTx: Expected OK return from DeliverTx, Error: %v", dres)
assert.True(dres.IsOK(), "Commit, DeliverTx: Expected OK return from DeliverTx, Error: %v", dres)

resQueryPreCommit := at.app.Query(abci.RequestQuery{
Path: "/account",
Data: at.acctIn.Address(),
})

cres := at.app.Commit()
assert.True(cres.Code.IsOK(), cres)
assert.True(cres.IsOK(), cres)

key := stack.PrefixedKey(coin.NameCoin, at.acctIn.Address())
resQueryPostCommit := at.app.Query(abci.RequestQuery{
Expand Down
6 changes: 3 additions & 3 deletions app/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (app *StoreApp) SetOption(res abci.RequestSetOption) abci.ResponseSetOption
func (app *StoreApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
if len(reqQuery.Data) == 0 {
resQuery.Log = "Query cannot be zero length"
resQuery.Code = abci.CodeType_EncodingError
resQuery.Code = errors.CodeTypeEncodingErr
return
}

Expand All @@ -150,7 +150,7 @@ func (app *StoreApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQu
// is not yet in the blockchain

withProof := app.CommittedHeight() - 1
if tree.Tree.VersionExists(withProof) {
if tree.Tree.VersionExists(uint64(withProof)) {
height = withProof
} else {
height = app.CommittedHeight()
Expand All @@ -176,7 +176,7 @@ func (app *StoreApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQu
}

default:
resQuery.Code = abci.CodeType_UnknownRequest
resQuery.Code = errors.CodeTypeUnknownRequest
resQuery.Log = cmn.Fmt("Unexpected Query path: %v", reqQuery.Path)
}
return
Expand Down
2 changes: 1 addition & 1 deletion app/val_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestEndBlock(t *testing.T) {
tx := base.ValChangeTx{c}.Wrap()
txBytes := wire.BinaryBytes(tx)
res := app.DeliverTx(txBytes)
require.True(res.Code.IsOK(), "%#v", res)
require.True(res.IsOK(), "%#v", res)
}
diff := app.EndBlock(abci.RequestEndBlock{app.height})
// TODO: don't care about order here...
Expand Down
4 changes: 2 additions & 2 deletions client/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestAppProofs(t *testing.T) {
require.NoError(err, "%+v", err)
require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx)
require.EqualValues(0, br.DeliverTx.Code)
brh := int(br.Height)
brh := br.Height

// This sets up our trust on the node based on some past point.
source := certclient.NewProvider(cl)
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestTxProofs(t *testing.T) {
require.NoError(err, "%+v", err)
require.EqualValues(0, br.CheckTx.Code, "%#v", br.CheckTx)
require.EqualValues(0, br.DeliverTx.Code)
brh := int(br.Height)
brh := br.Height

source := certclient.NewProvider(cl)
seed, err := source.GetByHeight(brh - 2)
Expand Down
19 changes: 9 additions & 10 deletions errors/code.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package errors

const (
defaultErrCode uint32 = 0x1
CodeTypeInternalErr uint32 = 1
CodeTypeEncodingErr uint32 = 2
CodeTypeUnauthorized uint32 = 3
CodeTypeUnknownRequest uint32 = 4
CodeTypeUnknownAddress uint32 = 5
CodeTypeBaseUnknownAddress uint32 = 5 // lol fuck it
CodeTypeBadNonce uint32 = 6

CodeTypeInternalErr uint32 = 0
CodeTypeEncodingErr uint32 = 1
CodeTypeUnauthorized uint32 = 2
CodeTypeUnknownRequest uint32 = 3
CodeTypeUnknownAddress uint32 = 4
CodeTypeBaseUnknownAddress uint32 = 4 // lol fuck it
CodeTypeBadNonce uint32 = 5
CodeTypeBaseInvalidInput uint32 = 20
CodeTypeBaseInvalidOutput uint32 = 21
CodeTypeBaseInvalidInput uint32 = 20
CodeTypeBaseInvalidOutput uint32 = 21
)
4 changes: 2 additions & 2 deletions errors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func Wrap(err error) TMError {
return tm
}

return WithCode(err, defaultErrCode)
return WithCode(err, CodeTypeInternalErr)
}

// WithCode adds a stacktrace if necessary and sets the code and msg,
Expand Down Expand Up @@ -145,5 +145,5 @@ func HasErrorCode(err error, code uint32) bool {
if tm, ok := err.(TMError); ok {
return tm.ErrorCode() == code
}
return code == defaultErrCode
return code == CodeTypeInternalErr
}
18 changes: 8 additions & 10 deletions errors/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (

pkerr "github.com/pkg/errors"
"github.com/stretchr/testify/assert"

abci "github.com/tendermint/abci/types"
)

func TestCreateResult(t *testing.T) {
Expand All @@ -17,15 +15,15 @@ func TestCreateResult(t *testing.T) {
cases := []struct {
err error
msg string
code abci.CodeType
code uint32
}{
{stderr.New("base"), "base", defaultErrCode},
{pkerr.New("dave"), "dave", defaultErrCode},
{New("nonce", abci.CodeType_BadNonce), "nonce", abci.CodeType_BadNonce},
{Wrap(stderr.New("wrap")), "wrap", defaultErrCode},
{WithCode(stderr.New("coded"), abci.CodeType_BaseInvalidInput), "coded", abci.CodeType_BaseInvalidInput},
{ErrDecoding(), errDecoding.Error(), abci.CodeType_EncodingError},
{ErrUnauthorized(), errUnauthorized.Error(), abci.CodeType_Unauthorized},
{stderr.New("base"), "base", CodeTypeInternalErr},
{pkerr.New("dave"), "dave", CodeTypeInternalErr},
{New("nonce", CodeTypeBadNonce), "nonce", CodeTypeBadNonce},
{Wrap(stderr.New("wrap")), "wrap", CodeTypeInternalErr},
{WithCode(stderr.New("coded"), CodeTypeBaseInvalidInput), "coded", CodeTypeBaseInvalidInput},
{ErrDecoding(), errDecoding.Error(), CodeTypeEncodingErr},
{ErrUnauthorized(), errUnauthorized.Error(), CodeTypeUnauthorized},
}

for idx, tc := range cases {
Expand Down
9 changes: 6 additions & 3 deletions examples/basecoin/cmd/baseserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
"github.com/tendermint/tmlibs/cli"
tmlog "github.com/tendermint/tmlibs/log"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/commands"
rest "github.com/cosmos/cosmos-sdk/client/rest"
coinrest "github.com/cosmos/cosmos-sdk/modules/coin/rest"
noncerest "github.com/cosmos/cosmos-sdk/modules/nonce/rest"
rolerest "github.com/cosmos/cosmos-sdk/modules/roles/rest"
"github.com/tendermint/tmlibs/cli"
)

var srvCli = &cobra.Command{
Expand Down Expand Up @@ -79,13 +82,13 @@ func serve(cmd *cobra.Command, args []string) error {
port := viper.GetInt(envPortFlag)
addr := fmt.Sprintf(":%d", port)

onDisconnect := rpc.OnDisconnect(func(remoteAddr string) {
onDisconnect := rpcserver.OnDisconnect(func(remoteAddr string) {
// FIXME: TODO
// n.eventBus.UnsubscribeAll(context.Background(), remoteAddr)
})
routes := client.RPCRoutes(rpcClient)
wm := rpcserver.NewWebsocketManager(routes, onDisconnect)
wsLogger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "ws")
wsLogger := tmlog.NewTMLogger(tmlog.NewSyncWriter(os.Stdout)).With("module", "ws")
wm.SetLogger(wsLogger)
router.HandleFunc("/websocket", wm.WebsocketHandler)

Expand Down
3 changes: 1 addition & 2 deletions examples/counter/plugins/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package counter
import (
"fmt"

abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-wire"

sdk "github.com/cosmos/cosmos-sdk"
Expand Down Expand Up @@ -74,7 +73,7 @@ var (

// ErrInvalidCounter - custom error class
func ErrInvalidCounter() error {
return errors.WithCode(errInvalidCounter, abci.CodeType_BaseInvalidInput)
return errors.WithCode(errInvalidCounter, errors.CodeTypeBaseInvalidInput)
}

// IsInvalidCounterErr - custom error class check
Expand Down
4 changes: 2 additions & 2 deletions examples/counter/plugins/counter/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestCounterPlugin(t *testing.T) {

// Test a basic send, no fee
res := DeliverCounterTx(true, nil, 1)
assert.True(res.Code.IsOK(), res.String())
assert.True(res.IsOK(), res.String())

// Test an invalid send, no fee
res = DeliverCounterTx(false, nil, 2)
Expand All @@ -65,7 +65,7 @@ func TestCounterPlugin(t *testing.T) {

// Test an valid send, with supported fee
res = DeliverCounterTx(true, coin.Coins{{"gold", 100}}, 3)
assert.True(res.Code.IsOK(), res.String())
assert.True(res.IsOK(), res.String())

// Test unsupported fee
res = DeliverCounterTx(true, coin.Coins{{"silver", 100}}, 4)
Expand Down
6 changes: 3 additions & 3 deletions modules/coin/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package coin
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/errors"
"github.com/cosmos/cosmos-sdk/modules/auth"
"github.com/cosmos/cosmos-sdk/modules/ibc"
"github.com/cosmos/cosmos-sdk/stack"
"github.com/cosmos/cosmos-sdk/state"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
wire "github.com/tendermint/go-wire"
)

Expand All @@ -24,7 +24,7 @@ func TestIBCPostPacket(t *testing.T) {

otherID := "chain-2"
ourID := "dex"
start := 200
start := int64(200)

// create the app and our chain
app := stack.New().
Expand Down
5 changes: 3 additions & 2 deletions modules/coin/rest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ func doQueryAccount(w http.ResponseWriter, r *http.Request) {
return
}

var h int
var h int64
qHeight := r.URL.Query().Get("height")
if qHeight != "" {
h, err = strconv.Atoi(qHeight)
_h, err := strconv.Atoi(qHeight)
if err != nil {
common.WriteError(w, err)
return
}
h = int64(_h)
}

actor = coin.ChainAddr(actor)
Expand Down
4 changes: 1 addition & 3 deletions modules/eyes/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package eyes
import (
"fmt"

abci "github.com/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/errors"
)

var (
errMissingData = fmt.Errorf("All tx fields must be filled")

malformed = errors.CodeTypeEncodingError
malformed = errors.CodeTypeEncodingErr
)

//nolint
Expand Down
6 changes: 3 additions & 3 deletions modules/ibc/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestIBCUpdate(t *testing.T) {
// this is the root seed, that others are evaluated against
keys := lite.GenValKeys(7)
appHash := []byte{0, 4, 7, 23}
start := 100 // initial height
start := int64(100) // initial height
root := genEmptyCommit(keys, "chain-1", 100, appHash, len(keys))

keys2 := keys.Extend(2)
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestIBCCreatePacket(t *testing.T) {
// this is the root seed, that others are evaluated against
keys := lite.GenValKeys(7)
appHash := []byte{1, 2, 3, 4}
start := 100 // initial height
start := int64(100) // initial height
chainID := "cosmos-hub"
root := genEmptyCommit(keys, chainID, start, appHash, len(keys))

Expand Down Expand Up @@ -327,7 +327,7 @@ func TestIBCPostPacket(t *testing.T) {

otherID := "chain-1"
ourID := "hub"
start := 200
start := int64(200)
msg := "it's okay"

// create the app and our chain
Expand Down
4 changes: 2 additions & 2 deletions modules/ibc/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func makeCommits(keys lite.ValKeys, count int, chainID, app string) []lite.FullC
// two commits for each validator, to check how we handle dups
// (10, 0), (10, 1), (10, 1), (10, 2), (10, 2), ...
vals := keys.ToValidators(10, int64(count/2))
h := 20 + 10*i
h := int64(20 + 10*i)
commits[i] = keys.GenFullCommit(chainID, h, nil, vals, appHash, 0, len(keys))
}
return commits
Expand Down Expand Up @@ -130,7 +130,7 @@ func checkProvider(t *testing.T, p lite.Provider, chainID, app string) {
fc, err = p.GetByHeight(47)
if assert.Nil(err) {
// we only step by 10, so 40 must be the one below this
assert.Equal(40, fc.Height())
assert.Equal(int64(40), fc.Height())
}

}
5 changes: 3 additions & 2 deletions modules/nonce/rest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ func doQueryNonce(w http.ResponseWriter, r *http.Request) {
return
}

var h int
var h int64
qHeight := r.URL.Query().Get("height")
if qHeight != "" {
h, err = strconv.Atoi(qHeight)
_h, err := strconv.Atoi(qHeight)
if err != nil {
common.WriteError(w, err)
return
}
h = int64(_h)
}

actor = coin.ChainAddr(actor)
Expand Down
3 changes: 1 addition & 2 deletions modules/roles/rest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/gorilla/mux"

abci "github.com/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/client/commands"
"github.com/cosmos/cosmos-sdk/errors"
Expand Down Expand Up @@ -37,7 +36,7 @@ type RoleInput struct {
func decodeRoleHex(roleInHex string) ([]byte, error) {
parsedRole, err := hex.DecodeString(common.StripHex(roleInHex))
if err != nil {
err = errors.WithMessage("invalid hex", err, errors.CodeTypeEncodingError)
err = errors.WithMessage("invalid hex", err, errors.CodeTypeEncodingErr)
return nil, err
}
return parsedRole, nil
Expand Down
Loading

0 comments on commit f00ce7a

Please sign in to comment.