def test_has_won_return_true_on_2nd_check(self, mock_vert, mock_horiz): """Assert if one of the 'check' functions returns True, player wins""" game = Game("1") game.game = {"board": []} self.assertTrue(game.has_won('x', (0, 1))) mock_vert.assert_called_once_with([], 'x', 0, 1) self.assertFalse(mock_horiz.called)
def update_game(game_id): """Update the specified game (make a move or end a game).""" game = Game(game_id) game.load_game() if request.json.get("game_status") == Game.DISCONNECTED: game.game_over(won=False) return {"message": "OK"}, status.HTTP_200_OK, RESPONSE_HEADERS column = request.json["column"] name = request.json["name"] move_result = game.move(name, column) if move_result is None: message = "Bad request, column full." status_code = status.HTTP_400_BAD_REQUEST elif move_result is True: message = Game.WON status_code = status.HTTP_200_OK else: message = "OK" status_code = status.HTTP_200_OK response_data = {"message": message} response_data.update(game.game) response_body = json.dumps(response_data, cls=DecimalEncoder) return response_body, status_code, RESPONSE_HEADERS
def test_toggle_turn_player_1_moved_no_player_2(self, mock_db): """2nd player hasn't joined yet, turn = None.""" game = Game("3") game.game = { "players": ["galahad"], "max_players": 2, } game.toggle_turn("galahad") mock_db.save_game.assert_called_once_with( '3', {'players': ['galahad'], 'turn': None, 'max_players': 2})
def test_toggle_turn_player_2_moved(self, mock_db): game = Game("2") game.game = { "players": ["dingo", "zoot"], "max_players": 2, } game.toggle_turn("zoot") mock_db.save_game.assert_called_once_with( '2', {'players': ['dingo', 'zoot'], 'turn': 'dingo', 'max_players': 2})
def test_game_over_disconnected(self, mock_db): game = Game("1") game.game = {} game.game_over(won=False) mock_db.save_game.assert_called_once_with( "1", {"game_status": "disconnected"})
def test_game_over_won(self, mock_db): game = Game("1") game.game = {} game.game_over() mock_db.save_game.assert_called_once_with("1", {"game_status": "won"})
def test_toggle_turn_player_1_moved(self, mock_db): game = Game("1") game.game = {"players": ["arthur", "billy"]} game.toggle_turn("arthur") mock_db.save_game.assert_called_once_with( '1', {'players': ['arthur', 'billy'], 'turn': 'billy'})
def test_get_player_disc_colour_player_2_is_ys_2_players(self): """Player 2 is Ys""" game = Game("1") game.game = {"players": ["robin", "brian"]} disc = game.get_player_disc_colour("brian") self.assertEqual("o", disc)
def test_get_player_disc_colour_player_1_is_xs_2_players(self): """Player 1 is Xs""" game = Game("1") game.game = {"players": ["robin", "brian"]} disc = game.get_player_disc_colour("robin") self.assertEqual("x", disc)
def test_get_player_disc_colour_player_1_is_xs_no_player_2(self): """Test with one player, 2nd player hasn't joined yet""" game = Game("1") game.game = {"players": ["john"]} disc = game.get_player_disc_colour("john") self.assertEqual("x", disc)
def test_has_won_all_check_methods_return_false(self, mock_check_methods): """Assert if none of the 'check' functions return True game still on""" game = Game("2") game.game = {"board": []} self.assertFalse(game.has_won('x', (0, 1)))
def get_game(game_id): """Get the specified game.""" game = Game(game_id) game.load_game() response_body = json.dumps(game.game, cls=DecimalEncoder) return response_body, status.HTTP_200_OK, RESPONSE_HEADERS