Skip to content

Commit 9c545ac

Browse files
author
gregorgololicic
committed
commands tests
1 parent 94f51af commit 9c545ac

3 files changed

Lines changed: 135 additions & 28 deletions

File tree

test/account_test.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,17 @@ package test
33
import (
44
"testing"
55

6-
"github.com/onflow/flow-go-sdk/crypto"
76
"github.com/stretchr/testify/assert"
8-
9-
"github.com/onflow/flow-go-sdk"
10-
"github.com/sideninja/flow-cli/models"
117
)
128

13-
var sigAlgo = crypto.StringToSignatureAlgorithm("ECDSA_P256")
14-
var pubKey, _ = crypto.DecodePublicKeyHex(sigAlgo, "0a4c9f7bf0c8adf6ebdf4859c11d8e2867e0eaa4af6880e18a0730a0b44a494e976cefa0caf8efb7ec2da469c3f320dab4a2ca72fb340621776f4a1403ae39ed")
15-
16-
var flowAccount = flow.Account{
17-
Address: flow.HexToAddress("01cf0e2f2f715450"),
18-
Balance: 10,
19-
Code: nil,
20-
Keys: []*flow.AccountKey{
21-
&flow.AccountKey{
22-
Index: 1,
23-
PublicKey: pubKey,
24-
SigAlgo: sigAlgo,
25-
HashAlgo: crypto.HashAlgorithm(1),
26-
Weight: 1000,
27-
SequenceNumber: 1,
28-
Revoked: false,
29-
},
30-
},
31-
}
32-
33-
var account = &models.Account{&flowAccount}
9+
var account = GetMockAccount()
3410

