Creating Some Tests: Things to Check

Get to know what we should check to maintain consistency.

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:

  1. User.num_games is either the same or one larger after each interaction.
  2. User.by_lang counts and User.outcomes counts each sum up to User.num_games.
  3. User.avg_time is always User.total_time divided by User.num_games.
  4. Game.player and Game.usage_id never change after the game is created.
  5. At each step, User.guessed is either the same as before or a string of length one more with a new letter appended.
  6. The letters in Game.reveal_word are only letters from the Game.guessed string.
  7. Game.bad_guesses is always a count of the letters in Game.guessed that does not appear in Game.reveal_word.
  8. 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.