Skip to content

Commit

Permalink
Merge pull request #10 from hyperledger/testing/unit_tests
Browse files Browse the repository at this point in the history
DA-505 Increase unit test coverage
  • Loading branch information
nguyer authored Oct 9, 2023
2 parents 58a360c + 9644783 commit 2ca51b1
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 5 deletions.
6 changes: 1 addition & 5 deletions internal/tezos/prepare_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *tezosConnector) TransactionPrepare(ctx context.Context, req *ffcapi.Tra

op, err := c.buildOp(ctx, params, req.From, req.To, req.Nonce)
if err != nil {
return nil, ffcapi.ErrorReasonInvalidInputs, err
return nil, "", err
}

return &ffcapi.TransactionPrepareResponse{
Expand Down Expand Up @@ -116,10 +116,6 @@ func (c *tezosConnector) completeOp(ctx context.Context, op *codec.Op, fromStrin
nextCounter = state.Counter + 1
}
for _, op := range op.Contents {
// skip non-manager ops
if op.GetCounter() < 0 {
continue
}
op.WithCounter(nextCounter)
nextCounter++
}
Expand Down
135 changes: 135 additions & 0 deletions internal/tezos/prepare_transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package tezos

import (
"errors"
"testing"

"blockwatch.cc/tzgo/rpc"
"blockwatch.cc/tzgo/tezos"
"github.com/hyperledger/firefly-common/pkg/fftypes"
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestTransactionPrepareOk(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("GetBlockHash", ctx, mock.Anything).
Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil)

mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(&rpc.ContractInfo{
Counter: 10,
Manager: "edpkv89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, nil)

req := &ffcapi.TransactionPrepareRequest{
TransactionInput: ffcapi.TransactionInput{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s",
},
Method: fftypes.JSONAnyPtr("\"pause\""),
Params: []*fftypes.JSONAny{
fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"),
},
},
}
res, reason, err := c.TransactionPrepare(ctx, req)
assert.NoError(t, err)
assert.Empty(t, reason)
assert.NotNil(t, res)
assert.NotEmpty(t, res.TransactionData)
}

func TestTransactionPrepareWrongParamsError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

req := &ffcapi.TransactionPrepareRequest{
TransactionInput: ffcapi.TransactionInput{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s",
},
Method: fftypes.JSONAnyPtr("\"pause\""),
Params: []*fftypes.JSONAny{
fftypes.JSONAnyPtr("wrong"),
},
},
}
_, reason, err := c.TransactionPrepare(ctx, req)
assert.Error(t, err)
assert.Regexp(t, "FF23014", err)
assert.Equal(t, ffcapi.ErrorReasonInvalidInputs, reason)
}

func TestTransactionPrepareWrongToAddressError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

req := &ffcapi.TransactionPrepareRequest{
TransactionInput: ffcapi.TransactionInput{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
To: "wrong",
},
Method: fftypes.JSONAnyPtr("\"pause\""),
Params: []*fftypes.JSONAny{
fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"),
},
},
}
_, _, err := c.TransactionPrepare(ctx, req)
assert.Error(t, err)
assert.Regexp(t, "FF23020", err)
}

func TestTransactionPrepareWrongFromAddressError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

req := &ffcapi.TransactionPrepareRequest{
TransactionInput: ffcapi.TransactionInput{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "wrong",
To: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
},
Method: fftypes.JSONAnyPtr("\"pause\""),
Params: []*fftypes.JSONAny{
fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"),
},
},
}
_, _, err := c.TransactionPrepare(ctx, req)
assert.Error(t, err)
assert.Regexp(t, "FF23019", err)
}

func TestTransactionPrepareGetContractExtError(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("GetBlockHash", ctx, mock.Anything).
Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil)

mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(nil, errors.New("error"))

req := &ffcapi.TransactionPrepareRequest{
TransactionInput: ffcapi.TransactionInput{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s",
},
Method: fftypes.JSONAnyPtr("\"pause\""),
Params: []*fftypes.JSONAny{
fftypes.JSONAnyPtr("{\"entrypoint\":\"pause\",\"value\":{\"prim\":\"True\"}}"),
},
},
}
_, _, err := c.TransactionPrepare(ctx, req)
assert.Error(t, err)
}
151 changes: 151 additions & 0 deletions internal/tezos/send_transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package tezos

import (
"encoding/json"
"errors"
"testing"

"blockwatch.cc/tzgo/rpc"
"blockwatch.cc/tzgo/tezos"
"github.com/hyperledger/firefly-transaction-manager/pkg/ffcapi"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestTransactionSendDecodeStrError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

req := &ffcapi.TransactionSendRequest{
TransactionData: "1",
}
res, reason, err := c.TransactionSend(ctx, req)
assert.Error(t, err)
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs)
assert.Nil(t, res)
}

