Skip to content

Commit

Permalink
Merge pull request #335 from okp4/test/logic-grpc-test
Browse files Browse the repository at this point in the history
🧪 Add more tests on logic module
  • Loading branch information
bdeneux authored Apr 26, 2023
2 parents f0fd4e1 + 3ff413b commit 9380f31
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 18 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ mock: ## Generate all the mocks (for tests)
@mockgen -source=x/vesting/types/expected_keepers.go -package testutil -destination x/vesting/testutil/expected_keepers_mocks.go
@mockgen -source=x/logic/types/expected_keepers.go -package testutil -destination x/logic/testutil/expected_keepers_mocks.go
@mockgen -destination x/logic/testutil/gas_mocks.go -package testutil github.com/cosmos/cosmos-sdk/store/types GasMeter
@mockgen -destination x/logic/testutil/fs_mocks.go -package testutil -source=x/logic/fs/fs.go

## Release:
.PHONY: release-assets
Expand Down
101 changes: 101 additions & 0 deletions x/logic/keeper/grpc_query_ask_test.go
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)
})
})
})
})
}
})
}
89 changes: 87 additions & 2 deletions x/logic/keeper/grpc_query_params_test.go
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)
})
})
})
})
}
})
}
89 changes: 89 additions & 0 deletions x/logic/keeper/msg_server_test.go
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)
}
})
})
})
}
})
}
73 changes: 63 additions & 10 deletions x/logic/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewParams(interpreter Interpreter, limits Limits) Params {

// DefaultParams returns a default set of parameters.
func DefaultParams() Params {
return NewParams(NewInterpreter(), DefaultLimits())
return NewParams(NewInterpreter(), NewLimits())
}

// Validate validates the set of params.
Expand Down Expand Up @@ -89,6 +89,20 @@ func WithPredicatesBlacklist(blacklist []string) InterpreterOption {
}
}

// WithVirtualFilesWhitelist sets the whitelist of predicates.
func WithVirtualFilesWhitelist(whitelist []string) InterpreterOption {
return func(i *Interpreter) {
i.VirtualFilesFilter.Whitelist = whitelist
}
}

// WithVirtualFilesBlacklist sets the blacklist of predicates.
func WithVirtualFilesBlacklist(blacklist []string) InterpreterOption {
return func(i *Interpreter) {
i.VirtualFilesFilter.Blacklist = blacklist
}
}

// WithBootstrap sets the bootstrap program.
func WithBootstrap(bootstrap string) InterpreterOption {
return func(i *Interpreter) {
Expand Down Expand Up @@ -116,18 +130,57 @@ func validateInterpreter(i interface{}) error {
return nil
}

// NewLimits creates a new Limits object.
func NewLimits(maxGas, maxSize, maxResultCount *math.Uint) Limits {
return Limits{
MaxGas: maxGas,
MaxSize: maxSize,
MaxResultCount: maxResultCount,
// LimitsOption is a functional option for configuring the Limits.
type LimitsOption func(*Limits)

// WithMaxGas sets the max gas limits for interpreter.
func WithMaxGas(maxGas math.Uint) LimitsOption {
return func(i *Limits) {
i.MaxGas = &maxGas
}
}

// DefaultLimits return a Limits object with default params.
func DefaultLimits() Limits {
return NewLimits(&DefaultMaxGas, &DefaultMaxSize, &DefaultMaxResultCount)
// WithMaxSize sets the max size limits accepted for a prolog program.
func WithMaxSize(maxSize math.Uint) LimitsOption {
return func(i *Limits) {
i.MaxSize = &maxSize
}
}

// WithMaxResultCount sets the maximum number of results that can be requested for a query.
func WithMaxResultCount(maxResultCount math.Uint) LimitsOption {
return func(i *Limits) {
i.MaxResultCount = &maxResultCount
}
}

// WithMaxUserOutputSize specifies the maximum number of bytes to keep in the user output.
func WithMaxUserOutputSize(maxUserOutputSize math.Uint) LimitsOption {
return func(i *Limits) {
i.MaxUserOutputSize = &maxUserOutputSize
}
}

// NewLimits creates a new Limits object.
func NewLimits(opts ...LimitsOption) Limits {
l := Limits{}
for _, opt := range opts {
opt(&l)
}

if l.MaxGas == nil {
l.MaxGas = &DefaultMaxGas
}

if l.MaxSize == nil {
l.MaxSize = &DefaultMaxSize
}

if l.MaxResultCount == nil {
l.MaxResultCount = &DefaultMaxResultCount
}

return l
}

func validateLimits(i interface{}) error {
Expand Down
Loading

0 comments on commit 9380f31

Please sign in to comment.