# Query - Help Find a Correct Move

Make sure you have everything you need before proceeding:

synopsis

In this section, you will:

  • Improve usability with queries.
  • Create a battery of integration tests.

A player sends a MsgPlayMove when making a move. This message can succeed or fail for several reasons. One error situation is when the message represents an invalid move.

Players should be able to confirm that a move is valid before burning gas. To add this functionality, you need to create a way for the player to call the Move (opens new window) function without changing the game's state. Use a query because they are evaluated in memory and do not commit anything permanently to storage.

# New information

To run a query to check the validity of a move you need to pass:

  • The game ID: call the field IdValue.
  • The player, as queries do not have a signer.
  • The origin board position: fromX and fromY.
  • The target board position: toX and toY.

The information to be returned is:

  • A boolean for whether the move is valid, called Possible.
  • A text which explains why the move is not valid, called Reason.

As with other data structures, you can create the query message object with Ignite CLI:

Copy $ ignite scaffold query canPlayMove idValue player fromX:uint fromY:uint toX:uint toY:uint --module checkers --response possible:bool,reason

Among other files, you should now have this:

Copy message QueryCanPlayMoveRequest { string idValue = 1; string player = 2; uint64 fromX = 3; uint64 fromY = 4; uint64 toX = 5; uint64 toY = 6; } message QueryCanPlayMoveResponse { bool possible = 1; string reason = 2; } proto checkers query.proto View source

Ignite CLI has created the following boilerplate for you:

# Query handling

Now you need to implement the answer to the player's query in grpc_query_can_play_move.go. Differentiate between two types of errors:

  • Errors relating to the move, returning a reason.
  • Errors indicating a move test is impossible, returning an error.
  1. The game needs to be fetched. If it does not exist at all, you can return an error message because you did not test the move:

    Copy storedGame, found := k.GetStoredGame(ctx, req.IdValue) if !found { return nil, sdkerrors.Wrapf(types.ErrGameNotFound, types.ErrGameNotFound.Error(), req.IdValue) } x checkers keeper grpc_query_can_play_move.go View source
  2. Has the game already been won?

    Copy if storedGame.Winner != rules.PieceStrings[rules.NO_PLAYER] { return &types.QueryCanPlayMoveResponse{ Possible: false, Reason: types.ErrGameFinished.Error(), }, nil } x checkers keeper grpc_query_can_play_move.go View source
  3. Is the player given actually one of the game players?

    Copy var player rules.Player if strings.Compare(rules.PieceStrings[rules.RED_PLAYER], req.Player) == 0 { player = rules.RED_PLAYER } else if strings.Compare(rules.PieceStrings[rules.BLACK_PLAYER], req.Player) == 0 { player = rules.BLACK_PLAYER } else { return &types.QueryCanPlayMoveResponse{ Possible: false, Reason: fmt.Sprintf(types.ErrCreatorNotPlayer.Error(), req.Player), }, nil } x checkers keeper grpc_query_can_play_move.go View source
  4. Is it the player's turn?

    Copy game, err := storedGame.ParseGame() if err != nil { return nil, err } if !game.TurnIs(player) { return &types.QueryCanPlayMoveResponse{ Possible: false, Reason: fmt.Sprintf(types.ErrNotPlayerTurn.Error(), player.Color), }, nil } x checkers keeper grpc_query_can_play_move.go View source
  5. Attempt the move and report back:

    Copy _, moveErr := game.Move( rules.Pos{ X: int(req.FromX), Y: int(req.FromY), }, rules.Pos{ X: int(req.ToX), Y: int(req.ToY), }, ) if moveErr != nil { return &types.QueryCanPlayMoveResponse{ Possible: false, Reason: fmt.Sprintf(types.ErrWrongMove.Error(), moveErr.Error()), }, nil } x checkers keeper grpc_query_can_play_move.go View source
  6. If all went well:

    Copy return &types.QueryCanPlayMoveResponse{ Possible: true, Reason: "ok", }, nil x checkers keeper grpc_query_can_play_move.go View source

# Integration tests

A query is evaluated in memory, while using the current state in a read-only mode. Thanks to this, you can take some liberties with the current state before running a test, as long as reading the state works. For example, you can pretend the game has been progressed through a number of moves even though you have only pasted the board in that state. For this reason, you are going to test the new method with unit tests, even though you painstakingly prepared integration tests.

# Battery of unit tests

