Skip to content

Commit

Permalink
refactor: CLI tx spend unittest no longer needs a network connection
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Jul 26, 2019
1 parent 9049f6c commit e36bc54
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
10 changes: 6 additions & 4 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ var txSpendCmd = &cobra.Command{
Short: "Create a transaction to another account (unsigned)",
Long: ``,
Args: cobra.ExactArgs(3),
RunE: txSpendFunc,
RunE: func(cmd *cobra.Command, args []string) error {
aeNode := newAeNode()
return txSpendFunc(aeNode, args)
},
}

func txSpendFunc(cmd *cobra.Command, args []string) (err error) {
func txSpendFunc(conn aeternity.GetAccounter, args []string) (err error) {
var (
sender string
recipient string
Expand Down Expand Up @@ -59,8 +62,7 @@ func txSpendFunc(cmd *cobra.Command, args []string) (err error) {

// Connect to the node to find out sender nonce only
if nonce == 0 {
client := aeternity.NewNode(aeternity.Config.Node.URL, false)
nonce, err = aeternity.GetNextNonce(client, sender)
nonce, err = aeternity.GetNextNonce(conn, sender)
if err != nil {
return err
}
Expand Down
31 changes: 25 additions & 6 deletions cmd/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,31 @@ import (
var alice = "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi"
var bob = "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v"

func TestTxSpend(t *testing.T) {
emptyCmd := cobra.Command{}

err := txSpendFunc(&emptyCmd, []string{alice, bob, "10"})
if err != nil {
t.Error(err)
func Test_txSpendFunc(t *testing.T) {
type args struct {
conn aeternity.GetAccounter
args []string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
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"},
},
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 {
t.Errorf("txSpendFunc() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

Expand Down

0 comments on commit e36bc54

Please sign in to comment.