-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexey Semenyuk <[email protected]>
- Loading branch information
1 parent
a2233eb
commit 2a5364a
Showing
3 changed files
with
264 additions
and
11 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,184 @@ | ||
package tezos | ||
|
||
import ( | ||
"math/big" | ||
"os" | ||
"testing" | ||
|
||
"blockwatch.cc/tzgo/micheline" | ||
"blockwatch.cc/tzgo/rpc" | ||
"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 TestQueryInvokeSuccess(t *testing.T) { | ||
ctx, c, mRPC, done := newTestConnector(t) | ||
defer done() | ||
|
||
os.Setenv("ENV", "test") | ||
req := &ffcapi.QueryInvokeRequest{ | ||
TransactionInput: ffcapi.TransactionInput{ | ||
Method: fftypes.JSONAnyPtr("\"simple_view\""), | ||
}, | ||
} | ||
res := rpc.RunViewResponse{ | ||
Data: micheline.Prim{ | ||
Type: micheline.PrimString, | ||
String: "3", | ||
}, | ||
} | ||
mRPC.On("RunView", ctx, mock.Anything, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) { | ||
arg := args.Get(3).(*rpc.RunViewResponse) | ||
*arg = res | ||
}) | ||
|
||
resp, reason, err := c.QueryInvoke(ctx, req) | ||
|
||
assert.NotNil(t, resp) | ||
assert.Equal(t, resp.Outputs.String(), "\"3\"") | ||
assert.Empty(t, reason) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestQueryInvokeSuccessArray(t *testing.T) { | ||
ctx, c, mRPC, done := newTestConnector(t) | ||
defer done() | ||
|
||
os.Setenv("ENV", "test") | ||
req := &ffcapi.QueryInvokeRequest{ | ||
TransactionInput: ffcapi.TransactionInput{ | ||
Method: fftypes.JSONAnyPtr("\"simple_view\""), | ||
}, | ||
} | ||
res := rpc.RunViewResponse{ | ||
Data: micheline.Prim{ | ||
Type: micheline.PrimSequence, | ||
OpCode: micheline.D_PAIR, | ||
Args: []micheline.Prim{ | ||
{Type: micheline.PrimString, String: "str"}, | ||
{Type: micheline.PrimInt, Int: big.NewInt(1)}, | ||
}, | ||
}, | ||
} | ||
mRPC.On("RunView", ctx, mock.Anything, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) { | ||
arg := args.Get(3).(*rpc.RunViewResponse) | ||
*arg = res | ||
}) | ||
|
||
resp, reason, err := c.QueryInvoke(ctx, req) | ||
|
||
assert.NotNil(t, resp) | ||
assert.Equal(t, resp.Outputs.String(), "[\"str\",\"1\"]") | ||
assert.Empty(t, reason) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestQueryInvokeRunViewError(t *testing.T) { | ||
ctx, c, mRPC, done := newTestConnector(t) | ||
defer done() | ||
|
||
os.Setenv("ENV", "test") | ||
req := &ffcapi.QueryInvokeRequest{ | ||
TransactionInput: ffcapi.TransactionInput{ | ||
Method: fftypes.JSONAnyPtr("\"simple_view\""), | ||
}, | ||
} | ||
mRPC.On("RunView", ctx, mock.Anything, mock.Anything, mock.Anything).Return(assert.AnError) | ||
|
||
resp, reason, err := c.QueryInvoke(ctx, req) | ||
|
||
assert.Nil(t, resp) | ||
assert.Equal(t, reason, ffcapi.ErrorReasonTransactionReverted) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestQueryInvokeWrongParamsError(t *testing.T) { | ||
ctx, c, _, done := newTestConnector(t) | ||
defer done() | ||
|
||
os.Setenv("ENV", "test") | ||
req := &ffcapi.QueryInvokeRequest{ | ||
TransactionInput: ffcapi.TransactionInput{ | ||
Method: fftypes.JSONAnyPtr("\"simple_view\""), | ||
Params: []*fftypes.JSONAny{ | ||
fftypes.JSONAnyPtr("wrong"), | ||
}, | ||
}, | ||
} | ||
|
||
resp, reason, err := c.QueryInvoke(ctx, req) | ||
|
||
assert.Nil(t, resp) | ||
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestQueryInvokeParseAddressError(t *testing.T) { | ||
ctx, c, _, done := newTestConnector(t) | ||
defer done() | ||
|
||
os.Setenv("ENV", "test") | ||
req := &ffcapi.QueryInvokeRequest{ | ||
TransactionInput: ffcapi.TransactionInput{ | ||
Method: fftypes.JSONAnyPtr("\"simple_view\""), | ||
TransactionHeaders: ffcapi.TransactionHeaders{ | ||
To: "t......", | ||
}, | ||
}, | ||
} | ||
|
||
resp, reason, err := c.QueryInvoke(ctx, req) | ||
|
||
assert.Nil(t, resp) | ||
assert.Equal(t, reason, ffcapi.ErrorReasonTransactionReverted) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestQueryInvokeViewNameMissingError(t *testing.T) { | ||
ctx, c, _, done := newTestConnector(t) | ||
defer done() | ||
|
||
os.Setenv("ENV", "test") | ||
req := &ffcapi.QueryInvokeRequest{ | ||
TransactionInput: ffcapi.TransactionInput{ | ||
Method: fftypes.JSONAnyPtr(""), | ||
}, | ||
} | ||
|
||
resp, reason, err := c.QueryInvoke(ctx, req) | ||
|
||
assert.Nil(t, resp) | ||
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestQueryInvokeMethodNotDefinedError(t *testing.T) { | ||
ctx, c, _, done := newTestConnector(t) | ||
defer done() | ||
|
||
os.Setenv("ENV", "test") | ||
req := &ffcapi.QueryInvokeRequest{ | ||
TransactionInput: ffcapi.TransactionInput{}, | ||
} | ||
|
||
resp, reason, err := c.QueryInvoke(ctx, req) | ||
|
||
assert.Nil(t, resp) | ||
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestQueryInvokeRequestNotDefinedError(t *testing.T) { | ||
ctx, c, _, done := newTestConnector(t) | ||
defer done() | ||
|
||
os.Setenv("ENV", "test") | ||
|
||
resp, reason, err := c.QueryInvoke(ctx, nil) | ||
|
||
assert.Nil(t, resp) | ||
assert.Equal(t, reason, ffcapi.ErrorReasonInvalidInputs) | ||
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