Take inspiration from the other ones (opens new window), which create a battery of tests to run in a loop. Running a battery of test cases makes it easier to insert new code and surface any unintended impact:

  1. Declare a struct that describes a test:

    Copy type canPlayBoard struct { desc string board string turn string request *types.QueryCanPlayMoveRequest response *types.QueryCanPlayMoveResponse err error } x checkers keeper grpc_query_can_play_move_test.go View source
  2. Create the common OK response, so as to reuse it later:

    Copy var (canPlayOkResponse = &types.QueryCanPlayMoveResponse{ Possible: true, Reason: "ok", }) x checkers keeper grpc_query_can_play_move_test.go View source
  3. Create the first test case, which you will reuse:

    Copy var(firstTestCase = canPlayBoard{ desc: "First move by black", 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", request: &types.QueryCanPlayMoveRequest{ IdValue: "1", Player: "b", FromX: 1, FromY: 2, ToX: 2, ToY: 3, }, response: canPlayOkResponse, }) x checkers keeper grpc_query_can_play_move_test.go View source
  4. Create the list of test cases you want to run, including the just-defined firstTestCase:

    Copy var (canPlayTestRange = []canPlayBoard{ firstTestCase, { desc: "First move by red, wrong", 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", request: &types.QueryCanPlayMoveRequest{ IdValue: "1", Player: "r", FromX: 1, FromY: 2, ToX: 2, ToY: 3, }, response: &types.QueryCanPlayMoveResponse{ Possible: false, Reason: "player tried to play out of turn: red", }, }, { desc: "Black can win", board: "*b*b****|**b*b***|*****b**|********|********|**r*****|*B***b**|********", turn: "b", request: &types.QueryCanPlayMoveRequest{ IdValue: "1", Player: "b", FromX: 1, FromY: 6, ToX: 3, ToY: 4, }, response: canPlayOkResponse, }, { desc: "Black must capture, see next for right move", 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", request: &types.QueryCanPlayMoveRequest{ IdValue: "1", Player: "b", FromX: 7, FromY: 2, ToX: 6, ToY: 3, }, response: &types.QueryCanPlayMoveResponse{ Possible: false, Reason: "wrong move%!(EXTRA string=Invalid move: {7 2} to {6 3})", }, }, { desc: "Black can capture, same board as previous", 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", request: &types.QueryCanPlayMoveRequest{ IdValue: "1", Player: "b", FromX: 2, FromY: 3, ToX: 0, ToY: 5, }, response: canPlayOkResponse, }, { desc: "Black king can capture backwards", board: "*b*b***b|**b*b***|***b***r|********|***r****|********|***r****|r*B*r*r*", turn: "b", request: &types.QueryCanPlayMoveRequest{ IdValue: "1", Player: "b", FromX: 2, FromY: 7, ToX: 4, ToY: 5, }, response: canPlayOkResponse, }, }) x checkers keeper grpc_query_can_play_move_test.go View source

    Fortunately you already have a test file with all the steps (opens new window) to a complete game.

  5. With this preparation, add the single test function that runs all the cases:

    Copy func TestCanPlayAsExpected(t *testing.T) { keeper, ctx := setupKeeper(t) goCtx := sdk.WrapSDKContext(ctx) for _, testCase := range canPlayTestRange { t.Run(testCase.desc, func(t *testing.T) { keeper.SetStoredGame(ctx, types.StoredGame{ Creator: alice, Index: testCase.request.IdValue, Game: testCase.board, Turn: testCase.turn, Red: bob, Black: carol, MoveCount: 1, BeforeId: "-1", AfterId: "-1", Deadline: "", Winner: "*", Wager: 0, }) response, err := keeper.CanPlayMove(goCtx, testCase.request) require.Nil(t, err) require.EqualValues(t, testCase.response, response) }) } } x checkers keeper grpc_query_can_play_move_test.go View source
  6. Finally, add the error tests that cannot be covered with the previous test cases:

    Copy func TestCanPlayWrongNoRequest(t *testing.T) { keeper, ctx := setupKeeper(t) goCtx := sdk.WrapSDKContext(ctx) _, err := keeper.CanPlayMove(goCtx, nil) require.ErrorIs(t, err, status.Error(codes.InvalidArgument, "invalid request")) } func TestCanPlayWrongNoGame(t *testing.T) { keeper, ctx := setupKeeper(t) goCtx := sdk.WrapSDKContext(ctx) keeper.SetStoredGame(ctx, types.StoredGame{ Creator: alice, Index: "9999999999", Game: firstTestCase.board, Turn: firstTestCase.turn, Red: bob, Black: carol, MoveCount: 1, BeforeId: "-1", AfterId: "-1", Deadline: "", Winner: "*", Wager: 0, }) _, err := keeper.CanPlayMove(goCtx, nil) require.Error(t, err, "game by id not found: 9999999999") } x checkers keeper grpc_query_can_play_move_test.go View source

    Note that this reuses firstTestCase.

# One integration test

Since you have set up the tests to work as integrated, why not create one integration test that makes use of them in the same file? Test the first case of the battery, which is the initial situation anyway:

Copy func (suite *IntegrationTestSuite) TestCanPlayAfterCreate() { suite.setupSuiteWithOneGameForPlayMove() goCtx := sdk.WrapSDKContext(suite.ctx) response, err := suite.queryClient.CanPlayMove(goCtx, canPlayTestRange[0].request) suite.Require().Nil(err) suite.Require().EqualValues(firstTestCase.response, response) } x checkers keeper grpc_query_can_play_move_test.go View source

With these, your function should be covered.

# Interact via the CLI

A friendly reminder that the CLI can always inform you about available commands:


You can test this query at any point in a game's life.


These queries are all satisfactory.

# Next up

Do you want to give players more flexibility about which tokens they can use for games? Let players wager any fungible token in the next section.