11#!/usr/bin/env python
22#
33# Test cases for tournament.py
4+ # These tests are not exhaustive, but they should cover the majority of cases.
5+ #
6+ # If you do add any of the extra credit options, be sure to add/modify these test cases
7+ # as appropriate to account for your module's added functionality.
48
59from tournament import *
610
7- def testDeleteMatches ():
8- deleteMatches ()
9- print "1. Old matches can be deleted."
10-
11-
12- def testDelete ():
13- deleteMatches ()
14- deletePlayers ()
15- print "2. Player records can be deleted."
16-
17-
1811def testCount ():
12+ """
13+ Test for initial player count,
14+ player count after 1 and 2 players registered,
15+ player count after players deleted.
16+ """
1917 deleteMatches ()
2018 deletePlayers ()
2119 c = countPlayers ()
2220 if c == '0' :
2321 raise TypeError (
24- "countPlayers() should return numeric zero, not string '0'." )
22+ "countPlayers should return numeric zero, not string '0'." )
2523 if c != 0 :
26- raise ValueError ("After deleting, countPlayers should return zero." )
27- print "3. After deleting, countPlayers() returns zero."
28-
29-
30- def testRegister ():
31- deleteMatches ()
32- deletePlayers ()
24+ raise ValueError ("After deletion, countPlayers should return zero." )
25+ print "1. countPlayers() returns 0 after initial deletePlayers() execution."
3326 registerPlayer ("Chandra Nalaar" )
3427 c = countPlayers ()
3528 if c != 1 :
3629 raise ValueError (
37- "After one player registers, countPlayers() should be 1." )
38- print "4. After registering a player, countPlayers() returns 1."
39-
40-
41- def testRegisterCountDelete ():
42- deleteMatches ()
43- deletePlayers ()
44- registerPlayer ("Markov Chaney" )
45- registerPlayer ("Joe Malik" )
46- registerPlayer ("Mao Tsu-hsi" )
47- registerPlayer ("Atlanta Hope" )
30+ "After one player registers, countPlayers() should be 1. Got {c}" .format (c = c ))
31+ print "2. countPlayers() returns 1 after one player is registered."
32+ registerPlayer ("Jace Beleren" )
4833 c = countPlayers ()
49- if c != 4 :
34+ if c != 2 :
5035 raise ValueError (
51- "After registering four players, countPlayers should be 4." )
36+ "After two players register, countPlayers() should be 2. Got {c}" .format (c = c ))
37+ print "3. countPlayers() returns 2 after two players are registered."
5238 deletePlayers ()
5339 c = countPlayers ()
5440 if c != 0 :
55- raise ValueError ("After deleting, countPlayers should return zero." )
56- print "5. Players can be registered and deleted."
57-
41+ raise ValueError (
42+ "After deletion, countPlayers should return zero." )
43+ print "4. countPlayers() returns zero after registered players are deleted. \n 5. Player records successfully deleted."
5844
5945def testStandingsBeforeMatches ():
46+ """
47+ Test to ensure players are properly represented in standings prior
48+ to any matches being reported.
49+ """
6050 deleteMatches ()
6151 deletePlayers ()
6252 registerPlayer ("Melpomene Murray" )
@@ -78,8 +68,11 @@ def testStandingsBeforeMatches():
7868 "even if they have no matches played." )
7969 print "6. Newly registered players appear in the standings with no matches."
8070
81-
8271def testReportMatches ():
72+ """
73+ Test that matches are reported properly.
74+ Test to confirm matches are deleted properly.
75+ """
8376 deleteMatches ()
8477 deletePlayers ()
8578 registerPlayer ("Bruno Walton" )
@@ -99,41 +92,64 @@ def testReportMatches():
9992 elif i in (id2 , id4 ) and w != 0 :
10093 raise ValueError ("Each match loser should have zero wins recorded." )
10194 print "7. After a match, players have updated standings."
102-
95+ deleteMatches ()
96+ standings = playerStandings ()
97+ if len (standings ) != 4 :
98+ raise ValueError ("Match deletion should not change number of players in standings." )
99+ for (i , n , w , m ) in standings :
100+ if m != 0 :
101+ raise ValueError ("After deleting matches, players should have zero matches recorded." )
102+ if w != 0 :
103+ raise ValueError ("After deleting matches, players should have zero wins recorded." )
104+ print "8. After match deletion, player standings are properly reset.\n 9. Matches are properly deleted."
103105
104106def testPairings ():
107+ """
108+ Test that pairings are generated properly both before and after match reporting.
109+ """
105110 deleteMatches ()
106111 deletePlayers ()
107112 registerPlayer ("Twilight Sparkle" )
108113 registerPlayer ("Fluttershy" )
109114 registerPlayer ("Applejack" )
110115 registerPlayer ("Pinkie Pie" )
116+ registerPlayer ("Rarity" )
117+ registerPlayer ("Rainbow Dash" )
118+ registerPlayer ("Princess Celestia" )
119+ registerPlayer ("Princess Luna" )
111120 standings = playerStandings ()
112- [id1 , id2 , id3 , id4 ] = [row [0 ] for row in standings ]
121+ [id1 , id2 , id3 , id4 , id5 , id6 , id7 , id8 ] = [row [0 ] for row in standings ]
122+ pairings = swissPairings ()
123+ if len (pairings ) != 4 :
124+ raise ValueError (
125+ "For eight players, swissPairings should return 4 pairs. Got {pairs}" .format (pairs = len (pairings )))
113126 reportMatch (id1 , id2 )
114127 reportMatch (id3 , id4 )
128+ reportMatch (id5 , id6 )
129+ reportMatch (id7 , id8 )
115130 pairings = swissPairings ()
116- if len (pairings ) != 2 :
131+ if len (pairings ) != 4 :
117132 raise ValueError (
118- "For four players, swissPairings should return two pairs." )
119- [(pid1 , pname1 , pid2 , pname2 ), (pid3 , pname3 , pid4 , pname4 )] = pairings
120- correct_pairs = set ([frozenset ([id1 , id3 ]), frozenset ([id2 , id4 ])])
121- actual_pairs = set ([frozenset ([pid1 , pid2 ]), frozenset ([pid3 , pid4 ])])
122- if correct_pairs != actual_pairs :
123- raise ValueError (
124- "After one match, players with one win should be paired." )
125- print "8. After one match, players with one win are paired."
133+ "For eight players, swissPairings should return 4 pairs. Got {pairs}" .format (pairs = len (pairings )))
134+ [(pid1 , pname1 , pid2 , pname2 ), (pid3 , pname3 , pid4 , pname4 ), (pid5 , pname5 , pid6 , pname6 ), (pid7 , pname7 , pid8 , pname8 )] = pairings
135+ possible_pairs = set ([frozenset ([id1 , id3 ]), frozenset ([id1 , id5 ]),
136+ frozenset ([id1 , id7 ]), frozenset ([id3 , id5 ]),
137+ frozenset ([id3 , id7 ]), frozenset ([id5 , id7 ]),
138+ frozenset ([id2 , id4 ]), frozenset ([id2 , id6 ]),
139+ frozenset ([id2 , id8 ]), frozenset ([id4 , id6 ]),
140+ frozenset ([id4 , id8 ]), frozenset ([id6 , id8 ])
141+ ])
142+ actual_pairs = set ([frozenset ([pid1 , pid2 ]), frozenset ([pid3 , pid4 ]), frozenset ([pid5 , pid6 ]), frozenset ([pid7 , pid8 ])])
143+ for pair in actual_pairs :
144+ if pair not in possible_pairs :
145+ raise ValueError (
146+ "After one match, players with one win should be paired." )
147+ print "10. After one match, players with one win are properly paired."
126148
127149
128150if __name__ == '__main__' :
129- testDeleteMatches ()
130- testDelete ()
131151 testCount ()
132- testRegister ()
133- testRegisterCountDelete ()
134152 testStandingsBeforeMatches ()
135153 testReportMatches ()
136154 testPairings ()
137155 print "Success! All tests pass!"
138-
139-
0 commit comments