def test_pairings(self): """Test pairing players.""" tournament_id = tournament.register_tournament( "Test Pairings Tournament", 4) player_names = ("Twilight Sparkle", "Fluttershy", "Applejack", "Pinkie Pie") for player_name in player_names: tournament.register_player_in_tournament( tournament.register_player(player_name), tournament_id) standings = tournament.player_standings_by_tournament(tournament_id) player1, player2, player3, player4 = [row[0] for row in standings] tournament.report_match(player1, player2, tournament_id) tournament.report_match(player3, player4, tournament_id) pairings = tournament.swiss_pairings(tournament_id) # For four players, swiss_pairings should return two pairs self.assertEqual(len(pairings), 2) # After one match, players with one win should be paired [(id1, name1, id2, name2), (id3, name3, id4, name4)] = pairings correct_pairs = set( [frozenset([player1, player3]), frozenset([player2, player4])]) actual_pairs = set([frozenset([id1, id2]), frozenset([id3, id4])]) self.assertEqual(actual_pairs, correct_pairs) print "* After one match, players with one win are paired."
def test_pairings_with_bye(self): """Test pairing players with an odd number of players.""" tournament_id = tournament.register_tournament( "Test Pairings With Bye Tournament", 5) player_names = ("Twilight Sparkle", "Fluttershy", "Applejack", "Pinkie Pie", "Brandy Ruby") for player_name in player_names: tournament.register_player_in_tournament( tournament.register_player(player_name), tournament_id) standings = tournament.player_standings_by_tournament(tournament_id) player1, player2, player3, player4, player5 = [ row[0] for row in standings ] tournament.report_match(player1, player2, tournament_id) tournament.report_match(player3, player4, tournament_id) # Report a match bye for player 5 to test tournament.report_match_bye(player5, tournament_id) pairings = tournament.swiss_pairings(tournament_id) [(id1, name1, id2, name2), (id3, name3, id4, name4)] = pairings correct_pairs = set( [frozenset([player1, player3]), frozenset([player5, player2])]) actual_pairs = set([frozenset([id1, id2]), frozenset([id3, id4])]) self.assertEqual(actual_pairs, correct_pairs) # Ensure player5 did not receive another bye self.assertIn(player5, (id1, id2, id3, id4)) print( "* After one match where one player was granted a bye, " "players with one win are paired.")
def test_report_matches(self): """Test reporting matches.""" tournament_id = tournament.register_tournament( "Test Matches Tournament", 4) player_names = ("Bruno Walton", "Boots O'Neal", "Cathy Burton", "Diane Grant") for player_name in player_names: tournament.register_player_in_tournament( tournament.register_player(player_name), tournament_id) standings = tournament.player_standings_by_tournament(tournament_id) player1, player2, player3, player4 = [row[0] for row in standings] tournament.report_match(player1, player2, tournament_id) tournament.report_match(player3, player4, tournament_id) standings = tournament.player_standings() for id, name, wins, matches in standings: # Each player should have one match recorded self.assertEqual(matches, 1) # Each match winner should have one win recorded if id in (player1, player3): self.assertEqual(wins, 1) # Each match loser should have zero wins recorded elif id in (player2, player4): self.assertEqual(wins, 0) print "* After a match, players have updated standings."
def test_rank_by_opponent_match_wins(self): """Test ranking standings by opponent match wins.""" tournament_id = tournament.register_tournament("Test OMW Tournament", 4) player_names = ("Bruno Walton", "Boots O'Neal", "Cathy Burton", "Diane Grant") for player_name in player_names: tournament.register_player_in_tournament( tournament.register_player(player_name), tournament_id) standings = tournament.player_standings_by_tournament(tournament_id) player1, player2, player3, player4 = [row[0] for row in standings] tournament.report_match(player1, player2, tournament_id) tournament.report_match(player3, player4, tournament_id) # Give player a bye to force re-ordering by omw tournament.report_match_bye(player1, tournament_id) standings = tournament.player_standings_by_tournament(tournament_id) omw_standings = tournament.rank_by_opponent_match_wins( standings, tournament_id) # Standings should be re-ordered (Boots and Diane); Boots lost to # Bruno, who has more wins than Cathy (who Diane lost to) self.assertEqual(omw_standings, [(1, 'Bruno Walton', 2, 2), (3, 'Cathy Burton', 1, 1), (2, "Boots O'Neal", 0, 1), (4, 'Diane Grant', 0, 1)]) print "* Matches are ranked by opponent match wins."
def test_delete_matches(self): """Test matches can be deleted.""" tournament_id = tournament.register_tournament("Test Delete Matches", 2) player1_id = tournament.register_player("Twilight Sparkle") player2_id = tournament.register_player("Fluttershy") tournament.register_player_in_tournament(player1_id, tournament_id) tournament.register_player_in_tournament(player2_id, tournament_id) tournament.report_match(player1_id, player2_id, tournament_id) matches_deleted = tournament.delete_matches() self.assertEqual(matches_deleted, 2) print "* Old matches can be deleted."
def generate_whole_swiss_tournament(self): """Display each round statistics in a swiss tournament. Display winner's name. """ # clean database delete_matches() delete_players() print '\n\nSWISS TOURNAMENT FOR {0} PLAYERS:'.format( self.NR_OF_PLAYERS) # register {NR_OF_PLAYERS} players i = 0 while i < self.NR_OF_PLAYERS: register_player("Player " + str(i + 1)) i += 1 # calculate nr of rounds necessary to determine a winner rounds = log(self.NR_OF_PLAYERS) / log(2) i = 0 while i < int(rounds): # determine next round matches pairings = swiss_pairings() group = {} j = 0 z = 0 # register next round matches while j < self.NR_OF_PLAYERS: (group['pid' + str(j)], group['pname' + str(j)], group['pid' + str(j + 1)], group['pname' + str(j + 1)]) = pairings[z] report_match(group['pid' + str(j)], group['pid' + str(j + 1)]) j += 2 z += 1 print '\nStats after round: ' + str(i + 1) # show round statistics first_player = self.stats() print '\n' i += 1 print "-------------------------" print "THE WINNER IS: " + first_player + "." print "-------------------------\n\n"
def test_report_matches(self): """Test that matches are reported properly. Test to confirm matches are deleted properly. """ self._register_players( ["Bruno Walton", "Boots O'Neal", "Cathy Burton", "Diane Grant"]) standings = player_standings() [id1, id2, id3, id4] = [row[0] for row in standings] report_match(id1, id2) report_match(id3, id4) self._assert_standings_values_after_first_round(id1, id2, id3, id4) print "7. After a match, players have updated standings." delete_matches() self._assert_standings_values_after_delete_matches() print "8. After match deletion, player standings are properly reset." print "9. Matches are properly deleted."
def test_report_match_with_tie(self): """Test reporting a match tie.""" tournament_id = tournament.register_tournament( "Test Matches With Tie Tournament", 2) player_names = ("Bruno Walton", "Boots O'Neal") for player_name in player_names: tournament.register_player_in_tournament( tournament.register_player(player_name), tournament_id) standings = tournament.player_standings_by_tournament(tournament_id) player1, player2 = [row[0] for row in standings] tournament.report_match(player1, player2, tournament_id, tie=True) standings = tournament.player_standings() for id, name, wins, matches in standings: # Each player should have one match recorded self.assertEqual(matches, 1) # Each match winner should have one win recorded self.assertEqual(wins, 1) print "* After a match tie, tied players both have wins."
def test_report_matches(): '''This function tests to see if after a match the players' standing have been updated.''' tournament.delete_matches() tournament.delete_players() tournament.register_player("Bruno Walton") tournament.register_player("Boots O'Neal") tournament.register_player("Cathy Burton") tournament.register_player("Diane Grant") standings = tournament.player_standings() [id1, id2, id3, id4] = [row[0] for row in standings] tournament.report_match(id1, id2) tournament.report_match(id3, id4) standings = tournament.player_standings() for (i, n, w, m) in standings: if m != 1: raise ValueError("Each player should have one match recorded.") if i in (id1, id3) and w != 1: raise ValueError("Each match winner should have one win recorded.") elif i in (id2, id4) and w != 0: raise ValueError("Each match loser should have zero wins " "recorded.") print "7. After a match, players have updated standings."
def test_pairings(): '''This tests to see if match pairings work properly.''' tournament.delete_matches() tournament.delete_players() tournament.register_player("Twilight Sparkle") tournament.register_player("Fluttershy") tournament.register_player("Applejack") tournament.register_player("Pinkie Pie") standings = tournament.player_standings() [id1, id2, id3, id4] = [row[0] for row in standings] tournament.report_match(id1, id2) tournament.report_match(id3, id4) pairings = tournament.swiss_pairings() if len(pairings) != 2: raise ValueError( "For four players, swiss_Pairings should return two pairs.") [(pid1, pname1, pid2, pname2), (pid3, pname3, pid4, pname4)] = pairings correct_pairs = set([frozenset([id1, id3]), frozenset([id2, id4])]) actual_pairs = set([frozenset([pid1, pid2]), frozenset([pid3, pid4])]) if correct_pairs != actual_pairs: raise ValueError( "After one match, players with one win should be paired.") print "8. After one match, players with one win are paired."
def test_prevent_rematches(self): """Test that the system does not allow rematches. Test that the system prevent matches between a player and himself. """ self._register_players( ["Bruno Walton", "Boots O'Neal", "Cathy Burton", "Diane Grant"]) standings = player_standings() [id1, id2, id3, id4] = [row[0] for row in standings] report_match(id1, id2) report_match(id3, id4) self.assertRaises(DatabaseError, report_match(id2, id1)) self.assertRaises(DatabaseError, report_match(id2, id2)) print "11. Rematches are properly prevented."
def _register_first_round(self, id1, id2, id3, id4, id5, id6, id7, id8): report_match(id1, id2) report_match(id3, id4) report_match(id5, id6) report_match(id7, id8)