def test_get_cards_remaining(): iid = test_utils.make_instance_with_players() response = test_utils.post_server_command(iid, 'crd_cards_left', []) assert response['contents'] == [-1] test_utils.post_server_command(iid, 'crd_draw_cards', [5, False]) response = test_utils.post_server_command(iid, 'crd_cards_left', []) assert response['contents'] == [47]
def test_pass_cards(): iid = test_utils.make_instance_with_players() args = [7, True, True, False, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args) hands = get_hands(iid) # Player 2 passes four cards to player 1 passing_player = check_playerid(players[1]) initial_hand = hands[passing_player] args = [firstpid, initial_hand[3:]] response = test_utils.post_server_command(iid, 'crd_pass_cards', args, pid=passing_player) assert response['contents'] == initial_hand[:3] hands = get_hands(iid) assert len(hands[firstpid]) == 11 assert hands[firstpid][7:] == initial_hand[3:] # Player 1 passes all cards to player 3 receiving_player = check_playerid(players[2]) args = [receiving_player, hands[firstpid]] response = test_utils.post_server_command(iid, 'crd_pass_cards', args) assert response['contents'] == [] hands = get_hands(iid) assert len(hands[firstpid]) == 0 assert len(hands[receiving_player]) == 18
def test_pass_cards(): iid = test_utils.make_instance_with_players() args = [7, True, True, False, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args) hands = get_hands(iid) # Player 2 passes four cards to player 1 passing_player = check_playerid(players[1]) initial_hand = hands[passing_player] args = [firstpid, initial_hand[3:]] response = test_utils.post_server_command(iid, 'crd_pass_cards', args, pid = passing_player) assert response['contents'] == initial_hand[:3] hands = get_hands(iid) assert len(hands[firstpid]) == 11 assert hands[firstpid][7:] == initial_hand[3:] # Player 1 passes all cards to player 3 receiving_player = check_playerid(players[2]) args = [receiving_player, hands[firstpid]] response = test_utils.post_server_command(iid, 'crd_pass_cards', args) assert response['contents'] == [] hands = get_hands(iid) assert len(hands[firstpid]) == 0 assert len(hands[receiving_player]) == 18
def test_discard_cards_not_present(): iid = test_utils.make_instance_with_players() args = [7, True, True, False, players] test_utils.post_server_command(iid, 'crd_deal_cards', args) initial_hand = get_hands(iid)[firstpid] to_discard = [initial_hand[0]] + ['%s' % x for x in xrange(12)] response = test_utils.post_server_command(iid, 'crd_discard', to_discard) assert response['contents'] == initial_hand[1:]
def test_get_results(): iid = test_utils.make_instance() poll_id = make_poll(iid) test_utils.post_server_command(iid, 'vot_cast_vote', [poll_id, 1])['contents'] contents = test_utils.post_server_command(iid, 'vot_get_results', [poll_id])['contents'] assert contents == ['You have already voted in this poll.', [0, 1]]
def test_draw_cards(): iid = test_utils.make_instance_with_players() test_utils.post_server_command(iid, 'crd_draw_cards', [5, False]) hands = get_hands(iid) for player, hand in hands.items(): assert len(hand) == (5 if player == firstpid else 0) assert hands[firstpid] == get_player_hand(iid, firstpid) assert get_cards_left(iid) == 47
def test_out_of_guesses(): iid = test_utils.make_instance() game_id = new_game(iid) game = get_game(iid, game_id) game.bac_guesses_remaining = 0 game.put() guess = ["Blue", "Yellow", "Green", "Red"] test_utils.post_server_command(iid, "bac_guess", [game_id, [guess]], error_expected=True)
def test_delete_instance(): test_iid = test_utils.make_instance() state = test_utils.get_invited_and_joined_instances(test_iid, firstpid) assert test_iid in state["joined"] test_utils.post_server_command(test_iid, "sys_delete_instance", []) assert not test_utils.get_instance_model(test_iid) state = test_utils.get_invited_and_joined_instances("", firstpid) assert test_iid not in state["joined"]
def test_discard(): iid = test_utils.make_instance_with_players() args = [7, True, True, False, players] test_utils.post_server_command(iid, 'crd_deal_cards', args) initial_hand = get_hands(iid)[firstpid] response = test_utils.post_server_command(iid, 'crd_discard', initial_hand[:3]) assert response['contents'] == initial_hand[3:]
def test_get_reslts_before_voting_in_closed_poll(): iid = test_utils.make_instance() poll_id = make_poll(iid) test_utils.post_server_command(iid, 'vot_close_poll', [poll_id])['contents'] contents = test_utils.post_server_command(iid, 'vot_get_results', [poll_id])['contents'] assert contents == ['Poll is now closed.', [0, 0]]
def test_cast_vote_to_closed_poll(): iid = test_utils.make_instance() poll_id = make_poll(iid) test_utils.post_server_command(iid, 'vot_close_poll', [poll_id])['contents'] contents = test_utils.post_server_command(iid, 'vot_cast_vote', [poll_id, 1])['contents'] assert contents[1] == [0, 0] assert contents[0] == 'Poll closed to new votes.'
def test_cast_vote_twice(): iid = test_utils.make_instance() poll_id = make_poll(iid) test_utils.post_server_command(iid, 'vot_cast_vote', [poll_id, 1])['contents'] contents = test_utils.post_server_command(iid, 'vot_cast_vote', [poll_id, 1])['contents'] assert contents[1] == [0, 1] assert contents[0] == 'Your vote was already counted in this poll.'
def test_clear_and_get_scoreboard(): iid = test_utils.make_instance_with_players() response = set_score(iid, firstpid, 100) response = test_utils.post_server_command(iid, 'scb_get_scoreboard', []) assert [100, firstpid] in response['contents'] response = test_utils.post_server_command(iid, 'scb_clear_scoreboard', []) assert [0, firstpid] in response['contents'] response = test_utils.post_server_command(iid, 'scb_get_scoreboard', []) assert [0, firstpid] in response['contents']
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_deal_twice(): iid = test_utils.make_instance_with_players() args = [7, True, True, False, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args) args = [7, False, False, False, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args) assert len(response['contents']) == 14 for hand in get_hands(iid).values(): assert len(hand) == 14 assert get_cards_left(iid) == 10
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_out_of_guesses(): iid = test_utils.make_instance() game_id = new_game(iid) game = get_game(iid, game_id) game.bac_guesses_remaining = 0 game.put() guess = ['Blue', 'Yellow', 'Green', 'Red'] test_utils.post_server_command(iid, 'bac_guess', [game_id, [guess]], error_expected=True)
def test_scoreboard_rejects_unknown_players(): iid = test_utils.make_instance_with_players() #Try to add a scoreboard entry for a player not in the game, it should fail. fake_player = "*****@*****.**" args = [fake_player, 42] test_utils.post_server_command(iid, 'scb_add_to_score', args, error_expected = True, pid=fake_player) response = test_utils.post_server_command(iid, 'scb_get_scoreboard', []) assert [42, fake_player] not in response['contents']
def test_delete_instance_and_messages(): test_iid = test_utils.make_instance() instance = test_utils.get_instance_model(test_iid) for i in xrange(10): message = Message(parent=instance, sender=firstpid, msg_type="blah", recipient=firstpid, content="%d" % i) message.put() messages = Message.all(keys_only=True).ancestor(instance.key()).fetch(1000) assert len(messages) == 10 test_utils.post_server_command(test_iid, "sys_delete_instance", []) messages = Message.all(keys_only=True).ancestor(instance.key()).fetch(1000) assert len(messages) == 0
def test_deal_too_many_cards(): iid = test_utils.make_instance_with_players() args = [500, True, True, False, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args, error_expected = True) args = [500, True, True, True, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args, error_expected = False) assert len(response['contents']) == 18 for player, hand in get_hands(iid).items(): assert len(hand) == (18 if player == firstpid else 17) assert get_cards_left(iid) == 0
def test_guesses(): iid = test_utils.make_instance() game_id = new_game(iid) game = get_game(iid, game_id) guess = game.bac_solution[::-1] score = game.bac_score guesses = game.bac_guesses_remaining response = test_utils.post_server_command(iid, "bac_guess", [game_id, guess]) assert response["contents"] == [guesses - 1, score - 4, 0, 4] guess = [game.bac_solution[0]] * 4 response = test_utils.post_server_command(iid, "bac_guess", [game_id, guess]) assert response["contents"] == [guesses - 2, score - 7, 1, 3]
def test_scoreboard_rejects_unknown_players(): iid = test_utils.make_instance_with_players() #Try to add a scoreboard entry for a player not in the game, it should fail. fake_player = "*****@*****.**" args = [fake_player, 42] test_utils.post_server_command(iid, 'scb_add_to_score', args, error_expected=True, pid=fake_player) response = test_utils.post_server_command(iid, 'scb_get_scoreboard', []) assert [42, fake_player] not in response['contents']
def test_set_public_with_strings(): # Make sure new games are not public iid = test_utils.make_instance_with_players() # Set public to true with lowercase string test_utils.post_server_command(iid, "sys_set_public", ["true"]) instance = test_utils.get_instance_model(iid) assert instance.public # Set public to false with lowercase string test_utils.post_server_command(iid, "sys_set_public", ["false"]) instance = test_utils.get_instance_model(iid) assert not instance.public
def test_decline_invite(): test_iid = test_utils.make_instance() invitee = "*****@*****.**" response = app.post("/invite", {"gid": gid, "iid": test_iid, "inv": invitee}).json assert response["e"] is False assert response["response"]["inv"] == invitee assert response["request_type"] == "/invite" state = test_utils.get_invited_and_joined_instances(test_iid, invitee) assert test_iid in state["invited"] test_utils.post_server_command(test_iid, "sys_decline_invite", [], pid=invitee) state = test_utils.get_invited_and_joined_instances(test_iid, invitee) assert test_iid not in state["invited"]
def test_set_deck(): iid = test_utils.make_instance_with_players() deck = [[n, s] for n in range(14)[4:] for s in ['Hearts','Spades']] missing_cards = [[n, s] for n in range(4)[1:] for s in ['Hearts', 'Spades']] response = test_utils.post_server_command(iid, 'crd_set_deck', deck) assert response['contents'] == [20] # Set deck again, should fail. test_utils.post_server_command(iid, 'crd_set_deck', deck, error_expected = True) # Draw all the cards response = test_utils.post_server_command(iid, 'crd_draw_cards', [40, True]) for card in missing_cards: assert card not in response['contents']
def test_guesses(): iid = test_utils.make_instance() game_id = new_game(iid) game = get_game(iid, game_id) guess = game.bac_solution[::-1] score = game.bac_score guesses = game.bac_guesses_remaining response = test_utils.post_server_command(iid, 'bac_guess', [game_id, guess]) assert response['contents'] == [guesses - 1, score - 4, 0, 4] guess = [game.bac_solution[0]] * 4 response = test_utils.post_server_command(iid, 'bac_guess', [game_id, guess]) assert response['contents'] == [guesses - 2, score - 7, 1, 3]
def test_deal_new_hand(): iid = test_utils.make_instance_with_players() args = [7, True, True, False, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args) assert len(response['contents']) == 7 for player, hand in get_hands(iid).items(): assert len(hand) == 7 assert hand == get_player_hand(iid, player) assert get_cards_left(iid) == 31 args = [7, False, True, False, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args) assert len(response['contents']) == 7 for hand in get_hands(iid).values(): assert len(hand) == 7 assert get_cards_left(iid) == 10
def test_set_public(): # Make sure new games are not public iid = test_utils.make_instance_with_players() instance = test_utils.get_instance_model(iid) assert not instance.public # Set public to True test_utils.post_server_command(iid, "sys_set_public", [True]) instance = test_utils.get_instance_model(iid) assert instance.public # Set public to False test_utils.post_server_command(iid, "sys_set_public", [False]) instance = test_utils.get_instance_model(iid) assert not instance.public
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_deal_too_many_cards(): iid = test_utils.make_instance_with_players() args = [500, True, True, False, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args, error_expected=True) args = [500, True, True, True, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args, error_expected=False) assert len(response['contents']) == 18 for player, hand in get_hands(iid).items(): assert len(hand) == (18 if player == firstpid else 17) assert get_cards_left(iid) == 0
def test_cast_vote(): iid = test_utils.make_instance() poll_id = make_poll(iid) contents = test_utils.post_server_command(iid, 'vot_cast_vote', [poll_id, 1])['contents'] assert contents[1] == [0, 1] assert contents[0] == 'Vote accepted.'
def test_add_to_score(): iid = test_utils.make_instance_with_players() score = 100 # Add the first player to the scoreboard change_response = add_to_score(iid, firstpid, score) response = test_utils.post_server_command(iid, 'scb_get_scoreboard', []) assert response['contents']==change_response['contents'] assert [score, firstpid] in response['contents'] # Increment first players score and add another player add_to_score(iid, firstpid, score) add_to_score(iid, players[1], score) response = test_utils.post_server_command(iid, 'scb_get_scoreboard', []) assert [score * 2, players[0]] in response['contents'] assert [score, check_playerid(players[1])] in response['contents']
def test_add_to_score(): iid = test_utils.make_instance_with_players() score = 100 # Add the first player to the scoreboard change_response = add_to_score(iid, firstpid, score) response = test_utils.post_server_command(iid, 'scb_get_scoreboard', []) assert response['contents'] == change_response['contents'] assert [score, firstpid] in response['contents'] # Increment first players score and add another player add_to_score(iid, firstpid, score) add_to_score(iid, players[1], score) response = test_utils.post_server_command(iid, 'scb_get_scoreboard', []) assert [score * 2, players[0]] in response['contents'] assert [score, check_playerid(players[1])] in response['contents']
def test_delete_poll(): iid = test_utils.make_instance() poll_id = make_poll(iid) contents = test_utils.post_server_command(iid, 'vot_delete_poll', [poll_id])['contents'] assert contents == [True] poll = get_poll(iid, poll_id) assert not poll
def test_join_public(): iid = test_utils.make_instance_with_players() # Make sure that an uninvited player cannot join. playerid = "*****@*****.**" response = app.post("/joininstance", {"gid": gid, "iid": iid, "pid": playerid}).json assert response["e"] is True assert "not invited" in response["response"] # Set the game to public and confirm that uninvited players can join. test_utils.post_server_command(iid, "sys_set_public", [True]) response = app.post("/joininstance", {"gid": gid, "iid": iid, "pid": playerid}).json assert response["e"] is False state = test_utils.get_invited_and_joined_instances(iid, playerid) assert iid not in state["invited"] assert iid in state["joined"]
def test_draw_cards_beyond_deck(): iid = test_utils.make_instance_with_players() # Draw too many cards with ignore empty deck set to false. # There should be no effect. test_utils.post_server_command(iid, 'crd_draw_cards', [75, False], error_expected = True) hands = get_hands(iid) for hand in hands.values(): assert len(hand) == 0 assert get_cards_left(iid) == -1 # Draw too many cards with ignore empty deck, should deal all cards. test_utils.post_server_command(iid, 'crd_draw_cards', [75, True]) hands = get_hands(iid) for player, hand in hands.items(): assert len(hand) == (52 if player == firstpid else 0) assert get_cards_left(iid) == 0
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_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_draw_cards_beyond_deck(): iid = test_utils.make_instance_with_players() # Draw too many cards with ignore empty deck set to false. # There should be no effect. test_utils.post_server_command(iid, 'crd_draw_cards', [75, False], error_expected=True) hands = get_hands(iid) for hand in hands.values(): assert len(hand) == 0 assert get_cards_left(iid) == -1 # Draw too many cards with ignore empty deck, should deal all cards. test_utils.post_server_command(iid, 'crd_draw_cards', [75, True]) hands = get_hands(iid) for player, hand in hands.items(): assert len(hand) == (52 if player == firstpid else 0) assert get_cards_left(iid) == 0
def test_set_deck(): iid = test_utils.make_instance_with_players() deck = [[n, s] for n in range(14)[4:] for s in ['Hearts', 'Spades']] missing_cards = [[n, s] for n in range(4)[1:] for s in ['Hearts', 'Spades']] response = test_utils.post_server_command(iid, 'crd_set_deck', deck) assert response['contents'] == [20] # Set deck again, should fail. test_utils.post_server_command(iid, 'crd_set_deck', deck, error_expected=True) # Draw all the cards response = test_utils.post_server_command(iid, 'crd_draw_cards', [40, True]) for card in missing_cards: assert card not in response['contents']
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_close_poll(): iid = test_utils.make_instance() poll_id = make_poll(iid) contents = test_utils.post_server_command(iid, 'vot_close_poll', [poll_id])['contents'] assert contents == [question, choices, poll_id, [0] * len(choices), False] poll = get_poll(iid, poll_id) assert poll.msg_type == 'closed_poll' assert poll.open == False
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_pass_cards_not_present(): iid = test_utils.make_instance_with_players() args = [7, True, True, False, players] response = test_utils.post_server_command(iid, 'crd_deal_cards', args) hands = get_hands(iid) # Player 2 passes four cards to player 1 that she has and two cards that # she doesn't have. passing_player = check_playerid(players[1]) initial_hand = hands[passing_player] args = [firstpid, ['fake_card_1'] + initial_hand[3:] + ['fake_card_2']] response = test_utils.post_server_command(iid, 'crd_pass_cards', args, pid=passing_player) assert response['contents'] == initial_hand[:3] hands = get_hands(iid) assert len(hands[firstpid]) == 11 assert hands[firstpid][7:] == initial_hand[3:]
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_got_solution(): iid = test_utils.make_instance() game_id = new_game(iid) game = get_game(iid, game_id) guess = game.bac_solution score = game.bac_score guesses = game.bac_guesses_remaining response = test_utils.post_server_command(iid, 'bac_guess', [game_id, guess]) assert response['contents'] == [[score, score, 1], True] game = get_game(iid, game_id) assert game.bac_guesses_remaining == 0