35-
func TestAccountEncapsulation(t *testing.T) {
11+
func Test_AccountEncapsulation(t *testing.T) {
3612
assert.Equal(t, account.Address.String(), "01cf0e2f2f715450")
3713
assert.Equal(t, int(account.Balance), 10)
3814
}
3915

40-
func TestAccountStringSerialization(t *testing.T) {
16+
func Test_AccountStringSerialization(t *testing.T) {
4117

4218
assert.Equal(t, account.String("address"), "01cf0e2f2f715450")
4319
assert.Equal(t, account.String("balance"), "10")
@@ -46,7 +22,7 @@ func TestAccountStringSerialization(t *testing.T) {
4622
assert.Equal(t, account.String(""), "\nAddress\t 01cf0e2f2f715450\nBalance\t 10\nKeys\t 1\n\nKey 0\tPublic Key\t\t 0a4c9f7bf0c8adf6ebdf4859c11d8e2867e0eaa4af6880e18a0730a0b44a494e976cefa0caf8efb7ec2da469c3f320dab4a2ca72fb340621776f4a1403ae39ed\n\tWeight\t\t\t 1000\n\tSignature Algorithm\t ECDSA_P256\n\tHash Algorithm\t\t SHA2_256\n\n\nCode\t\t \n")
4723
}
4824

49-
func TestAccountJSONSerialization(t *testing.T) {
25+
func Test_AccountJSONSerialization(t *testing.T) {
5026
assert.Equal(t, account.JSON(""), "{\"Address\":\"01cf0e2f2f715450\",\"Balance\":10,\"Code\":null,\"Keys\":[{\"Index\":1,\"PublicKey\":{},\"SigAlgo\":2,\"HashAlgo\":1,\"Weight\":1000,\"SequenceNumber\":1,\"Revoked\":false}],\"Contracts\":null}")
5127
assert.Equal(t, account.JSON("balance"), "{\"Balance\":10}")
5228
assert.Equal(t, account.JSON("address"), "{\"Address\":\"01cf0e2f2f715450\"}")

test/command_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package test
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"testing"
7+
8+
"github.com/sideninja/flow-cli/cmd/account/get"
9+
10+
"github.com/sideninja/flow-cli/cmd"
11+
"github.com/sideninja/flow-cli/gateway"
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func Test_RootCommand(t *testing.T) {
16+
var gateway gateway.IGateway = GetMockGateway()
17+
b := bytes.NewBufferString("")
18+
19+
command := cmd.NewCmdRoot(gateway, "1.0")
20+
command.SetOut(b)
21+
command.Execute()
22+
23+
out, err := ioutil.ReadAll(b)
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
assert.Equal(t, string(out), "\nFlow CLI tool to interact with flow emulator.\n\nUsage:\n flow [command]\n\nAvailable Commands:\n account Get account information, create account, add account key\n config Save persistent configuration\n help Help about any command\n\nFlags:\n -h, --help help for flow\n -j, --json show output format as JSON\n -v, --version show version information\n\nUse \"flow [command] --help\" for more information about a command.\n")
29+
}
30+
31+
func Test_AccountCommandGeneral(t *testing.T) {
32+
var gateway gateway.IGateway = GetMockGateway()
33+
b := bytes.NewBufferString("")
34+
35+
command := get.NewCmdGet(gateway, "1.0")
36+
command.SetOut(b)
37+
command.Execute()
38+
39+
out, err := ioutil.ReadAll(b)
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
44+
assert.Equal(t, string(out), "Usage:\n get <address> [flags]\n\nAliases:\n get, fetch, g\n\nFlags:\n -a, --api string set discovery api host on the fly only for this request\n -b, --block-height int gets an account at the given block height (default -1)\n -f, --filter string filter output to show only provided field (address, balance, code, keys)\n -h, --help help for get\n -j, --json show output format as JSON\n\n")
45+
}
46+
47+
func Test_AccountTooManyArgs(t *testing.T) {
48+
var gateway gateway.IGateway = GetMockGateway()
49+
b := bytes.NewBufferString("")
50+
51+
command := get.NewCmdGet(gateway, "1.0")
52+
53+
command.SetArgs([]string{"123", "123"})
54+
command.SetOut(b)
55+
command.SetErr(b)
56+
command.Execute()
57+
58+
out, err := ioutil.ReadAll(b)
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
63+
assert.Equal(t, string(out), "Error: accepts 1 arg(s), received 2\nUsage:\n get <address> [flags]\n\nAliases:\n get, fetch, g\n\nFlags:\n -a, --api string set discovery api host on the fly only for this request\n -b, --block-height int gets an account at the given block height (default -1)\n -f, --filter string filter output to show only provided field (address, balance, code, keys)\n -h, --help help for get\n -j, --json show output format as JSON\n\n")
64+
}
65+
66+
func Test_AccountWrongArgs(t *testing.T) {
67+
var gateway gateway.IGateway = GetMockGateway()
68+
b := bytes.NewBufferString("")
69+
70+
command := get.NewCmdGet(gateway, "1.0")
71+
72+
command.SetArgs([]string{})
73+
command.SetOut(b)
74+
command.SetErr(b)
75+
command.Execute()
76+
77+
out, err := ioutil.ReadAll(b)
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
82+
assert.Equal(t, string(out), "Error: accepts 1 arg(s), received 0\nUsage:\n get <address> [flags]\n\nAliases:\n get, fetch, g\n\nFlags:\n -a, --api string set discovery api host on the fly only for this request\n -b, --block-height int gets an account at the given block height (default -1)\n -f, --filter string filter output to show only provided field (address, balance, code, keys)\n -h, --help help for get\n -j, --json show output format as JSON\n\n")
83+
}

test/testhelpers.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package test
2+
3+
import (
4+
"github.com/onflow/flow-go-sdk"
5+
"github.com/onflow/flow-go-sdk/crypto"
6+
"github.com/sideninja/flow-cli/models"
7+
)
8+
9+
// GetMockAccount gets mock account for test
10+
func GetMockAccount() *models.Account {
11+
sigAlgo := crypto.StringToSignatureAlgorithm("ECDSA_P256")
12+
pubKey, _ := crypto.DecodePublicKeyHex(sigAlgo, "0a4c9f7bf0c8adf6ebdf4859c11d8e2867e0eaa4af6880e18a0730a0b44a494e976cefa0caf8efb7ec2da469c3f320dab4a2ca72fb340621776f4a1403ae39ed")
13+
14+
flowAccount := flow.Account{
15+
Address: flow.HexToAddress("01cf0e2f2f715450"),
16+
Balance: 10,
17+
Code: nil,
18+
Keys: []*flow.AccountKey{
19+
&flow.AccountKey{
20+
Index: 1,
21+
PublicKey: pubKey,
22+
SigAlgo: sigAlgo,
23+
HashAlgo: crypto.HashAlgorithm(1),
24+
Weight: 1000,
25+
SequenceNumber: 1,
26+
Revoked: false,
27+
},
28+
},
29+
}
30+
31+
return &models.Account{&flowAccount}
32+
}
33+
34+
// MockGateway for testing
35+
type MockGateway struct{}
36+
37+
// SetAPIURL for testing
38+
func (r *MockGateway) SetAPIURL(url string) {}
39+
40+
// GetAccount for testing
41+
func (r *MockGateway) GetAccount(address string) (*models.Account, error) {
42+
return GetMockAccount(), nil
43+
}
44+
45+
// GetMockGateway for testing
46+
func GetMockGateway() *MockGateway {
47+
return &MockGateway{}
48+
}

0 commit comments

Comments
 (0)