Skip to content

Commit

Permalink
fix(cli): fix output redirection and little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
czar0 committed Dec 29, 2023
1 parent 8952ef8 commit 5ff1d0c
Showing 1 changed file with 50 additions and 65 deletions.
115 changes: 50 additions & 65 deletions cmd/ethkit/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,76 @@ package main

import (
"bytes"
"fmt"
"math"
"strconv"

// "fmt"
// "strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestOKExecuteBalanceCmd(t *testing.T) {
func execute(args string) (string, error) {
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia"})
assert.NoError(t, cmd.Execute())
actual := new(bytes.Buffer)
cmd.SetOut(actual)
cmd.SetErr(actual)
cmd.SetArgs(strings.Split(args, " "))
if err := cmd.Execute(); err != nil {
return "", err
}

return actual.String(), nil
}

// TODO: Fix bad output redirection that makes assert to fail
func TestOKBalanceValidWei(t *testing.T) {
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia"})
assert.NoError(t, cmd.Execute())
// assert.Equal(t, b.String(), fmt.Sprintln(strconv.Itoa(500_000_000_000_000_000), "wei"))
func Test_BalanceCmd(t *testing.T) {
res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia")
assert.Nil(t, err)
assert.NotNil(t, res)
}

// TODO: Fix bad output redirection that makes assert to fail
func TestOKBalanceValidEther(t *testing.T) {
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia", "--ether"})
assert.NoError(t, cmd.Execute())
// assert.Equal(t, b.String(), fmt.Sprintln(strconv.FormatFloat(0.5, 'f', -1, 64), "ether"))
func Test_BalanceCmd_ValidWei(t *testing.T) {
res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia")
assert.Nil(t, err)
assert.Equal(t, res, fmt.Sprintln(strconv.Itoa(500_000_000_000_000_000), "wei"))
}

func TestFailInvalidAddress(t *testing.T) {
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
cmd.SetArgs([]string{"0x1", "--rpc-url", "https://nodes.sequence.app/sepolia"})
assert.Error(t, cmd.Execute())
assert.Contains(t, b.String(), "valid account address")
func Test_BalanceCmd_ValidEther(t *testing.T) {
res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --ether")
assert.Nil(t, err)
assert.Equal(t, res, fmt.Sprintln(strconv.FormatFloat(0.5, 'f', -1, 64), "ether"))
}

func TestFailInvalidRPC(t *testing.T) {
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.Println(b)
cmd.SetErr(b)
cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "nodes.sequence.app/sepolia"})
assert.Error(t, cmd.Execute())
assert.Contains(t, b.String(), "valid rpc url")
func Test_BalanceCmd_InvalidAddress(t *testing.T) {
res, err := execute("0x1 --rpc-url https://nodes.sequence.app/sepolia")
assert.NotNil(t, err)
assert.Empty(t, res)
assert.Contains(t, err.Error(), "please provide a valid account address")
}

func TestFailNotExistingBlockHeigh(t *testing.T) {
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia", "--block", strconv.FormatInt(math.MaxInt64, 10)})
assert.Error(t, cmd.Execute())
assert.Contains(t, b.String(), "jsonrpc error -32000: header not found")
func Test_BalanceCmd_InvalidRPC(t *testing.T) {
res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url nodes.sequence.app/sepolia")
assert.NotNil(t, err)
assert.Empty(t, res)
assert.Contains(t, err.Error(), "please provide a valid rpc url")
}

func TestFailNotAValidStringBlockHeigh(t *testing.T) {
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia", "--block", "something"})
assert.Error(t, cmd.Execute())
assert.Contains(t, b.String(), "invalid block height")
func Test_BalanceCmd_NotExistingBlockHeigh(t *testing.T) {
res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --block " + fmt.Sprint(math.MaxInt64))
assert.NotNil(t, err)
assert.Empty(t, res)
assert.Contains(t, err.Error(), "jsonrpc error -32000: header not found")
}

func TestFailNotAValidNumberBlockHeigh(t *testing.T) {
cmd := NewBalanceCmd()
b := new(bytes.Buffer)
cmd.SetOut(b)
cmd.SetErr(b)
cmd.SetArgs([]string{"0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742", "--rpc-url", "https://nodes.sequence.app/sepolia", "--block", "-100"})
assert.Error(t, cmd.Execute())
func Test_BalanceCmd_NotAValidStringBlockHeigh(t *testing.T) {
res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --block something")
assert.NotNil(t, err)
assert.Empty(t, res)
assert.Contains(t, err.Error(), "invalid block height")
}

func Test_BalanceCmd_NotAValidNumberBlockHeigh(t *testing.T) {
res, err := execute("0x213a286A1AF3Ac010d4F2D66A52DeAf762dF7742 --rpc-url https://nodes.sequence.app/sepolia --block -100")
assert.NotNil(t, err)
assert.Empty(t, res)
}

0 comments on commit 5ff1d0c

Please sign in to comment.