Skip to content

Commit

Permalink
chore(gnoclient): modify Run to use baseTxCfg & multiple MsgRun
Browse files Browse the repository at this point in the history
… args (gnolang#1637)
  • Loading branch information
leohhhn authored Feb 23, 2024
1 parent eca95e6 commit 0c3d4ae
Show file tree
Hide file tree
Showing 4 changed files with 566 additions and 83 deletions.
347 changes: 343 additions & 4 deletions gno.land/pkg/gnoclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/std"
)

func TestClient_Render(t *testing.T) {
func TestRender(t *testing.T) {
t.Parallel()
testRealmPath := "gno.land/r/demo/deep/very/deep"
expectedRender := []byte("it works!")
Expand Down Expand Up @@ -54,7 +54,8 @@ func TestClient_Render(t *testing.T) {
assert.Equal(t, data.Response.Data, expectedRender)
}

func TestClient_CallSingle(t *testing.T) {
// Call tests
func TestCallSingle(t *testing.T) {
t.Parallel()

client := Client{
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestClient_CallSingle(t *testing.T) {
assert.Equal(t, string(res.DeliverTx.Data), "it works!")
}

func TestClient_CallMultiple(t *testing.T) {
func TestCallMultiple(t *testing.T) {
t.Parallel()

client := Client{
Expand Down Expand Up @@ -178,7 +179,7 @@ func TestClient_CallMultiple(t *testing.T) {
assert.NotNil(t, res)
}

func TestClient_Call_Errors(t *testing.T) {
func TestCallErrors(t *testing.T) {
t.Parallel()

testCases := []struct {
Expand Down Expand Up @@ -350,6 +351,7 @@ func TestClient_Call_Errors(t *testing.T) {
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -549,3 +551,340 @@ func TestClient_Send_Errors(t *testing.T) {
})
}
}

// Run tests
func TestRunSingle(t *testing.T) {
t.Parallel()

client := Client{
Signer: &mockSigner{
sign: func(cfg SignCfg) (*std.Tx, error) {
return &std.Tx{}, nil
},
info: func() keys.Info {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
},
},
RPCClient: &mockRPCClient{
broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
res := &ctypes.ResultBroadcastTxCommit{
DeliverTx: abci.ResponseDeliverTx{
ResponseBase: abci.ResponseBase{
Data: []byte("hi gnoclient!\n"),
},
},
}
return res, nil
},
},
}

cfg := BaseTxCfg{
GasWanted: 100000,
GasFee: "10000ugnot",
AccountNumber: 1,
SequenceNumber: 1,
Memo: "Test memo",
}

fileBody := `package main
import (
"std"
"gno.land/p/demo/ufmt"
"gno.land/r/demo/deep/very/deep"
)
func main() {
println(ufmt.Sprintf("%s", deep.Render("gnoclient!")))
}`

msg := MsgRun{
Package: &std.MemPackage{
Files: []*std.MemFile{
{
Name: "main.gno",
Body: fileBody,
},
},
},
Send: "",
}

res, err := client.Run(cfg, msg)
assert.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, "hi gnoclient!\n", string(res.DeliverTx.Data))
}

func TestRunMultiple(t *testing.T) {
t.Parallel()

client := Client{
Signer: &mockSigner{
sign: func(cfg SignCfg) (*std.Tx, error) {
return &std.Tx{}, nil
},
info: func() keys.Info {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
},
},
RPCClient: &mockRPCClient{
broadcastTxCommit: func(tx types.Tx) (*ctypes.ResultBroadcastTxCommit, error) {
res := &ctypes.ResultBroadcastTxCommit{
DeliverTx: abci.ResponseDeliverTx{
ResponseBase: abci.ResponseBase{
Data: []byte("hi gnoclient!\nhi gnoclient!\n"),
},
},
}
return res, nil
},
},
}

cfg := BaseTxCfg{
GasWanted: 100000,
GasFee: "10000ugnot",
AccountNumber: 1,
SequenceNumber: 1,
Memo: "Test memo",
}

fileBody := `package main
import (
"std"
"gno.land/p/demo/ufmt"
"gno.land/r/demo/deep/very/deep"
)
func main() {
println(ufmt.Sprintf("%s", deep.Render("gnoclient!")))
}`

msg1 := MsgRun{
Package: &std.MemPackage{
Files: []*std.MemFile{
{
Name: "main1.gno",
Body: fileBody,
},
},
},
Send: "",
}

msg2 := MsgRun{
Package: &std.MemPackage{
Files: []*std.MemFile{
{
Name: "main2.gno",
Body: fileBody,
},
},
},
Send: "",
}

res, err := client.Run(cfg, msg1, msg2)
assert.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, "hi gnoclient!\nhi gnoclient!\n", string(res.DeliverTx.Data))
}

func TestRunErrors(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
client Client
cfg BaseTxCfg
msgs []MsgRun
expectedError error
}{
{
name: "Invalid Signer",
client: Client{
Signer: nil,
RPCClient: &mockRPCClient{},
},
cfg: BaseTxCfg{
GasWanted: 100000,
GasFee: "10000ugnot",
AccountNumber: 1,
SequenceNumber: 1,
Memo: "Test memo",
},
msgs: []MsgRun{
{
Package: &std.MemPackage{
Name: "",
Path: "",
Files: []*std.MemFile{
{
Name: "file1.gno",
Body: "",
},
},
},
Send: "",
},
},
expectedError: ErrMissingSigner,
},
{
name: "Invalid RPCClient",
client: Client{
&mockSigner{},
nil,
},
cfg: BaseTxCfg{
GasWanted: 100000,
GasFee: "10000ugnot",
AccountNumber: 1,
SequenceNumber: 1,
Memo: "Test memo",
},
msgs: []MsgRun{},
expectedError: ErrMissingRPCClient,
},
{
name: "Invalid Gas Fee",
client: Client{
Signer: &mockSigner{},
RPCClient: &mockRPCClient{},
},
cfg: BaseTxCfg{
GasWanted: 100000,
GasFee: "",
AccountNumber: 1,
SequenceNumber: 1,
Memo: "Test memo",
},
msgs: []MsgRun{
{
Package: &std.MemPackage{
Name: "",
Path: "",
Files: []*std.MemFile{
{
Name: "file1.gno",
Body: "",
},
},
},
Send: "",
},
},
expectedError: ErrInvalidGasFee,
},
{
name: "Negative Gas Wanted",
client: Client{
Signer: &mockSigner{},
RPCClient: &mockRPCClient{},
},
cfg: BaseTxCfg{
GasWanted: -1,
GasFee: "10000ugnot",
AccountNumber: 1,
SequenceNumber: 1,
Memo: "Test memo",
},
msgs: []MsgRun{
{
Package: &std.MemPackage{
Name: "",
Path: "",
Files: []*std.MemFile{
{
Name: "file1.gno",
Body: "",
},
},
},
Send: "",
},
},
expectedError: ErrInvalidGasWanted,
},
{
name: "0 Gas Wanted",
client: Client{
Signer: &mockSigner{},
RPCClient: &mockRPCClient{},
},
cfg: BaseTxCfg{
GasWanted: 0,
GasFee: "10000ugnot",
AccountNumber: 1,
SequenceNumber: 1,
Memo: "Test memo",
},
msgs: []MsgRun{
{
Package: &std.MemPackage{
Name: "",
Path: "",
Files: []*std.MemFile{
{
Name: "file1.gno",
Body: "",
},
},
},
Send: "",
},
},
expectedError: ErrInvalidGasWanted,
},
{
name: "Invalid Empty Package",
client: Client{
Signer: &mockSigner{
info: func() keys.Info {
return &mockKeysInfo{
getAddress: func() crypto.Address {
adr, _ := crypto.AddressFromBech32("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
return adr
},
}
},
},
RPCClient: &mockRPCClient{},
},
cfg: BaseTxCfg{
GasWanted: 100000,
GasFee: "10000ugnot",
AccountNumber: 1,
SequenceNumber: 1,
Memo: "Test memo",
},
msgs: []MsgRun{
{
Package: nil,
Send: "",
},
},
expectedError: ErrEmptyPackage,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

res, err := tc.client.Run(tc.cfg, tc.msgs...)
assert.Nil(t, res)
assert.ErrorIs(t, err, tc.expectedError)
})
}
}
Loading

0 comments on commit 0c3d4ae

Please sign in to comment.