def test_score_save(self): # create game g = Game.start_new() game_code = g.code g.word = 'EASY' self.db.session.commit() # play g.check_guess('E') g.check_guess('A') g.check_guess('S') g.check_guess('Y') assert g.status == 'won' # save score player = "T'Challa" response = self.client.post(url_for("leaderboard.save"), data=json.dumps( dict(player=player, game=g.code)), content_type='application/json') assert response.status_code == 200 s = Score.query.order_by(desc(Score.id)).first() assert s.player == player assert s.word == g.word assert s.score == g.calculate_score()
def test_game_check(self): # create game => g = Game.start_new() game_code = g.code word = g.word # check a letter that doesn't belongs to the game word L1 = 'X' # we know for sure none of the words contain X response = self.client.post(url_for("game.check", code=game_code), data=json.dumps(dict(guess=L1)), content_type='application/json') assert response.status_code == 200 result = json.loads(response.get_data(as_text=True)) assert result['found'] == False assert result['positions'] == [] assert result['status'] == 'playing' # check a char that belongs to the game word L2 = word[0] # on the setup all first chars are unique to the word response = self.client.post(url_for("game.check", code=game_code), data=json.dumps(dict(guess=L2)), content_type='application/json') assert response.status_code == 200 result = json.loads(response.get_data(as_text=True)) assert result['found'] == True assert result['positions'] == [0] assert result['status'] == 'playing' # check a char that belongs to the game word more than once L3 = word[0] # on the setup all first chars are unique to the word response = self.client.post(url_for("game.check", code=game_code), data=json.dumps(dict(guess=L3)), content_type='application/json') assert response.status_code == 400 # check a character that is not valid L4 = '?' response = self.client.post(url_for("game.check", code=game_code), data=json.dumps(dict(guess=L4)), content_type='application/json') assert response.status_code == 400
def test_game_load(self): # create game => g = Game.start_new() game_code = g.code word = g.word # load a game that does not exist response = self.client.get(url_for("game.load", code='X')) assert response.status_code == 404 # verify the info on the game you created response = self.client.get(url_for("game.load", code=game_code)) assert response.status_code == 200 result = json.loads(response.get_data(as_text=True)) assert result['status'] == 'playing' assert result['attempts'] == [] assert result['wordLength'] == len(word) assert result['foundChars'] == {} assert result['mistakeCount'] == 0 # after a guess L1 = word[0] response = self.client.post(url_for("game.check", code=game_code), data=json.dumps(dict(guess=L1)), content_type='application/json') assert response.status_code == 200 response = self.client.get(url_for("game.load", code=game_code)) assert response.status_code == 200 result = json.loads(response.get_data(as_text=True)) assert result['status'] == 'playing' assert result['attempts'] == [L1] assert result['wordLength'] == len(word) assert L1 in result['foundChars'] assert result['foundChars'][L1] == [0] assert result['mistakeCount'] == 0