From 6c73a6c062611c2a4db79c3819f8c40abe59adf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Husiaty=C5=84ski?= Date: Thu, 7 Mar 2019 09:48:23 +0100 Subject: [PATCH] cleanup static cash decorator tests --- x/cash/staticfee_test.go | 136 +++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 77 deletions(-) diff --git a/x/cash/staticfee_test.go b/x/cash/staticfee_test.go index 2eceac9b..3d30923e 100644 --- a/x/cash/staticfee_test.go +++ b/x/cash/staticfee_test.go @@ -1,7 +1,6 @@ package cash import ( - "fmt" "testing" "github.com/iov-one/weave" @@ -10,7 +9,6 @@ import ( "github.com/iov-one/weave/gconf" "github.com/iov-one/weave/orm" "github.com/iov-one/weave/store" - "github.com/iov-one/weave/x" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -38,20 +36,6 @@ func (f *feeTx) Unmarshal([]byte) error { return errors.ErrInternal.New("TODO: not implemented") } -type okHandler struct{} - -var _ weave.Handler = okHandler{} - -func (okHandler) Check(weave.Context, weave.KVStore, - weave.Tx) (weave.CheckResult, error) { - return weave.CheckResult{}, nil -} - -func (okHandler) Deliver(weave.Context, weave.KVStore, - weave.Tx) (weave.DeliverResult, error) { - return weave.DeliverResult{}, nil -} - func must(obj orm.Object, err error) orm.Object { if err != nil { panic(err) @@ -60,7 +44,6 @@ func must(obj orm.Object, err error) orm.Object { } func TestFees(t *testing.T) { - var helpers x.TestHelpers cash := coin.NewCoin(50, 0, "FOO") min := coin.NewCoin(0, 1234, "FOO") @@ -68,79 +51,78 @@ func TestFees(t *testing.T) { perm2 := weave.NewCondition("sigs", "ed25519", []byte{3, 4, 5}) perm3 := weave.NewCondition("custom", "type", []byte{0xAB}) - cases := []struct { + cases := map[string]struct { signers []weave.Condition initState []orm.Object fee *FeeInfo min coin.Coin expect checkErr }{ - // no fee given, nothing expected - 0: {nil, nil, nil, coin.Coin{}, noErr}, - // no fee given, something expected - 1: {nil, nil, nil, min, errors.ErrInsufficientAmount.Is}, - // no signer given - 2: {nil, nil, &FeeInfo{Fees: &min}, min, errors.ErrInvalidInput.Is}, - // use default signer, but not enough money - 3: { - []weave.Condition{perm}, - nil, - &FeeInfo{Fees: &min}, - min, - errors.ErrEmpty.Is, + "no fee given, nothing expected": { + min: coin.Coin{}, + expect: noErr, + }, + "no fee given, something expected": { + min: min, + expect: errors.ErrInsufficientAmount.Is, + }, + "no signer given": { + fee: &FeeInfo{Fees: &min}, + min: min, + expect: errors.ErrInvalidInput.Is, + }, + "use default signer, but not enough money": { + signers: []weave.Condition{perm}, + fee: &FeeInfo{Fees: &min}, + min: min, + expect: errors.ErrEmpty.Is, }, - // signer can cover min, but not pledge - 4: { - []weave.Condition{perm}, - []orm.Object{must(WalletWith(perm.Address(), &min))}, - &FeeInfo{Fees: &cash}, - min, - errors.ErrInsufficientAmount.Is, + "signer can cover min, but not pledge": { + signers: []weave.Condition{perm}, + initState: []orm.Object{must(WalletWith(perm.Address(), &min))}, + fee: &FeeInfo{Fees: &cash}, + min: min, + expect: errors.ErrInsufficientAmount.Is, }, - // all proper - 5: { - []weave.Condition{perm}, - []orm.Object{must(WalletWith(perm.Address(), &cash))}, - &FeeInfo{Fees: &min}, - min, - noErr, + "all proper": { + signers: []weave.Condition{perm}, + initState: []orm.Object{must(WalletWith(perm.Address(), &cash))}, + fee: &FeeInfo{Fees: &min}, + min: min, + expect: noErr, }, - // trying to pay from wrong account - 6: { - []weave.Condition{perm}, - []orm.Object{must(WalletWith(perm2.Address(), &cash))}, - &FeeInfo{Payer: perm2.Address(), Fees: &min}, - min, - errors.ErrUnauthorized.Is, + "trying to pay from wrong account": { + signers: []weave.Condition{perm}, + initState: []orm.Object{must(WalletWith(perm2.Address(), &cash))}, + fee: &FeeInfo{Payer: perm2.Address(), Fees: &min}, + min: min, + expect: errors.ErrUnauthorized.Is, }, - // can pay in any fee - 7: { - []weave.Condition{perm}, - []orm.Object{must(WalletWith(perm.Address(), &cash))}, - &FeeInfo{Fees: &min}, - coin.NewCoin(0, 1000, ""), - noErr, + "can pay in any fee": { + signers: []weave.Condition{perm}, + initState: []orm.Object{must(WalletWith(perm.Address(), &cash))}, + fee: &FeeInfo{Fees: &min}, + min: coin.NewCoin(0, 1000, ""), + expect: noErr, }, - // wrong currency checked - 8: { - []weave.Condition{perm}, - []orm.Object{must(WalletWith(perm.Address(), &cash))}, - &FeeInfo{Fees: &min}, - coin.NewCoin(0, 1000, "NOT"), - coin.ErrInvalidCurrency.Is, + "wrong currency checked": { + signers: []weave.Condition{perm}, + initState: []orm.Object{must(WalletWith(perm.Address(), &cash))}, + fee: &FeeInfo{Fees: &min}, + min: coin.NewCoin(0, 1000, "NOT"), + expect: coin.ErrInvalidCurrency.Is, }, - // has the cash, but didn't offer enough fees - 9: { - []weave.Condition{perm}, - []orm.Object{must(WalletWith(perm.Address(), &cash))}, - &FeeInfo{Fees: &min}, - coin.NewCoin(0, 45000, "FOO"), - errors.ErrInsufficientAmount.Is, + "has the cash, but didn't offer enough fees": { + signers: []weave.Condition{perm}, + initState: []orm.Object{must(WalletWith(perm.Address(), &cash))}, + fee: &FeeInfo{Fees: &min}, + min: coin.NewCoin(0, 45000, "FOO"), + expect: errors.ErrInsufficientAmount.Is, }, } - for i, tc := range cases { - t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) { + for testName, tc := range cases { + t.Run(testName, func(t *testing.T) { auth := helpers.Authenticate(tc.signers...) controller := NewController(NewBucket()) h := NewFeeDecorator(auth, controller) @@ -158,9 +140,9 @@ func TestFees(t *testing.T) { tx := &feeTx{tc.fee} - _, err := h.Check(nil, kv, tx, okHandler{}) + _, err := h.Check(nil, kv, tx, helpers.CountingHandler()) assert.True(t, tc.expect(err), "%+v", err) - _, err = h.Deliver(nil, kv, tx, okHandler{}) + _, err = h.Deliver(nil, kv, tx, helpers.CountingHandler()) assert.True(t, tc.expect(err), "%+v", err) }) }