Skip to content

Commit a46bb38

Browse files
Vedaad-Shakiblizhefeng
authored andcommitted
Add tests for iotc cli
1 parent 5e0cf4b commit a46bb38

File tree

10 files changed

+62
-24
lines changed

10 files changed

+62
-24
lines changed

cli/iotc/cmd/balance.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ var balanceCmd = &cobra.Command{
2121
Long: `Returns the current balance of given address.`,
2222
Args: cobra.ExactArgs(1),
2323
Run: func(cmd *cobra.Command, args []string) {
24-
balance(args)
24+
fmt.Println(balance(args))
2525
},
2626
}
2727

28-
func balance(args []string) {
28+
func balance(args []string) string {
2929
client, _ := getClientAndCfg()
3030
balance, err := client.GetAddressBalance(args[0])
3131
if err != nil {
3232
logger.Error().Err(err).Msgf("cannot get balance for address %s", args[0])
33-
return
33+
return ""
3434
}
35-
fmt.Printf("Address %s balance: %d\n", args[0], balance)
35+
return fmt.Sprintf("Address %s balance: %d", args[0], balance)
3636
}
3737

3838
func init() {

cli/iotc/cmd/details.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ var detailsCmd = &cobra.Command{
2121
Long: `Returns the details of given account, namely the balance and the nonce.`,
2222
Args: cobra.ExactArgs(1),
2323
Run: func(cmd *cobra.Command, args []string) {
24-
details(args)
24+
fmt.Println(details(args))
2525
},
2626
}
2727

28-
func details(args []string) {
28+
func details(args []string) string {
2929
client, _ := getClientAndCfg()
3030
det, err := client.GetAddressDetails(args[0])
3131
if err != nil {
3232
logger.Error().Err(err).Msgf("cannot get details for address %s", args[0])
33-
return
33+
return ""
3434
}
35-
fmt.Printf("Address %s nonce: %d\n", args[0], det.Nonce)
36-
fmt.Printf("Address %s balance: %d\n", args[0], det.TotalBalance)
35+
return fmt.Sprintf("Address %s nonce: %d\n", args[0], det.Nonce) +
36+
fmt.Sprintf("Address %s balance: %d", args[0], det.TotalBalance)
3737
}
3838

3939
func init() {

cli/iotc/cmd/height.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ var heightCmd = &cobra.Command{
2121
Long: `Returns the current height of the blockchain.`,
2222
Args: cobra.ExactArgs(0),
2323
Run: func(cmd *cobra.Command, args []string) {
24-
height()
24+
fmt.Println(height())
2525
},
2626
}
2727

28-
func height() {
28+
func height() string {
2929
client, _ := getClientAndCfg()
3030
tip, err := client.GetBlockchainHeight()
3131
if err != nil {
3232
logger.Error().Err(err).Msg("cannot get blockchain height")
33+
return ""
3334
}
34-
fmt.Printf("blockchain height: %d\n", tip)
35+
return fmt.Sprintf("blockchain height: %d", tip)
3536
}
3637

3738
func init() {

cli/iotc/cmd/iotc_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/iotexproject/iotex-core/config"
11+
"github.com/iotexproject/iotex-core/explorer"
12+
)
13+
14+
var configFile = "../../../e2etest/config_local_delegate.yaml"
15+
16+
func Test_All(t *testing.T) {
17+
cfg, err := config.LoadConfigWithPath(configFile)
18+
require.Nil(t, err)
19+
httpPort := cfg.Explorer.Addr
20+
explorer.StartJSONServer(nil, nil, true, httpPort, 0)
21+
22+
s := strings.Split(self(), " ")
23+
addr := s[len(s)-1]
24+
25+
assert.Equal(t, "io1qyqsyqcy8uhx9jtdc2xp5wx7nxyq3xf4c3jmxknzkuej8y", addr)
26+
assert.NotEqual(t, 0, height()) // height is random each time
27+
28+
limit = 10
29+
tr := transfers([]string{addr, "10"})
30+
assert.Equal(t, 9, strings.Count(tr, "\n"))
31+
32+
det := details([]string{addr})
33+
assert.Equal(t, 1, strings.Count(det, "\n"))
34+
assert.NotEqual(t, "", balance([]string{addr})) // no real way to test this because balance returned is random
35+
}

cli/iotc/cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
const (
23-
yamlPath = "/src/github.com/iotexproject/iotex-core/e2etests/config_local_delegate.yaml"
23+
yamlPath = "/src/github.com/iotexproject/iotex-core/e2etest/config_local_delegate.yaml"
2424
localhost = "http://127.0.0.1:"
2525
)
2626

cli/iotc/cmd/self.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ var selfCmd = &cobra.Command{
2121
Short: "Returns this node's address",
2222
Long: `Returns this node's address`,
2323
Run: func(cmd *cobra.Command, args []string) {
24-
self()
24+
fmt.Println(self())
2525
},
2626
}
2727

28-
func self() {
28+
func self() string {
2929
_, cfg := getClientAndCfg()
3030
rawAddr := address(cfg.Chain.ProducerPubKey)
31-
fmt.Printf("this node's address is %s\n", rawAddr)
31+
return fmt.Sprintf("this node's address is %s", rawAddr)
3232
}
3333

3434
func address(pubkey string) string {

cli/iotc/cmd/transfers.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ var transfersCmd = &cobra.Command{
2323
Long: `Returns the transfers associated with a given address`,
2424
Args: cobra.ExactArgs(1),
2525
Run: func(cmd *cobra.Command, args []string) {
26-
transfers(args)
26+
fmt.Println(transfers(args))
2727
},
2828
}
2929

30-
func transfers(args []string) {
30+
func transfers(args []string) string {
3131
client, _ := getClientAndCfg()
3232
transfers, err := client.GetTransfersByAddress(args[0], 0, int64(limit))
3333
if err != nil {
3434
logger.Error().Err(err).Msgf("cannot get transfers for address %s", args[0])
35-
return
35+
return ""
3636
}
37+
var res string
3738
for _, t := range transfers {
38-
fmt.Printf("%+v\n", t)
39+
res += fmt.Sprintf("%+v\n", t)
3940
}
41+
return res[:len(res)-1] // get rid of the extra \n at the end
4042
}
4143

4244
func init() {

server/itx/itxserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Server struct {
3636
// NewServer creates a new server
3737
func NewServer(cfg config.Config) *Server {
3838
// create StateFactory
39-
sf, err := state.NewFactoryFromTrieDBPath(cfg.Chain.TrieDBPath)
39+
sf, err := state.NewFactoryFromTrieDBPath(cfg.Chain.TrieDBPath, false)
4040
if err != nil {
4141
logger.Error().Err(err).Msg("Failed to create statefactory")
4242
return nil

simulator/consensus_sim_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (s *server) Init(in *pb.InitRequest, stream pb.Simulator_InitServer) error
101101
// set chain database path
102102
cfg.Chain.ChainDBPath = "./chain" + strconv.Itoa(i) + ".db"
103103

104-
sf, _ := state.NewFactoryFromTrieDBPath(cfg.Chain.TrieDBPath)
104+
sf, _ := state.NewFactoryFromTrieDBPath(cfg.Chain.TrieDBPath, false)
105105
bc := blockchain.CreateBlockchain(cfg, sf)
106106

107107
if i >= int(in.NFS+in.NHonest) { // is byzantine node

state/factory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ func NewFactory(tr trie.Trie) Factory {
8989
}
9090

9191
// NewFactoryFromTrieDBPath creates a new stateFactory from give trie db path.
92-
func NewFactoryFromTrieDBPath(dbPath string) (Factory, error) {
92+
func NewFactoryFromTrieDBPath(dbPath string, inMem bool) (Factory, error) {
9393
if len(dbPath) == 0 {
9494
// TODO not return error here is a hack
9595
return nil, nil
9696
}
97-
tr, err := trie.NewTrie(dbPath, false)
97+
tr, err := trie.NewTrie(dbPath, inMem)
9898
if err != nil {
9999
return nil, err
100100
}

0 commit comments

Comments
 (0)