Exemplo n.º 1
0
    def test_move_success(self):
        tests = [
            {
                'name': 'pin2_none',
                'pin1': 0,
                'pin2': None,
            },
            {
                'name': 'pin2_set',
                'pin1': 0,
                'pin2': 1,
            },
        ]
        game = Game(self.p1, self.p2)

        for test in tests:
            with self.subTest(test['name']):
                with mock.patch.object(Row, 'knockdown') as knockdown, \
                    mock.patch.object(Game, 'update_turn') as update_turn:
                    game.move(self.p1, test['pin1'], test['pin2'])

                    self.assertEqual(knockdown.call_count, 1)
                    knockdown.assert_called_with(test['pin1'], test['pin2'])

                    self.assertEqual(update_turn.call_count, 1)
                    update_turn.assert_called_with()
Exemplo n.º 2
0
    def test_update_turn_expired(self):
        game = Game(self.p1, self.p2)

        with mock.patch.object(Game, 'is_ended', return_value=True):
            # see that turn does not rotate players
            self.assertEqual(game.turn, self.p1)
            game.update_turn()
            self.assertEqual(game.turn, self.p1)
Exemplo n.º 3
0
    def test_move_game_ended(self):
        models.game = Game(self.player1, self.player2)
        models.game.row.pins = [False]

        res = self.client.post('/move/{}/0'.format(self.player1))
        self.assertEqual(res.status_code, 400)
        res_data = json.loads(res.data.decode())
        self.assertIn('message', res_data)
Exemplo n.º 4
0
def new_game():
    args = request.get_json(force=True) or {}
    player1 = args.get('player1')
    player2 = args.get('player2')

    validate_players(player1, player2)
    models.game = Game(player1, player2)

    data = {
        'message': 'new game started',
    }
    logger.info('started new game player1:%s player2:%s', models.game.player1,
                models.game.player2)
    return make_response(jsonify(data), 201)
Exemplo n.º 5
0
    def test_get_move_message(self):
        tests = [
            {
                'name': 'live-game',
                'winner': self.player1,
                'is_ended': False,
                'tournament': False,
                'expected': '!!!!!!!!!!',
            },
            {
                'name': 'game-ended-no-tourney',
                'winner': self.player1,
                'is_ended': True,
                'tournament': False,
                'expected': self.player1 + ' is the winner!',
            },
            {
                'name': 'game-ended-no-tourney',
                'winner': self.player1,
                'is_ended': True,
                'tournament': True,
                'expected': self.player1 + ' is the winner!',
            },
        ]

        for test in tests:
            with self.subTest(test['name']):
                game = Game(self.player1, self.player2)

                if test['tournament']:
                    tournament = Tournament(
                        players=[self.player1, self.player2, self.player3])
                else:
                    tournament = None

                with mock.patch.object(Game, 'is_ended', return_value=test['is_ended']), \
                        mock.patch.object(Game, 'winner') as winner, \
                        mock.patch.object(Tournament, 'remove_player') as remove_player:

                    winner.__get__ = mock.Mock(return_value=self.player1)
                    message = routes.get_move_message(game, tournament)
                    self.assertEqual(message, test['expected'])

                    if test['tournament']:
                        self.assertEqual(remove_player.call_count, 1)
                    else:
                        self.assertEqual(remove_player.call_count, 0)
Exemplo n.º 6
0
    def test_update_turn_not_expired(self):
        game = Game(self.p1, self.p2)

        with mock.patch.object(Game, 'is_ended', return_value=False):
            # see that turn goes to the other player
            self.assertEqual(game.turn, self.p1)
            game.update_turn()
            self.assertEqual(game.turn, self.p2)

            # turn goes back to original player
            game.update_turn()
            self.assertEqual(game.turn, self.p1)
Exemplo n.º 7
0
    def test_move(self):
        tests = [
            {
                'name': 'pin1',
                'pin1': 0,
                'pin2': None,
            },
            {
                'name': 'pin1-pin2',
                'pin1': 0,
                'pin2': 1,
            },
        ]

        for test in tests:
            with self.subTest(test['name']):
                models.game = Game(self.player1, self.player2)
                models.tournament = Tournament(
                    players=[self.player1, self.player2, self.player3])

                with mock.patch('kayles.routes.models.game.move') as move, \
                        mock.patch('kayles.routes.get_move_message', return_value='hello'):

                    if test['pin2']:
                        res = self.client.post('/move/{}/{},{}'.format(
                            self.player1, test['pin1'], test['pin2']))
                    else:
                        res = self.client.post('/move/{}/{}'.format(
                            self.player1, test['pin1']))

                    self.assertEqual(move.call_count, 1)
                    move.assert_called_with(self.player1, test['pin1'],
                                            test['pin2'])

                    self.assertEqual(res.status_code, 201)
                    res_data = json.loads(res.data.decode())
                    self.assertEqual(res_data['message'], 'hello')
Exemplo n.º 8
0
    def test_winner_set(self):
        game = Game(self.p1, self.p2)

        with mock.patch.object(Game, 'is_ended', return_value=True):
            self.assertEqual(game.winner, game.turn)
Exemplo n.º 9
0
    def test_winner_none(self):
        game = Game(self.p1, self.p2)

        with mock.patch.object(Game, 'is_ended', return_value=False):
            self.assertIsNone(game.winner)
Exemplo n.º 10
0
    def test_is_ended_false(self):
        game = Game(self.p1, self.p2)

        with mock.patch.object(Row, 'get_pins_left', return_value=1):
            self.assertFalse(game.is_ended())
Exemplo n.º 11
0
    def test_move_fail(self):
        game = Game(self.p1, self.p2)

        with self.assertRaises(exceptions.InvalidTurnException) as context:
            game.move(self.p2, 0)
Exemplo n.º 12
0
 def test_init(self):
     game = Game(self.p1, self.p2)
     self.assertEqual(game.player1, self.p1)
     self.assertEqual(game.player2, self.p2)
     self.assertEqual(len(game.row.pins), game.PINS)
Exemplo n.º 13
0
 def test_str(self):
     game = Game(self.p1, self.p2)
     game.row = [True, False, True]
     self.assertEqual(game.__str__(), game.row.__str__())
Exemplo n.º 14
0
    def test_loser_p1(self):
        game = Game(self.p1, self.p2)
        game.turn = self.p2

        with mock.patch.object(Game, 'is_ended', return_value=True):
            self.assertEqual(game.loser, self.p1)