def test_leave_instance(): test_utils.clear_data_store() iid = test_utils.make_instance() other = '*****@*****.**' test_utils.add_player(iid, other) response = app.post('/leaveinstance', { 'gid': gid, 'iid': iid, 'pid': firstpid }).json assert response['e'] is False assert response['request_type'] == '/leaveinstance' assert response['leader'] == '' assert response['response']['joined'] == [] response = app.post('/leaveinstance', { 'gid': gid, 'iid': iid, 'pid': other }).json assert response['e'] is False assert response['request_type'] == '/leaveinstance' assert response['leader'] == '' assert response['iid'] == '' assert response['response']['joined'] == [] response = app.post('/joininstance', { 'gid': gid, 'iid': iid, 'pid': firstpid }).json assert response['e'] is True
def test_with_two_players(): iid = test_utils.make_instance() test_utils.add_player(iid, "*****@*****.**") game_id = new_game(iid) bob_game_id = new_game(iid, "*****@*****.**") assert get_game(iid, game_id) assert get_game(iid, bob_game_id)
def test_with_two_players(): iid = test_utils.make_instance() test_utils.add_player(iid, '*****@*****.**') game_id = new_game(iid) bob_game_id = new_game(iid, '*****@*****.**') assert get_game(iid, game_id) assert get_game(iid, bob_game_id)
def test_get_only_my_polls(): iid = test_utils.make_instance() poll_id = make_poll(iid) test_utils.add_player(iid, '*****@*****.**') make_poll(iid, pid = '*****@*****.**') contents = test_utils.post_server_command(iid, 'vot_get_my_polls', [])['contents'] assert len(contents) == 1 assert contents[0] == [poll_id, question]
def test_leader_fails_at_submitting(): iid = test_utils.make_instance() for player in init_players: test_utils.add_player(iid, player) current_round = 1 response = test_utils.post_server_command(iid, 'ata_new_game', []) test_utils.post_server_command(iid, 'ata_submit_card', [current_round, ''], pid = firstpid, error_expected = True)
def test_get_only_my_polls(): iid = test_utils.make_instance() poll_id = make_poll(iid) test_utils.add_player(iid, '*****@*****.**') make_poll(iid, pid='*****@*****.**') contents = test_utils.post_server_command(iid, 'vot_get_my_polls', [])['contents'] assert len(contents) == 1 assert contents[0] == [poll_id, question]
def test_not_your_game(): bob = '*****@*****.**' iid = test_utils.make_instance() test_utils.add_player(iid, bob) game_id = new_game(iid) response = test_utils.post_server_command(iid, 'bac_guess', [game_id, ['Blue'] * 4], pid=bob, error_expected=True)
def test_leader_fails_at_submitting(): iid = test_utils.make_instance() for player in init_players: test_utils.add_player(iid, player) current_round = 1 response = test_utils.post_server_command(iid, 'ata_new_game', []) test_utils.post_server_command(iid, 'ata_submit_card', [current_round, ''], pid=firstpid, error_expected=True)
def test_send_message_to_multiple_people(): test_iid = test_utils.make_instance() mtype = 'test' contents = ['quack', 'duck'] player2 = '*****@*****.**' test_utils.add_player(test_iid, player2) test_utils.send_new_message(test_iid, 'type1', [firstpid, player2], contents) test_utils.send_new_message(test_iid, 'type2', [firstpid, player2], contents) response = test_utils.get_messages(test_iid, '', '', 2) assert response[0]['type'] == 'type1' assert response[1]['type'] == 'type2' response = test_utils.get_messages(test_iid, '', '', 2, pid = player2) assert response[0]['type'] == 'type1' assert response[1]['type'] == 'type2'
def test_invite_player_already_joined(): test_iid = test_utils.make_instance() invitee = '*****@*****.**' test_utils.add_player(test_iid, invitee) response = app.post('/invite', {'gid': gid, 'iid' : test_iid, 'inv' : invitee}).json assert response['e'] is False assert response['response']['inv'] == '' assert response['request_type'] == '/invite' instance_lists = test_utils.get_invited_and_joined_instances(test_iid, invitee) assert test_iid in instance_lists['joined'] assert test_iid not in instance_lists['invited']
def test_player_left(): iid = test_utils.make_instance() for player in init_players: test_utils.add_player(iid, player) # Start the game current_round = 1 response = test_utils.post_server_command(iid, 'ata_new_game', []) instance = test_utils.get_instance_model(iid) noun_cards_left = card_game.cards_left(instance) assert instance.ata_round == current_round # Get the cards for player in players: hand = test_utils.get_messages(iid, 'crd_hand', '', 1, pid=player)[0] assert len(hand['contents']) == 7 player_cards[player] = hand['contents'] player_submissions = {} instance = test_utils.get_instance_model(iid) removed_player = instance.players[2] instance.players.remove(removed_player) instance.put() card = player_cards[player].pop() response = test_utils.post_server_command(iid, 'ata_submit_card', [current_round, card], pid=instance.players[1]) assert len(response['contents']) == 1 instance = test_utils.get_instance_model(iid) assert removed_player in instance.invited test_utils.add_player(iid, removed_player) # Submit cards for player in players: if player != instance.leader: card = player_cards[player].pop() response = test_utils.post_server_command(iid, 'ata_submit_card', [current_round, card], pid=player) player_cards[player] = response['contents'][2] player_submissions[player] = card for c in player_submissions.values(): assert c in response['contents'][1] assert len(player_cards[player]) == 7
def test_send_message_to_multiple_people(): test_iid = test_utils.make_instance() mtype = 'test' contents = ['quack', 'duck'] player2 = '*****@*****.**' test_utils.add_player(test_iid, player2) test_utils.send_new_message(test_iid, 'type1', [firstpid, player2], contents) test_utils.send_new_message(test_iid, 'type2', [firstpid, player2], contents) response = test_utils.get_messages(test_iid, '', '', 2) assert response[0]['type'] == 'type1' assert response[1]['type'] == 'type2' response = test_utils.get_messages(test_iid, '', '', 2, pid=player2) assert response[0]['type'] == 'type1' assert response[1]['type'] == 'type2'
def test_player_left(): iid = test_utils.make_instance() for player in init_players: test_utils.add_player(iid, player) # Start the game current_round = 1 response = test_utils.post_server_command(iid, 'ata_new_game', []) instance = test_utils.get_instance_model(iid) noun_cards_left = card_game.cards_left(instance) assert instance.ata_round == current_round # Get the cards for player in players: hand = test_utils.get_messages(iid, 'crd_hand', '', 1, pid = player)[0] assert len(hand['contents']) == 7 player_cards[player] = hand['contents'] player_submissions = {} instance = test_utils.get_instance_model(iid) removed_player = instance.players[2] instance.players.remove(removed_player) instance.put() card = player_cards[player].pop() response = test_utils.post_server_command(iid, 'ata_submit_card', [current_round, card], pid = instance.players[1]) assert len(response['contents']) == 1 instance = test_utils.get_instance_model(iid) assert removed_player in instance.invited test_utils.add_player(iid, removed_player) # Submit cards for player in players: if player != instance.leader: card = player_cards[player].pop() response = test_utils.post_server_command(iid, 'ata_submit_card', [current_round, card], pid = player) player_cards[player] = response['contents'][2] player_submissions[player] = card for c in player_submissions.values(): assert c in response['contents'][1] assert len(player_cards[player]) == 7
def test_invite_player_already_joined(): test_iid = test_utils.make_instance() invitee = '*****@*****.**' test_utils.add_player(test_iid, invitee) response = app.post('/invite', { 'gid': gid, 'iid': test_iid, 'inv': invitee }).json assert response['e'] is False assert response['response']['inv'] == '' assert response['request_type'] == '/invite' instance_lists = test_utils.get_invited_and_joined_instances( test_iid, invitee) assert test_iid in instance_lists['joined'] assert test_iid not in instance_lists['invited']
def test_two_games_at_once(): bob = "*****@*****.**" iid = test_utils.make_instance() test_utils.add_player(iid, bob) game_id = new_game(iid) bob_game_id = new_game(iid, bob) game = get_game(iid, game_id) bob_game = get_game(iid, bob_game_id) guess = game.bac_solution[::-1] score = game.bac_score response = test_utils.post_server_command(iid, "bac_guess", [game_id, guess]) guess = bob_game.bac_solution response = test_utils.post_server_command(iid, "bac_guess", [bob_game_id, guess], pid=bob) assert response["contents"] == [[score, score, 1], True] guess = game.bac_solution response = test_utils.post_server_command(iid, "bac_guess", [game_id, guess]) assert response["contents"] == [[score - 4, score - 4, 1], True]
def test_deal_all_cards(): iid = test_utils.make_instance_with_players() players_list = test_utils.add_player(iid, '*****@*****.**') args = [13, True, True, False, players_list] response = test_utils.post_server_command(iid, 'crd_deal_cards', args) assert len(response['contents']) == 13 for hand in get_hands(iid).values(): assert len(hand) == 13 assert get_cards_left(iid) == 0
def test_only_leader_can_set_new_leader(): test_iid = test_utils.make_instance() new_player = '*****@*****.**' test_utils.add_player(test_iid, new_player) response = app.post('/setleader', {'gid': gid, 'iid' : test_iid, 'pid' : new_player, 'leader' : new_player}).json assert response['e'] is False assert response['leader'] == firstpid assert response['response']['leader_changed'] == False assert response['response']['current_leader'] == firstpid assert response['request_type'] == '/setleader' response = app.post('/getinstancelists', {'gid': gid, 'iid' : test_iid, 'pid' : firstpid}).json assert response['leader'] == firstpid
def test_wrong_round(): iid = test_utils.make_instance() for player in init_players: test_utils.add_player(iid, player) current_round = 1 response = test_utils.post_server_command(iid, 'ata_new_game', []) char_card = response['contents'][0] contents = test_utils.post_server_command(iid, 'ata_submit_card', [2, ''], pid = firstpid)['contents'] assert isinstance(contents[0], basestring) assert len(contents[1]) == 7 assert contents[2] == current_round assert contents[3] == char_card contents = test_utils.post_server_command(iid, 'ata_end_turn', [2, ''], pid = firstpid)['contents'] assert isinstance(contents[0], basestring) assert len(contents[1]) == 7 assert contents[2] == current_round assert contents[3] == char_card
def test_leave_instance(): test_utils.clear_data_store() iid = test_utils.make_instance() other = '*****@*****.**' test_utils.add_player(iid, other) response = app.post('/leaveinstance', {'gid': gid, 'iid' : iid, 'pid' : firstpid}).json assert response['e'] is False assert response['request_type'] == '/leaveinstance' assert response['leader'] == '' assert response['response']['joined'] == [] response = app.post('/leaveinstance', {'gid': gid, 'iid' : iid, 'pid' : other}).json assert response['e'] is False assert response['request_type'] == '/leaveinstance' assert response['leader'] == '' assert response['iid'] == '' assert response['response']['joined'] == [] response = app.post('/joininstance', {'gid': gid, 'iid' : iid, 'pid' : firstpid}).json assert response['e'] is True
def test_wrong_round(): iid = test_utils.make_instance() for player in init_players: test_utils.add_player(iid, player) current_round = 1 response = test_utils.post_server_command(iid, 'ata_new_game', []) char_card = response['contents'][0] contents = test_utils.post_server_command(iid, 'ata_submit_card', [2, ''], pid=firstpid)['contents'] assert isinstance(contents[0], basestring) assert len(contents[1]) == 7 assert contents[2] == current_round assert contents[3] == char_card contents = test_utils.post_server_command(iid, 'ata_end_turn', [2, ''], pid=firstpid)['contents'] assert isinstance(contents[0], basestring) assert len(contents[1]) == 7 assert contents[2] == current_round assert contents[3] == char_card
def test_two_games_at_once(): bob = '*****@*****.**' iid = test_utils.make_instance() test_utils.add_player(iid, bob) game_id = new_game(iid) bob_game_id = new_game(iid, bob) game = get_game(iid, game_id) bob_game = get_game(iid, bob_game_id) guess = game.bac_solution[::-1] score = game.bac_score response = test_utils.post_server_command(iid, 'bac_guess', [game_id, guess]) guess = bob_game.bac_solution response = test_utils.post_server_command(iid, 'bac_guess', [bob_game_id, guess], pid=bob) assert response['contents'] == [[score, score, 1], True] guess = game.bac_solution response = test_utils.post_server_command(iid, 'bac_guess', [game_id, guess]) assert response['contents'] == [[score - 4, score - 4, 1], True]
def test_only_leader_can_set_new_leader(): test_iid = test_utils.make_instance() new_player = '*****@*****.**' test_utils.add_player(test_iid, new_player) response = app.post('/setleader', { 'gid': gid, 'iid': test_iid, 'pid': new_player, 'leader': new_player }).json assert response['e'] is False assert response['leader'] == firstpid assert response['response']['leader_changed'] == False assert response['response']['current_leader'] == firstpid assert response['request_type'] == '/setleader' response = app.post('/getinstancelists', { 'gid': gid, 'iid': test_iid, 'pid': firstpid }).json assert response['leader'] == firstpid
def test_full_game(): iid = test_utils.make_instance() for player in init_players: test_utils.add_player(iid, player) # Start the game current_round = 1 response = test_utils.post_server_command(iid, 'ata_new_game', []) instance = test_utils.get_instance_model(iid) noun_cards_left = card_game.cards_left(instance) assert instance.ata_round == current_round # Get the cards for player in players: hand = test_utils.get_messages(iid, 'crd_hand', '', 1, pid=player)[0] assert len(hand['contents']) == 7 player_cards[player] = hand['contents'] player_submissions = {} # Submit cards for player in players: if player != instance.leader: card = player_cards[player].pop() response = test_utils.post_server_command(iid, 'ata_submit_card', [current_round, card], pid=player) player_cards[player] = response['contents'][2] player_submissions[player] = card for c in player_submissions.values(): assert c in response['contents'][1] assert len(player_cards[player]) == 7 instance = test_utils.get_instance_model(iid) assert noun_cards_left - len(players) + 1 == card_game.cards_left(instance) for player in players: response = test_utils.get_messages(iid, 'ata_submissions', '', 1, pid=player)[0] for c in player_submissions.values(): assert c in response['contents'][1] assert response['contents'][0] == current_round # Choose a winner winner = [x for x in players if x != instance.leader][0] contents = test_utils.post_server_command( iid, 'ata_end_turn', [current_round, player_submissions[winner]], pid=instance.leader)['contents'] assert contents[2] == 2 assert contents[3] == winner assert contents[4] == player_submissions[winner] assert [1, winner] in contents[1] current_round = current_round + 1 instance = test_utils.get_instance_model(iid) assert instance.ata_round == current_round # Check for next round messages for player in players: new_round_msg = test_utils.get_messages(iid, 'ata_new_round', '', 1, pid=player)[0]['contents'] assert new_round_msg[2] == 2 assert new_round_msg[3] == winner assert new_round_msg[4] == player_submissions[winner] # Accelerate the game, next point wins for player in players: scoreboard.set_score(instance, player, 4) instance.put() winner = [x for x in players if x != instance.leader][0] winning_card = player_cards[winner][0] test_utils.post_server_command(iid, 'ata_submit_card', [current_round, winning_card], pid=winner) contents = test_utils.post_server_command(iid, 'ata_end_turn', [current_round, winning_card], pid=instance.leader)['contents'] assert contents[0] == current_round assert contents[1] == winning_card assert [5, winner] in contents[2] # Check for game over messages for player in players: contents = test_utils.get_messages(iid, 'ata_game_over', '', 1, pid=player)[0]['contents'] assert contents[0] == current_round assert contents[1] == winning_card assert [5, winner] in contents[2] instance = test_utils.get_instance_model(iid) assert 'ata_round' not in instance.dynamic_properties() assert 'ata_char_card' not in instance.dynamic_properties() assert 'ata_submissions' not in instance.dynamic_properties()
def test_full_game(): iid = test_utils.make_instance() for player in init_players: test_utils.add_player(iid, player) # Start the game current_round = 1 response = test_utils.post_server_command(iid, 'ata_new_game', []) instance = test_utils.get_instance_model(iid) noun_cards_left = card_game.cards_left(instance) assert instance.ata_round == current_round # Get the cards for player in players: hand = test_utils.get_messages(iid, 'crd_hand', '', 1, pid = player)[0] assert len(hand['contents']) == 7 player_cards[player] = hand['contents'] player_submissions = {} # Submit cards for player in players: if player != instance.leader: card = player_cards[player].pop() response = test_utils.post_server_command(iid, 'ata_submit_card', [current_round, card], pid = player) player_cards[player] = response['contents'][2] player_submissions[player] = card for c in player_submissions.values(): assert c in response['contents'][1] assert len(player_cards[player]) == 7 instance = test_utils.get_instance_model(iid) assert noun_cards_left - len(players) + 1 == card_game.cards_left(instance) for player in players: response = test_utils.get_messages(iid, 'ata_submissions', '', 1, pid = player)[0] for c in player_submissions.values(): assert c in response['contents'][1] assert response['contents'][0] == current_round # Choose a winner winner = [x for x in players if x != instance.leader][0] contents = test_utils.post_server_command(iid, 'ata_end_turn', [current_round, player_submissions[winner]], pid = instance.leader)['contents'] assert contents[2] == 2 assert contents[3] == winner assert contents[4] == player_submissions[winner] assert [1, winner] in contents[1] current_round = current_round + 1 instance = test_utils.get_instance_model(iid) assert instance.ata_round == current_round # Check for next round messages for player in players: new_round_msg = test_utils.get_messages(iid, 'ata_new_round', '', 1, pid = player)[0]['contents'] assert new_round_msg[2] == 2 assert new_round_msg[3] == winner assert new_round_msg[4] == player_submissions[winner] # Accelerate the game, next point wins for player in players: scoreboard.set_score(instance, player, 4) instance.put() winner = [x for x in players if x != instance.leader][0] winning_card = player_cards[winner][0] test_utils.post_server_command(iid, 'ata_submit_card', [current_round, winning_card], pid = winner) contents = test_utils.post_server_command(iid, 'ata_end_turn', [current_round, winning_card], pid = instance.leader)['contents'] assert contents[0] == current_round assert contents[1] == winning_card assert [5, winner] in contents[2] # Check for game over messages for player in players: contents = test_utils.get_messages(iid, 'ata_game_over', '', 1, pid = player)[0]['contents'] assert contents[0] == current_round assert contents[1] == winning_card assert [5, winner] in contents[2] instance = test_utils.get_instance_model(iid) assert 'ata_round' not in instance.dynamic_properties() assert 'ata_char_card' not in instance.dynamic_properties() assert 'ata_submissions' not in instance.dynamic_properties()
def test_not_your_game(): bob = "*****@*****.**" iid = test_utils.make_instance() test_utils.add_player(iid, bob) game_id = new_game(iid) response = test_utils.post_server_command(iid, "bac_guess", [game_id, ["Blue"] * 4], pid=bob, error_expected=True)