def test_get_game(self):
        game = GameFactory()
        game.active_player = PlayerFactory(id='U1')
        game.active_player.played_card = Card.query.first()
        self.api_client.game = game

        rv = self.auth_get('/game')
        self.assert_200(rv)
        rv_data = json.loads(rv.data.decode('utf-8'))

        self.assertIn('id', rv_data)
        self.assertEqual(0, rv_data['turn'])
        self.assertEqual('U1', rv_data['active_player'])
        self.assertEqual('black card 0', rv_data['active_card'])
    def test_leave_game(self):
        game = GameFactory()
        self.api_client.game = game
        player = PlayerFactory(id='U1', played_card_id=11)
        game.players.append(player)
        game.active_player = player
        game.players.append(PlayerFactory(id='U2'))
        db.session.commit()

        rv = self.auth_delete('/game/players/U1')
        self.assert_200(rv)

        db.session.refresh(game)
        self.assertEqual(1, game.players.count())
        self.assertEqual('U2', game.active_player.id)
        self.assertEqual(11, game.active_player.played_card_id)
    def test_leave_game(self):
        game = GameFactory()
        self.api_client.game = game
        player = PlayerFactory(id='U1', played_card_id=11)
        game.players.append(player)
        game.active_player = player
        game.players.append(PlayerFactory(id='U2'))
        db.session.commit()

        rv = self.auth_delete('/game/players/U1')
        self.assert_200(rv)

        db.session.refresh(game)
        self.assertEqual(1, game.players.count())
        self.assertEqual('U2', game.active_player.id)
        self.assertEqual(11, game.active_player.played_card_id)
    def test_delete_game(self):
        game = GameFactory()
        self.api_client.game = game

        rv = self.auth_delete('/game')
        self.assert_200(rv)

        self.assertIsNone(ApiClient.query.first().game)
    def test_first_player_join(self):
        game = GameFactory(deck_seed=0)
        self.api_client.game = game

        rv = self.auth_post('/game/players', data={'id': 'U1'})
        self.assert_200(rv)
        rv_data = json.loads(rv.data.decode('utf-8'))

        self.assertEqual('U1', game.active_player.id)
        self.assertEqual(6, game.active_player.played_card.id)
    def test_get_player(self):
        game = GameFactory(deck_seed=0)
        self.api_client.game = game
        game.players.append(PlayerFactory(id='UA1'))

        rv = self.auth_get('/game/players/UA1')
        self.assert_200(rv)
        rv_data = json.loads(rv.data.decode('utf-8'))

        self.assertEqual('UA1', rv_data['id'])
    def test_join_game_twice(self):
        game = GameFactory()
        self.api_client.game = game

        rv = self.auth_post('/game/players', data={'id': 'UA123'})
        self.assert_200(rv)
        rv = self.auth_post('/game/players', data={'id': 'UA123'})
        self.assert_status(rv, 422)

        db.session.refresh(game)
        self.assertEqual(1, game.players.count())
    def test_pick_cards_on_create(self):
        game = GameFactory(deck_seed=0)
        self.api_client.game = game
        db.session.commit()

        rv = self.auth_post('/game/players', data={'id': 'UA1'})
        self.assert_200(rv)

        db.session.refresh(game)
        self.assertEqual([13, 30, 34, 37, 43, 45, 48, 51, 56, 59],
                         [c.id for c in Player.query.get('UA1').cards])
        self.assertTrue(game.white_cards_picked >= 10)
    def test_join_game(self):
        game = GameFactory()
        self.api_client.game = game

        rv = self.auth_post('/game/players', data={'id': 'UA123'})
        self.assert_200(rv)
        rv_data = json.loads(rv.data.decode('utf-8'))

        self.assertEqual('UA123', rv_data['id'])

        db.session.refresh(game)
        self.assertEqual('UA123', game.players[0].id)
    def test_game_ready_players_played(self):
        game = GameFactory()
        game.players.append(PlayerFactory(id='U1'))
        game.players.append(PlayerFactory(id='U2'))
        game.players.append(PlayerFactory(id='U3'))
        game.players.append(PlayerFactory(id='U4'))
        game.start_turn()

        game.players[1].played_card = Card.query.get(11)
        with self.assertRaises(errors.NotEnoughPlayers):
            game.check_turn_ready()

        game.players[2].played_card = Card.query.get(12)
        with self.assertRaises(errors.TooEarly):
            game.check_turn_ready()

        game.players[3].played_card = Card.query.get(13)
        self.assertTrue(game.check_turn_ready())
    def test_play_card(self):
        game = GameFactory(deck_seed=0)
        self.api_client.game = game

        rv = self.auth_post('/game/players', data={'id': 'U1'})
        self.assert_200(rv)
        rv = self.auth_post('/game/players', data={'id': 'U2'})
        self.assert_200(rv)

        rv = self.auth_put('/game/players/U2', data={'played_card': 3})
        self.assert_200(rv)
        rv_data = json.loads(rv.data.decode('utf-8'))

        self.assertEqual(3, rv_data['played_card']['id'])
        self.assertEqual('white card 17', rv_data['played_card']['text'])
    def test_pick_cards(self):
        game = GameFactory(deck_seed=0)
        db.session.commit()

        self.assertEqual([13, 34, 30, 37],
                         [c.id for c in game.pick_white_cards(4)])
        self.assertEqual([45], [c.id for c in game.pick_white_cards(1)])
        self.assertEqual([43], [c.id for c in game.pick_white_cards(1)])
        self.assertEqual(44, len(game.pick_white_cards(50)))
        self.assertEqual([], game.pick_white_cards(1))
    def test_game_ready_time_delay(self):
        game = GameFactory()
        game.players.append(PlayerFactory(id='U1'))
        game.players.append(PlayerFactory(id='U2'))
        game.players.append(PlayerFactory(id='U3'))
        game.players.append(PlayerFactory(id='U4'))

        with freeze_time("2000-01-01 12:00:00"):
            game.start_turn()
            game.players[1].played_card = Card.query.get(11)
            game.players[2].played_card = Card.query.get(12)
            with self.assertRaises(errors.TooEarly):
                game.check_turn_ready()
        with freeze_time("2000-01-01 12:01:01"):
            self.assertTrue(game.check_turn_ready())
    def test_no_player_id(self):
        game = GameFactory(deck_seed=0)
        self.api_client.game = game

        rv = self.auth_post('/game/players')
        self.assert_status(rv, 422)
    def test_create_game_already_exist(self):
        game = GameFactory()
        self.api_client.game = game

        rv = self.auth_post('/game')
        self.assertStatus(rv, 422)