-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from hyperledger/testing/unit_tests
DA-505 Increase unit test coverage
- Loading branch information
Showing
4 changed files
with
291 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters