# Query - Help Find a Correct Move
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. A GUI is the first place where a bad move can be caught, but it is still possible that a GUI wrongly enforces the rules.
Since sending transactions includes costs, how do you assist participants in making sure they at least do not make a wrong 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.
# Some initial thoughts
When it comes to finding a correct move, ask:
- What structure will facilitate this check?
- Who do you let make such checks?
- What acceptable limitations do you have for this?
- Are there new errors to report back?
- What event should you emit?
# Code needs
- What Ignite CLI commands, if any, will assist you?
- How do you adjust what Ignite CLI created for you?
- Where do you make your changes?
- How would you unit-test these new elements?
- How would you use Ignite CLI to locally run a one-node blockchain and interact with it via the CLI to see what you get?
# New information
To run a query to check the validity of a move you need to pass:
- The game ID: call the field
player, as queries do not have a signer.
- The origin board position:
- The target board position:
The information to be returned is:
- A boolean for whether the move is valid, called
- A text which explains why the move is not valid, called
As with other data structures, you can create the query message object with Ignite CLI:
Among other files, you should now have this:
Ignite CLI has created the following boilerplate for you:
- The Protobuf gRPC interface function (opens new window) to submit your new
QueryCanPlayMoveRequestand its default implementation.
- The routing of this new query (opens new window) in the query facilities.
- An empty function (opens new window) ready to implement the action.
# 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.
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:
Has the game already been won?
playergiven actually one of the game players?
Is it the player's turn?
Attempt the move and report back:
If all went well:
# 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:
structthat describes a test:
Create the common OK response, so as to reuse it later:
Create the first test case, which you will reuse:
Create the list of test cases you want to run, including the just-defined
Fortunately you already have a test file with all the steps (opens new window) to a complete game.
With this preparation, add the single test function that runs all the cases:
Finally, add the error tests that cannot be covered with the previous test cases:
Note that this reuses
# 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:
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.
When there is no such game:
Trying this on a game that does not exist returns:
Confirm this was an error from the point of view of the executable:
There is room to improve the error message, but it is important that you got an error, as expected.
When you ask for a bad color:
If the player tries to play the wrong color on a game that exists, it returns:
This is a proper message response, and a reason elaborating on the message.
When you ask for a player out of turn:
If the opponent tries to play out of turn, it returns:
When you ask for a piece that is not that of the player:
If black tries to play a red piece, it returns:
When it is correct:
If black tests a correct move, it returns:
When the player must capture:
If black fails to capture a mandatory red piece, it returns:
The reason given is understandable, but it does not clarify why the move is invalid. There is room to improve this message.
After the game has been forfeited:
If black tries to capture a red piece on a running game, it returns:
Wait five minutes for the forfeit:
Now it returns:
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.