func TestTransactionSendDecodeOpError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

req := &ffcapi.TransactionSendRequest{}
res, reason, err := c.TransactionSend(ctx, req)
assert.Error(t, err)
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs)
assert.Nil(t, res)
}

func TestTransactionSendWrongFromAddressError(t *testing.T) {
ctx, c, _, done := newTestConnector(t)
defer done()

req := &ffcapi.TransactionSendRequest{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "wrong",
},
TransactionData: "424d426559724d4a704c577271437337555463466155514365574271736a434c6c00889816a17ae688c971be1ad34bfe1990f8fa5e0f000b0000000130a980e6e41028da2cacfca4ddefea252d18bed900ffff05706175736500000002030a",
}
res, _, err := c.TransactionSend(ctx, req)
assert.Error(t, err)
assert.Regexp(t, "FF23019", err)
assert.Nil(t, res)
}

func TestTransactionSendGetContractExtError(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("GetBlockHash", ctx, mock.Anything).
Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil)

mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(nil, errors.New("error"))

req := &ffcapi.TransactionSendRequest{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s",
},
TransactionData: "424d426559724d4a704c577271437337555463466155514365574271736a434c6c00889816a17ae688c971be1ad34bfe1990f8fa5e0f000b0000000130a980e6e41028da2cacfca4ddefea252d18bed900ffff05706175736500000002030a",
}
_, _, err := c.TransactionSend(ctx, req)
assert.Error(t, err)
}

func TestTransactionSendSimulateError(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("GetBlockHash", ctx, mock.Anything).
Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil)

mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(&rpc.ContractInfo{
Counter: 10,
Manager: "edpkv89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, nil)

mRPC.On("Simulate", ctx, mock.Anything, mock.Anything).Return(nil, errors.New("error"))

req := &ffcapi.TransactionSendRequest{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s",
},
TransactionData: "424d426559724d4a704c577271437337555463466155514365574271736a434c6c00889816a17ae688c971be1ad34bfe1990f8fa5e0f000b0000000130a980e6e41028da2cacfca4ddefea252d18bed900ffff05706175736500000002030a",
}
_, _, err := c.TransactionSend(ctx, req)
assert.Error(t, err)
}

func TestTransactionSendWrongSimulateStatusError(t *testing.T) {
ctx, c, mRPC, done := newTestConnector(t)
defer done()

mRPC.On("GetBlockHash", ctx, mock.Anything).
Return(tezos.NewBlockHash([]byte("BMBeYrMJpLWrqCs7UTcFaUQCeWBqsjCLejX5D8zE8m9syHqHnZg")), nil)

mRPC.On("GetContractExt", ctx, mock.Anything, mock.Anything).
Return(&rpc.ContractInfo{
Counter: 10,
Manager: "edpkv89Jj4aVWetK69CWm5ss1LayvK8dQoiFz7p995y1k3E8CZwqJ6",
}, nil)

mRPC.On("Simulate", ctx, mock.Anything, mock.Anything).
Return(&rpc.Receipt{
Op: &rpc.Operation{
Contents: []rpc.TypedOperation{
rpc.Transaction{
Manager: rpc.Manager{
Generic: rpc.Generic{
Metadata: rpc.OperationMetadata{
Result: rpc.OperationResult{
Errors: []rpc.OperationError{
{
GenericError: rpc.GenericError{
ID: "error id",
Kind: "error",
},
Raw: json.RawMessage{},
},
},
},
},
},
},
},
},
},
}, nil)

req := &ffcapi.TransactionSendRequest{
TransactionHeaders: ffcapi.TransactionHeaders{
From: "tz1Y6GnVhC4EpcDDSmD3ibcC4WX6DJ4Q1QLN",
To: "KT1D254HTPKq5GZNVcF73XBinG9BLybHqu8s",
},
TransactionData: "424d426559724d4a704c577271437337555463466155514365574271736a434c6c00889816a17ae688c971be1ad34bfe1990f8fa5e0f000b0000000130a980e6e41028da2cacfca4ddefea252d18bed900ffff05706175736500000002030a",
}
_, _, err := c.TransactionSend(ctx, req)
assert.Error(t, err)
}
4 changes: 4 additions & 0 deletions internal/tezos/tezos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func TestConnectorInit(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, int64(500), cc.(*tezosConnector).catchupThreshold) // set to page size

conf.Set(BlockchainRPC, "wrong rpc")
cc, err = NewTezosConnector(context.Background(), conf)
assert.Regexp(t, "FF23052", err)

conf.Set(ConfigDataFormat, "map")
conf.Set(BlockCacheSize, "-1")
cc, err = NewTezosConnector(context.Background(), conf)
Expand Down

0 comments on commit 2ca51b1

Please sign in to comment.