-
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 #29 from alex-semenyuk/query_invoke
QueryInvoke implementation
- Loading branch information
Showing
3 changed files
with
232 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,164 @@ | ||
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() | ||
|
||
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() | ||
|
||
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() | ||
|
||
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 TestQueryInvokeParseAddressToError(t *testing.T) { | ||
ctx, c, _, done := newTestConnector(t) | ||
defer done() | ||
|
||
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 TestQueryInvokeParseAddressFromError(t *testing.T) { | ||
ctx, c, _, done := newTestConnector(t) | ||
defer done() | ||
|
||
req := &ffcapi.QueryInvokeRequest{ | ||
TransactionInput: ffcapi.TransactionInput{ | ||
Method: fftypes.JSONAnyPtr("\"simple_view\""), | ||
TransactionHeaders: ffcapi.TransactionHeaders{ | ||
From: "t......", | ||
}, | ||
}, | ||
} | ||
|
||
resp, reason, err := c.QueryInvoke(ctx, req) | ||
|
||
assert.Nil(t, resp) | ||
assert.Equal(t, reason, ffcapi.ErrorReasonTransactionReverted) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestQueryInvokeRequestNotDefinedError(t *testing.T) { | ||
ctx, c, _, done := newTestConnector(t) | ||
defer done() | ||
|
||
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