From 5c15505c8113cfcc94291c19a8a46aa7afdb6a10 Mon Sep 17 00:00:00 2001
From: Damian Nolan <damiannolan@gmail.com>
Date: Thu, 2 Sep 2021 10:36:39 +0200
Subject: [PATCH] adding ica grpc query tests

---
 .../keeper/grpc_query_test.go                 | 80 +++++++++++++++++++
 .../keeper/keeper_test.go                     |  7 ++
 2 files changed, 87 insertions(+)
 create mode 100644 modules/apps/27-interchain-accounts/keeper/grpc_query_test.go

diff --git a/modules/apps/27-interchain-accounts/keeper/grpc_query_test.go b/modules/apps/27-interchain-accounts/keeper/grpc_query_test.go
new file mode 100644
index 00000000000..6841f45963b
--- /dev/null
+++ b/modules/apps/27-interchain-accounts/keeper/grpc_query_test.go
@@ -0,0 +1,80 @@
+package keeper_test
+
+import (
+	"fmt"
+
+	sdk "github.com/cosmos/cosmos-sdk/types"
+	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
+
+	"github.com/cosmos/ibc-go/modules/apps/27-interchain-accounts/types"
+)
+
+func (suite *KeeperTestSuite) TestQueryInterchainAccountAddress() {
+	var (
+		req     *types.QueryInterchainAccountAddressRequest
+		expAddr string
+	)
+
+	testCases := []struct {
+		msg      string
+		malleate func()
+		expPass  bool
+	}{
+		{
+			"empty request",
+			func() {
+				req = &types.QueryInterchainAccountAddressRequest{}
+			},
+			false,
+		},
+		{
+			"invalid counterparty portID",
+			func() {
+				req = &types.QueryInterchainAccountAddressRequest{
+					CounterpartyPortId: "",
+				}
+			},
+			false,
+		},
+		{
+			"interchain account address not found",
+			func() {
+				req = &types.QueryInterchainAccountAddressRequest{
+					CounterpartyPortId: "ics-27",
+				}
+			},
+			false,
+		},
+		{
+			"success",
+			func() {
+				expAddr = authtypes.NewBaseAccountWithAddress(types.GenerateAddress("ics-27")).GetAddress().String()
+				req = &types.QueryInterchainAccountAddressRequest{
+					CounterpartyPortId: "ics-27",
+				}
+
+				suite.chainA.GetSimApp().ICAKeeper.SetInterchainAccountAddress(suite.chainA.GetContext(), "ics-27", expAddr)
+			},
+			true,
+		},
+	}
+
+	for _, tc := range testCases {
+		suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
+			suite.SetupTest() // reset
+
+			tc.malleate()
+			ctx := sdk.WrapSDKContext(suite.chainA.GetContext())
+
+			res, err := suite.queryClient.InterchainAccountAddress(ctx, req)
+
+			if tc.expPass {
+				suite.Require().NoError(err)
+				suite.Require().NotNil(res)
+				suite.Require().Equal(expAddr, res.GetInterchainAccountAddress())
+			} else {
+				suite.Require().Error(err)
+			}
+		})
+	}
+}
diff --git a/modules/apps/27-interchain-accounts/keeper/keeper_test.go b/modules/apps/27-interchain-accounts/keeper/keeper_test.go
index d3cc61c8d6a..06e0e2530c8 100644
--- a/modules/apps/27-interchain-accounts/keeper/keeper_test.go
+++ b/modules/apps/27-interchain-accounts/keeper/keeper_test.go
@@ -3,6 +3,7 @@ package keeper_test
 import (
 	"testing"
 
+	"github.com/cosmos/cosmos-sdk/baseapp"
 	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
 	"github.com/stretchr/testify/suite"
 
@@ -20,6 +21,8 @@ type KeeperTestSuite struct {
 	chainA *ibctesting.TestChain
 	chainB *ibctesting.TestChain
 	chainC *ibctesting.TestChain
+
+	queryClient types.QueryClient
 }
 
 func (suite *KeeperTestSuite) SetupTest() {
@@ -27,6 +30,10 @@ func (suite *KeeperTestSuite) SetupTest() {
 	suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0))
 	suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1))
 	suite.chainC = suite.coordinator.GetChain(ibctesting.GetChainID(2))
+
+	queryHelper := baseapp.NewQueryServerTestHelper(suite.chainA.GetContext(), suite.chainA.GetSimApp().InterfaceRegistry())
+	types.RegisterQueryServer(queryHelper, suite.chainA.GetSimApp().ICAKeeper)
+	suite.queryClient = types.NewQueryClient(queryHelper)
 }
 
 func NewICAPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path {