diff --git a/x/pylons/client/cli/query_account_test.go b/x/pylons/client/cli/query_account_test.go new file mode 100644 index 0000000000..420ca55906 --- /dev/null +++ b/x/pylons/client/cli/query_account_test.go @@ -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) + } + }) + } +} diff --git a/x/pylons/client/cli/query_execution_test.go b/x/pylons/client/cli/query_execution_test.go new file mode 100644 index 0000000000..e098e9d046 --- /dev/null +++ b/x/pylons/client/cli/query_execution_test.go @@ -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) + } + }) + } +} diff --git a/x/pylons/client/cli/query_item_test.go b/x/pylons/client/cli/query_item_test.go new file mode 100644 index 0000000000..5d77832ef1 --- /dev/null +++ b/x/pylons/client/cli/query_item_test.go @@ -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) + } + }) + } +}