From 9399780b602940dfd021444bcd48f6d09668d8e5 Mon Sep 17 00:00:00 2001 From: Olimdzhon Date: Wed, 31 Jul 2024 10:30:09 +0500 Subject: [PATCH] added tests; --- x/checkers/keeper/msg_server_create_game.go | 34 ++++++++++++++-- .../keeper/msg_server_create_game_test.go | 40 +++++++++++++++++-- x/checkers/types/message_create_game.go | 10 ++++- x/checkers/types/message_create_game_test.go | 36 ++++++++++++++--- 4 files changed, 107 insertions(+), 13 deletions(-) diff --git a/x/checkers/keeper/msg_server_create_game.go b/x/checkers/keeper/msg_server_create_game.go index 4730b4e..2cd2fca 100644 --- a/x/checkers/keeper/msg_server_create_game.go +++ b/x/checkers/keeper/msg_server_create_game.go @@ -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 } diff --git a/x/checkers/keeper/msg_server_create_game_test.go b/x/checkers/keeper/msg_server_create_game_test.go index 9326fa8..30a004c 100644 --- a/x/checkers/keeper/msg_server_create_game_test.go +++ b/x/checkers/keeper/msg_server_create_game_test.go @@ -1,14 +1,25 @@ 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, @@ -16,6 +27,29 @@ func TestCreateGame(t *testing.T) { }) 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) -} \ No newline at end of file +} + +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) +} diff --git a/x/checkers/types/message_create_game.go b/x/checkers/types/message_create_game.go index aacbf6b..4f3f865 100644 --- a/x/checkers/types/message_create_game.go +++ b/x/checkers/types/message_create_game.go @@ -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 } @@ -30,4 +38,4 @@ func (msg *MsgCreateGame) GetSigners() []sdk.AccAddress { panic(err) } return []sdk.AccAddress{creator} -} +} \ No newline at end of file diff --git a/x/checkers/types/message_create_game_test.go b/x/checkers/types/message_create_game_test.go index ef6d09e..4b766ce 100644 --- a/x/checkers/types/message_create_game_test.go +++ b/x/checkers/types/message_create_game_test.go @@ -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(), }, }, }