Skip to content

Commit

Permalink
added tests;
Browse files Browse the repository at this point in the history
  • Loading branch information
olimdzhon committed Jul 31, 2024
1 parent 7bc9ea7 commit 9399780
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 13 deletions.
34 changes: 31 additions & 3 deletions x/checkers/keeper/msg_server_create_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,44 @@ package keeper

import (
"context"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/olimdzhon/checkers/x/checkers/rules"
"github.com/olimdzhon/checkers/x/checkers/types"
)

func (k msgServer) CreateGame(goCtx context.Context, msg *types.MsgCreateGame) (*types.MsgCreateGameResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx
systemInfo, found := k.Keeper.GetSystemInfo(ctx)
if !found {
panic("SystemInfo not found")
}
newIndex := strconv.FormatUint(systemInfo.NextId, 10)

return &types.MsgCreateGameResponse{}, nil
newGame := rules.New()
storedGame := types.StoredGame{
Index: newIndex,
Board: newGame.String(),
Turn: rules.PieceStrings[newGame.Turn],
Black: msg.Black,
Red: msg.Red,
}

err := storedGame.Validate()
if err != nil {
return nil, err
}

k.Keeper.SetStoredGame(ctx, storedGame)
systemInfo.NextId++
k.Keeper.SetSystemInfo(ctx, systemInfo)




return &types.MsgCreateGameResponse{
GameIndex: newIndex,
}, nil
}
40 changes: 37 additions & 3 deletions x/checkers/keeper/msg_server_create_game_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
package keeper_test

import (
"context"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/olimdzhon/checkers/testutil/keeper"
"github.com/olimdzhon/checkers/x/checkers/keeper"
checkers "github.com/olimdzhon/checkers/x/checkers/module"
"github.com/olimdzhon/checkers/x/checkers/types"
"github.com/stretchr/testify/require"
)

func setupMsgServerCreateGame(t testing.TB) (types.MsgServer, keeper.Keeper, context.Context) {
k, ctx := keepertest.CheckersKeeper(t)
checkers.InitGenesis(ctx, k, *types.DefaultGenesis())
return keeper.NewMsgServerImpl(k), k , sdk.WrapSDKContext(ctx)
}

func TestCreateGame(t *testing.T) {
_, msgServer, context := setupMsgServer(t)
msgServer, _, context := setupMsgServerCreateGame(t)
createResponse, err := msgServer.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
})
require.Nil(t, err)
require.EqualValues(t, types.MsgCreateGameResponse{
GameIndex: "", // TODO: update with a proper value when updated
GameIndex: "1", // TODO: update with a proper value when updated
}, *createResponse)
}
}

func TestCreate1GameHasSaved(t *testing.T) {
msgSrvr, keeper, context := setupMsgServerCreateGame(t)
msgSrvr.CreateGame(context, &types.MsgCreateGame{
Creator: alice,
Black: bob,
Red: carol,
})
systemInfo, found := keeper.GetSystemInfo(sdk.UnwrapSDKContext(context))
require.True(t, found)
require.EqualValues(t, types.SystemInfo{
NextId: 2,
}, systemInfo)
game1, found1 := keeper.GetStoredGame(sdk.UnwrapSDKContext(context), "1")
require.True(t, found1)
require.EqualValues(t, types.StoredGame{
Index: "1",
Board: "*b*b*b*b|b*b*b*b*|*b*b*b*b|********|********|r*r*r*r*|*r*r*r*r|r*r*r*r*",
Turn: "b",
Black: bob,
Red: carol,
}, game1)
}
10 changes: 9 additions & 1 deletion x/checkers/types/message_create_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ func (msg *MsgCreateGame) ValidateBasic() error {
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
_, err = sdk.AccAddressFromBech32(msg.Black)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid black address (%s)", err)
}
_, err = sdk.AccAddressFromBech32(msg.Red)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid red address (%s)", err)
}
return nil
}

Expand All @@ -30,4 +38,4 @@ func (msg *MsgCreateGame) GetSigners() []sdk.AccAddress {
panic(err)
}
return []sdk.AccAddress{creator}
}
}
36 changes: 30 additions & 6 deletions x/checkers/types/message_create_game_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
package types
package types_test

import (
"testing"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/olimdzhon/checkers/testutil/sample"
"github.com/olimdzhon/checkers/x/checkers/types"
"github.com/stretchr/testify/require"
)

func TestMsgCreateGame_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgCreateGame
msg types.MsgCreateGame
err error
}{
{
name: "invalid address",
msg: MsgCreateGame{
name: "invalid creator address",
msg: types.MsgCreateGame{
Creator: "invalid_address",
Black: sample.AccAddress(),
Red: sample.AccAddress(),
},
err: sdkerrors.ErrInvalidAddress,
}, {
},
{
name: "invalid black address",
msg: types.MsgCreateGame{
Creator: sample.AccAddress(),
Black: "invalid_address",
Red: sample.AccAddress(),
},
err: sdkerrors.ErrInvalidAddress,
},
{
name: "invalid red address",
msg: types.MsgCreateGame{
Creator: sample.AccAddress(),
Black: sample.AccAddress(),
Red: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
},
{
name: "valid address",
msg: MsgCreateGame{
msg: types.MsgCreateGame{
Creator: sample.AccAddress(),
Black: sample.AccAddress(),
Red: sample.AccAddress(),
},
},
}
Expand Down

0 comments on commit 9399780

Please sign in to comment.