Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dang/add test cli #922

Merged
merged 4 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions x/pylons/client/cli/query_account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package cli_test

import (
"testing"

"github.com/Pylons-tech/pylons/testutil/network"
"github.com/Pylons-tech/pylons/x/pylons/client/cli"
"github.com/Pylons-tech/pylons/x/pylons/types"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
)

func settingUserMapObject(t *testing.T) (*network.Network, *types.UserMap) {
t.Helper()
cfg := network.DefaultConfig()
state := types.GenesisState{}
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state))
accountAddr := types.GenTestBech32List(1)
userMap := types.UserMap{
AccountAddr: accountAddr[0],
Username: "testUserName",
}

state.AccountList = append(state.AccountList, userMap)
buffer, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buffer
return network.New(t, cfg), &userMap
}

func TestCmdGetAddressByUsername(t *testing.T) {
net, userMap := settingUserMapObject(t)
ctx := net.Validators[0].ClientCtx

for _, tc := range []struct {
desc string
userName string
address string
shouldErr bool
}{
{
desc: "Valid",
userName: userMap.Username,
address: userMap.AccountAddr,
shouldErr: false,
},
{
desc: "Invalid UserName",
userName: "Invalid",
address: userMap.AccountAddr,
shouldErr: true,
},
} {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
args := []string{tc.userName}
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdGetAddressByUsername(), args)
if tc.shouldErr {
require.Error(t, err)
var resp types.QueryGetAddressByUsernameResponse
require.Error(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Address)
} else {
require.NoError(t, err)
var resp types.QueryGetAddressByUsernameResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Address)
require.Equal(t, resp.Address.Value, tc.address)
}
})
}
}

func TestCmdGetUsernameByAddress(t *testing.T) {
net, userMap := settingUserMapObject(t)
ctx := net.Validators[0].ClientCtx

for _, tc := range []struct {
desc string
userName string
address string
shouldErr bool
}{
{
desc: "Valid",
userName: userMap.Username,
address: userMap.AccountAddr,
shouldErr: false,
},
{
desc: "Invalid AccountAddr",
userName: userMap.Username,
address: "Invalid",
shouldErr: true,
},
} {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
args := []string{tc.address}
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdGetUsernameByAddress(), args)
if tc.shouldErr {
require.Error(t, err)
var resp types.QueryGetUsernameByAddressResponse
require.Error(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Username)
} else {
require.NoError(t, err)
var resp types.QueryGetUsernameByAddressResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Username)
require.Equal(t, resp.Username.Value, tc.userName)
}
})
}
}
84 changes: 84 additions & 0 deletions x/pylons/client/cli/query_execution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cli_test

import (
"testing"

"github.com/Pylons-tech/pylons/testutil/network"
"github.com/Pylons-tech/pylons/x/pylons/client/cli"
"github.com/Pylons-tech/pylons/x/pylons/types"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
)

func settingExecutionObject(t *testing.T) (*network.Network, *types.Execution, *types.Execution) {
t.Helper()
cfg := network.DefaultConfig()
state := types.GenesisState{}
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state))

execution := types.Execution{
Id: "1",
}
state.ExecutionList = append(state.ExecutionList, execution)
state.ExecutionCount = 1

pendingExecution := types.Execution{
Id: "2",
}
state.PendingExecutionList = append(state.PendingExecutionList, pendingExecution)
state.PendingExecutionCount = 1

buffer, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buffer
return network.New(t, cfg), &execution, &pendingExecution
}

func TestCmdShowExecution(t *testing.T) {
net, execution, pendingExecution := settingExecutionObject(t)
ctx := net.Validators[0].ClientCtx

for _, tc := range []struct {
desc string
executionId string
completed bool
shouldErr bool
}{
{
desc: "Valid - execution",
executionId: execution.Id,
completed: true,
shouldErr: false,
},
{
desc: "Valid - pendingExecution",
executionId: pendingExecution.Id,
completed: false,
shouldErr: false,
},
{
desc: "Invalid - executionId Invalid",
executionId: "Invalid",
shouldErr: true,
},
} {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
args := []string{tc.executionId}
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowExecution(), args)
if tc.shouldErr {
require.Error(t, err)
var resp types.QueryGetExecutionResponse
require.Error(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Execution)
} else {
require.NoError(t, err)
var resp types.QueryGetExecutionResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Execution)
require.Equal(t, resp.Execution.Id, tc.executionId)
require.Equal(t, resp.Completed, tc.completed)
}
})
}
}
79 changes: 79 additions & 0 deletions x/pylons/client/cli/query_item_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cli_test

import (
"testing"

"github.com/Pylons-tech/pylons/testutil/network"
"github.com/Pylons-tech/pylons/x/pylons/client/cli"
"github.com/Pylons-tech/pylons/x/pylons/types"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
)

func settingItemObject(t *testing.T) (*network.Network, *types.Item) {
t.Helper()
cfg := network.DefaultConfig()
state := types.GenesisState{}
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state))

item := types.Item{
CookbookId: "testCookbookId",
Id: "testId",
}
state.ItemList = append(state.ItemList, item)

buffer, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buffer
return network.New(t, cfg), &item
}

func TestCmdShowItem(t *testing.T) {
net, item := settingItemObject(t)
ctx := net.Validators[0].ClientCtx

for _, tc := range []struct {
desc string
CookbookId string
Id string
shouldErr bool
}{
{
desc: "Valid",
CookbookId: item.CookbookId,
Id: item.Id,
shouldErr: false,
},
{
desc: "Invalid - CookbookId",
CookbookId: "Invalid",
Id: item.Id,
shouldErr: true,
},
{
desc: "Invalid - Id",
CookbookId: item.CookbookId,
Id: "Invalid",
shouldErr: true,
},
} {
tc := tc
t.Run(tc.desc, func(t *testing.T) {
args := []string{}
args = append(args, tc.CookbookId, tc.Id)
out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowItem(), args)
if tc.shouldErr {
require.Error(t, err)
var resp types.QueryGetItemResponse
require.Error(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
} else {
require.NoError(t, err)
var resp types.QueryGetItemResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Item)
require.Equal(t, resp.Item.CookbookId, tc.CookbookId)
require.Equal(t, resp.Item.Id, tc.Id)
}
})
}
}