Skip to content

Commit

Permalink
refactor: tx spend/contract subcommand unittests should rely on Helpe…
Browse files Browse the repository at this point in the history
…rInterface, not API Interfaces. Also, fixed tx spend issue where it was printing what you wanted, instead of the actual tx's parameters
  • Loading branch information
randomshinichi committed Jul 26, 2019
1 parent b31e6b8 commit a4faff0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
37 changes: 17 additions & 20 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import (
"github.com/spf13/cobra"
)

// txCmd implments the tx command
// txCmd implments the tx command. All tx subcommands should work offline,
// without any connection to the node.
var txCmd = &cobra.Command{
Use: "tx SUBCOMMAND [ARGS]...",
Short: "Handle transactions creation",
Expand All @@ -28,12 +29,12 @@ var txSpendCmd = &cobra.Command{
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
u := aeternity.Helpers{}
return txSpendFunc(aeNode, u, args)
u := aeternity.Helpers{Node: aeNode}
return txSpendFunc(u, args)
},
}

func txSpendFunc(conn aeternity.GetAccounter, h aeternity.HelpersInterface, args []string) (err error) {
func txSpendFunc(helpers aeternity.HelpersInterface, args []string) (err error) {
var (
sender string
recipient string
Expand Down Expand Up @@ -63,7 +64,7 @@ func txSpendFunc(conn aeternity.GetAccounter, h aeternity.HelpersInterface, args

// Connect to the node to find out sender nonce only
if nonce == 0 {
nonce, err = h.GetNextNonce(sender)
nonce, err = helpers.GetNextNonce(sender)
if err != nil {
return err
}
Expand All @@ -77,13 +78,13 @@ func txSpendFunc(conn aeternity.GetAccounter, h aeternity.HelpersInterface, args

// Sender, Recipient, Amount, Ttl, Fee, Nonce, Payload, Encoded
Pp(
"Sender acount", sender,
"Recipient account", recipient,
"Amount", amount,
"TTL", ttl,
"Fee", fee,
"Nonce", nonce,
"Payload", spendTxPayload,
"Sender acount", tx.SenderID,
"Recipient account", tx.RecipientID,
"Amount", tx.Amount,
"TTL", tx.TTL,
"Fee", tx.Fee,
"Nonce", tx.Nonce,
"Payload", tx.Payload,
"Encoded", base64Tx,
)
return nil
Expand All @@ -96,8 +97,8 @@ var txContractCreateCmd = &cobra.Command{
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
u := aeternity.Helpers{}
return txContractCreateFunc(aeNode, u, args)
u := aeternity.Helpers{Node: aeNode}
return txContractCreateFunc(u, args)
},
}

Expand All @@ -106,7 +107,7 @@ type getHeightAccounter interface {
aeternity.GetAccounter
}

func txContractCreateFunc(conn aeternity.GetHeightAccountNamer, h aeternity.HelpersInterface, args []string) (err error) {
func txContractCreateFunc(h aeternity.HelpersInterface, args []string) (err error) {
var (
owner string
contract string
Expand All @@ -127,11 +128,7 @@ func txContractCreateFunc(conn aeternity.GetHeightAccountNamer, h aeternity.Help
return errors.New("Error, missing or invalid init calldata bytecode")
}

c := aeternity.Context{
Client: conn,
Address: owner,
Helpers: h.(aeternity.Helpers),
}
c := aeternity.NewContext(owner, h)

tx, err := c.ContractCreateTx(contract, calldata, aeternity.Config.Client.Contracts.VMVersion, aeternity.Config.Client.Contracts.ABIVersion, aeternity.Config.Client.Contracts.Deposit, aeternity.Config.Client.Contracts.Amount, aeternity.Config.Client.Contracts.Gas, aeternity.Config.Client.Contracts.GasPrice, aeternity.Config.Client.Fee)

Expand Down
23 changes: 10 additions & 13 deletions cmd/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var bob = "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v"

func Test_txSpendFunc(t *testing.T) {
type args struct {
conn aeternity.GetAccounter
args []string
helpers aeternity.HelpersInterface
args []string
}
tests := []struct {
name string
Expand All @@ -23,15 +23,15 @@ func Test_txSpendFunc(t *testing.T) {
{
name: "Alice sends 10 to Bob",
args: args{
conn: &mockGetAccounter{account: `{"balance":1600000000000000077131306000000000000000,"id":"ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi","kind":"basic","nonce":0}`},
args: []string{alice, bob, "10"},
helpers: &mockHelpers{},
args: []string{alice, bob, "10"},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := txSpendFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
if err := txSpendFunc(tt.args.helpers, tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("txSpendFunc() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down Expand Up @@ -62,12 +62,9 @@ func TestTxDumpRaw(t *testing.T) {
}

func Test_txContractCreateFunc(t *testing.T) {
aeternity.GetTTLNonce = func(c aeternity.GetHeightAccounter, accountID string, offset uint64) (height uint64, nonce uint64, err error) {
return 2, 1337, nil
}
type args struct {
conn getHeightAccounter
args []string
helpers aeternity.HelpersInterface
args []string
}
tests := []struct {
name string
Expand All @@ -77,15 +74,15 @@ func Test_txContractCreateFunc(t *testing.T) {
{
name: "Deploy SimpleStorage with alice (unsigned)",
args: args{
conn: &mockgetHeightAccounter{},
args: []string{alice, contractSimpleStorageBytecode, contractSimpleStorageInitCalldata},
helpers: &mockHelpers{},
args: []string{alice, contractSimpleStorageBytecode, contractSimpleStorageInitCalldata},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := txContractCreateFunc(tt.args.conn, tt.args.args); (err != nil) != tt.wantErr {
if err := txContractCreateFunc(tt.args.helpers, tt.args.args); (err != nil) != tt.wantErr {
t.Errorf("txContractCreateFunc() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down

0 comments on commit a4faff0

Please sign in to comment.