generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #335 from okp4/test/logic-grpc-test
🧪 Add more tests on logic module
- Loading branch information
Showing
6 changed files
with
420 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package keeper_test | ||
|
||
import ( | ||
gocontext "context" | ||
"fmt" | ||
"io/fs" | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
"github.com/golang/mock/gomock" | ||
"github.com/okp4/okp4d/x/logic" | ||
"github.com/okp4/okp4d/x/logic/keeper" | ||
logictestutil "github.com/okp4/okp4d/x/logic/testutil" | ||
"github.com/okp4/okp4d/x/logic/types" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestGRPCAsk(t *testing.T) { | ||
Convey("Given a test cases", t, func() { | ||
cases := []struct { | ||
program string | ||
query string | ||
expectedAsnwer types.Answer | ||
}{ | ||
{ | ||
program: "father(bob, alice).", | ||
query: "father(bob, X).", | ||
expectedAsnwer: types.Answer{ | ||
Success: true, | ||
HasMore: false, | ||
Variables: []string{"X"}, | ||
Results: []types.Result{{Substitutions: []types.Substitution{{ | ||
Variable: "X", | ||
Term: types.Term{ | ||
Name: "alice", | ||
Arguments: nil, | ||
}, | ||
}}}}, | ||
}, | ||
}, | ||
} | ||
|
||
for nc, tc := range cases { | ||
Convey( | ||
fmt.Sprintf("Given test case #%d with program: %v and query: %v", nc, tc.program, tc.query), | ||
func() { | ||
encCfg := moduletestutil.MakeTestEncodingConfig(logic.AppModuleBasic{}) | ||
key := storetypes.NewKVStoreKey(types.StoreKey) | ||
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) | ||
|
||
// gomock initializations | ||
ctrl := gomock.NewController(t) | ||
accountKeeper := logictestutil.NewMockAccountKeeper(ctrl) | ||
bankKeeper := logictestutil.NewMockBankKeeper(ctrl) | ||
fsProvider := logictestutil.NewMockFS(ctrl) | ||
|
||
logicKeeper := keeper.NewKeeper( | ||
encCfg.Codec, | ||
key, | ||
key, | ||
authtypes.NewModuleAddress(govtypes.ModuleName), | ||
accountKeeper, | ||
bankKeeper, | ||
func(ctx gocontext.Context) fs.FS { | ||
return fsProvider | ||
}, | ||
) | ||
err := logicKeeper.SetParams(testCtx.Ctx, types.DefaultParams()) | ||
|
||
So(err, ShouldBeNil) | ||
|
||
Convey("and given a query with program and query to grpc", func() { | ||
queryHelper := baseapp.NewQueryServerTestHelper(testCtx.Ctx, encCfg.InterfaceRegistry) | ||
types.RegisterQueryServiceServer(queryHelper, logicKeeper) | ||
|
||
queryClient := types.NewQueryServiceClient(queryHelper) | ||
|
||
query := types.QueryServiceAskRequest{ | ||
Program: tc.program, | ||
Query: tc.query, | ||
} | ||
|
||
Convey("when the grpc query ask is called", func() { | ||
result, err := queryClient.Ask(gocontext.Background(), &query) | ||
|
||
Convey("Then it should return the expected answer", func() { | ||
So(err, ShouldBeNil) | ||
So(result, ShouldNotBeNil) | ||
So(*result.Answer, ShouldResemble, tc.expectedAsnwer) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,94 @@ | ||
package keeper_test | ||
|
||
import ( | ||
gocontext "context" | ||
"fmt" | ||
"io/fs" | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
"github.com/golang/mock/gomock" | ||
"github.com/okp4/okp4d/x/logic" | ||
"github.com/okp4/okp4d/x/logic/keeper" | ||
logictestutil "github.com/okp4/okp4d/x/logic/testutil" | ||
"github.com/okp4/okp4d/x/logic/types" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestParamsQuery(t *testing.T) { | ||
// TODO implement me | ||
func TestGRPCParams(t *testing.T) { | ||
Convey("Given a test cases", t, func() { | ||
cases := []struct { | ||
params types.Params | ||
}{ | ||
{ | ||
params: types.NewParams( | ||
types.NewInterpreter( | ||
types.WithBootstrap("bootstrap"), | ||
types.WithPredicatesBlacklist([]string{"halt/1"}), | ||
types.WithPredicatesWhitelist([]string{"source_file/1"}), | ||
types.WithVirtualFilesBlacklist([]string{"file1"}), | ||
types.WithVirtualFilesWhitelist([]string{"file2"}), | ||
), | ||
types.NewLimits( | ||
types.WithMaxGas(math.NewUint(1)), | ||
types.WithMaxSize(math.NewUint(2)), | ||
types.WithMaxResultCount(math.NewUint(3)), | ||
types.WithMaxUserOutputSize(math.NewUint(4)), | ||
), | ||
), | ||
}, | ||
} | ||
|
||
for nc, tc := range cases { | ||
Convey( | ||
fmt.Sprintf("Given test case #%d with params: %v", nc, tc.params), func() { | ||
encCfg := moduletestutil.MakeTestEncodingConfig(logic.AppModuleBasic{}) | ||
key := storetypes.NewKVStoreKey(types.StoreKey) | ||
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) | ||
|
||
// gomock initializations | ||
ctrl := gomock.NewController(t) | ||
accountKeeper := logictestutil.NewMockAccountKeeper(ctrl) | ||
bankKeeper := logictestutil.NewMockBankKeeper(ctrl) | ||
fsProvider := logictestutil.NewMockFS(ctrl) | ||
|
||
logicKeeper := keeper.NewKeeper( | ||
encCfg.Codec, | ||
key, | ||
key, | ||
authtypes.NewModuleAddress(govtypes.ModuleName), | ||
accountKeeper, | ||
bankKeeper, | ||
func(ctx gocontext.Context) fs.FS { | ||
return fsProvider | ||
}, | ||
) | ||
|
||
Convey("and given params to the keeper", func() { | ||
err := logicKeeper.SetParams(testCtx.Ctx, tc.params) | ||
So(err, ShouldBeNil) | ||
|
||
queryHelper := baseapp.NewQueryServerTestHelper(testCtx.Ctx, encCfg.InterfaceRegistry) | ||
types.RegisterQueryServiceServer(queryHelper, logicKeeper) | ||
|
||
queryClient := types.NewQueryServiceClient(queryHelper) | ||
|
||
Convey("when the grpc query params is called", func() { | ||
params, err := queryClient.Params(gocontext.Background(), &types.QueryServiceParamsRequest{}) | ||
|
||
Convey("Then it should return the expected params set to the keeper", func() { | ||
So(err, ShouldBeNil) | ||
So(params.Params, ShouldResemble, tc.params) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package keeper_test | ||
|
||
import ( | ||
gocontext "context" | ||
"fmt" | ||
"io/fs" | ||
"testing" | ||
|
||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
"github.com/golang/mock/gomock" | ||
"github.com/okp4/okp4d/x/logic" | ||
"github.com/okp4/okp4d/x/logic/keeper" | ||
logictestutil "github.com/okp4/okp4d/x/logic/testutil" | ||
"github.com/okp4/okp4d/x/logic/types" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestUpdateParams(t *testing.T) { | ||
Convey("Given a test cases", t, func() { | ||
cases := []struct { | ||
name string | ||
request *types.MsgUpdateParams | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "set invalid authority", | ||
request: &types.MsgUpdateParams{ | ||
Authority: "foo", | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "set full valid params", | ||
request: &types.MsgUpdateParams{ | ||
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(), | ||
Params: types.DefaultParams(), | ||
}, | ||
expectErr: false, | ||
}, | ||
} | ||
|
||
for nc, tc := range cases { | ||
Convey( | ||
fmt.Sprintf("Given test case #%d: %v, with request: %v", nc, tc.name, tc.request), func() { | ||
encCfg := moduletestutil.MakeTestEncodingConfig(logic.AppModuleBasic{}) | ||
key := storetypes.NewKVStoreKey(types.StoreKey) | ||
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")) | ||
|
||
// gomock initializations | ||
ctrl := gomock.NewController(t) | ||
accountKeeper := logictestutil.NewMockAccountKeeper(ctrl) | ||
bankKeeper := logictestutil.NewMockBankKeeper(ctrl) | ||
fsProvider := logictestutil.NewMockFS(ctrl) | ||
|
||
logicKeeper := keeper.NewKeeper( | ||
encCfg.Codec, | ||
key, | ||
key, | ||
authtypes.NewModuleAddress(govtypes.ModuleName), | ||
accountKeeper, | ||
bankKeeper, | ||
func(ctx gocontext.Context) fs.FS { | ||
return fsProvider | ||
}, | ||
) | ||
|
||
msgServer := keeper.NewMsgServerImpl(*logicKeeper) | ||
|
||
Convey("when call msg server to update params", func() { | ||
res, err := msgServer.UpdateParams(testCtx.Ctx, tc.request) | ||
|
||
Convey("then it should return the expected result", func() { | ||
if tc.expectErr { | ||
So(err, ShouldNotBeNil) | ||
So(res, ShouldBeNil) | ||
} else { | ||
So(err, ShouldBeNil) | ||
So(res, ShouldNotBeNil) | ||
} | ||
}) | ||
}) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.