Creating Some Tests: Things to Check
Get to know what we should check to maintain consistency.
We'll cover the following
Checking consistency
Finding consistency checks that must hold helps to methodically step through the fields within the database. This is best accomplished by looking at the ORM. Of course, some consistency issues have already been addressed by the ORM properties themselves, such as the uniqueness of primary keys. Some things we might check:
User.num_games
is either the same or one larger after each interaction.User.by_lang
counts andUser.outcomes
counts each sum up toUser.num_games
.User.avg_time
is alwaysUser.total_time
divided byUser.num_games
.Game.player
andGame.usage_id
never change after the game is created.- At each step,
User.guessed
is either the same as before or a string of length one more with a new letter appended. - The letters in
Game.reveal_word
are only letters from theGame.guessed
string. Game.bad_guesses
is always a count of the letters inGame.guessed
that does not appear inGame.reveal_word
.- Only active games can be updated.
Checking game state
Instead of attempting to check each issue, we’ll focus on the game state, where it seems there are more things that could go wrong. To do this, we’ll add another method to the Games ORM
class, and then be sure to use it when updating the game state.
To do this, we update the _to_dict
method and create a new _check_consistent
method within the Game
class.
In api.py
we need to change two lines, which have been marked as updated in the following snippet.
Get hands-on with 1200+ tech skills courses.