class TestPlayer(unittest.TestCase): """ Tests the functionality of a Player. """ def setUp(self): """ Creates a new instance of a Player. """ self.player = Player('LizTest') def tearDown(self): """ Sets the original user data. """ self.player.data = { 'username': '******', 'games_played': 10, 'games_won': 5, 'longest_winning_streak': 3, 'longest_losing_streak': 2, 'current_streak': 1, 'shortest_winning_time': '00:01:03', 'longest_winning_time': '00:05:15', 'average_winning_time': '00:02:30', 'fewest_winning_moves': 100, 'most_winning_moves': 150, 'average_winning_moves': 125.3, 'highest_score': 1025, 'lowest_score': 350, 'average_score': 757.4 } self.player.db.update_all('LizTest', self.player.data) self.player.current_score = 0 self.player.current_moves = 0 self.player.current_time = 0 def test_init(self): """ """ self.assertEqual(self.player.username, 'LizTest') self.assertEqual(self.player.current_score, 0) self.assertEqual(self.player.current_moves, 0) self.assertEqual(self.player.current_time, 0) def test_usernameExists(self): """ Tests the username_exists function. """ self.assertTrue(self.player.username_exists('LizTest')) def test_getStatistics(self): """ Tests that a Player can get his or her statistics in a dictionary. """ keys = [ 'username', 'average_winning_time', 'most_winning_moves', 'average_score', 'games_won', 'highest_score', 'shortest_winning_time', 'average_winning_moves', 'fewest_winning_moves', 'longest_winning_streak', 'current_streak', 'longest_winning_time', 'lowest_score', 'games_played', 'id', 'longest_losing_streak' ] keys.sort() data = self.player.get_statistics() got_keys = data.keys() got_keys.sort() self.assertEqual(got_keys, keys) def test_changeUsername(self): """ Tests that a Player can change his or her username, and that the resulting change will maintain his or her existing statistics in the database. """ self.player.change_username('Liztest') self.assertEqual(self.player.username, 'Liz_Test') self.assertFalse(self.player.username_exists('LizTest')) self.assertTrue(self.player.username_exists('Liz_Test')) self.player.change_username('LizTest') self.assertEqual(self.player.username, 'LizTest') self.assertTrue(self.player.username_exists('LizTest')) self.assertFalse(self.player.username_exists('Liz_Test')) def test_winPercentage(self): """ Tests that a player calculates his or her win percentage correctly. """ percent = self.player.calculate_win_percentage() self.assertEqual(percent, 50.0) def test_updateCurrentScore(self): """ Tests that moving certain piles and cards updates scores correctly. """ score = self.player.current_score self.player.update_current_score(10) self.assertEqual(self.player.current_score, score + 10) def test_updateStreaks(self): """ """ longest_winning_streak = self.player.data["longest_winning_streak"] longest_losing_streak = self.player.data["longest_losing_streak"] current_streak = self.player.data["current_streak"] def test_updateScoresMiddle(self): """ Tests that when a game has ended the user's best/worse/average scores are updated in the database. """ old_highest = self.player.data["highest_score"] old_lowest = self.player.data["lowest_score"] self.player.data["games_played"] += 1 self.player.current_score = 500 self.player.update_scores(self.player.current_score) self.assertEqual(self.player.data["highest_score"], old_highest) self.assertEqual(self.player.data["lowest_score"], old_lowest) self.assertEqual(self.player.data["average_score"], 734.0) def test_updateScoresHighest(self): old_lowest = self.player.data["lowest_score"] self.player.data["games_played"] += 1 self.player.current_score = 1226 self.player.update_scores(self.player.current_score) self.assertEqual(self.player.data["highest_score"], self.player.current_score) self.assertEqual(self.player.data["lowest_score"], old_lowest) self.assertEqual(self.player.data["average_score"], 800.0) def test_updateScoresLowest(self): old_highest = self.player.data["highest_score"] self.player.data["games_played"] += 1 self.player.current_score = 126 self.player.update_scores(self.player.current_score) self.assertEqual(self.player.data["highest_score"], old_highest) self.assertEqual(self.player.data["lowest_score"], self.player.current_score) self.assertEqual(self.player.data["average_score"], 700.0) def test_updateTimes(self): """ Tests that when a game has ended the user's best/worse/average times are updated in the database. """ time = self.player.current_time def test_updateMoves(self): """ Tests that when a game has ended the user's best/worse/average moves are updated in the database. """ moves = self.player.current_moves self.player.update_moves(moves, win=1) self.assertEqual(self.player.data["fewest_winning_moves"], moves) def test_endGame(self): """ Tests that when a game ends, the user's data is updated and that this update is reflected in the database. """ games_played = self.player.data["games_played"] self.player.end_game(10, 200, 90, 0) self.assertEqual(self.player.data["games_played"], games_played + 1) def test_wonGame(self): """ Tests that when a player wins a game, the data is updated and reflected in the database. """ pass