|
| 1 | +import unittest |
| 2 | +from unittest.mock import create_autospec, patch |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | +from mastermind.game.game import Game |
| 7 | +from mastermind.main.game_history import GameHistoryManager, game_list_to_pandas |
| 8 | +from mastermind.storage.user_data import UserDataManager |
| 9 | + |
| 10 | + |
| 11 | +class TestGameHistoryManager(unittest.TestCase): |
| 12 | + """Unit tests for the GameHistoryManager class""" |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + self.sample_games = [ |
| 16 | + { |
| 17 | + "game_mode": "HvH", |
| 18 | + "number_of_dots": 4, |
| 19 | + "number_of_colors": 6, |
| 20 | + "amount_attempted": 8, |
| 21 | + "amount_allowed": 10, |
| 22 | + "win_status": True, |
| 23 | + "guesses": ["1234", "4561", "2312"], |
| 24 | + "feedback": [(4, 0), (3, 1), (2, 2)], |
| 25 | + }, |
| 26 | + { |
| 27 | + "game_mode": "HvAI", |
| 28 | + "number_of_dots": 5, |
| 29 | + "number_of_colors": 8, |
| 30 | + "amount_attempted": 12, |
| 31 | + "amount_allowed": 15, |
| 32 | + "win_status": False, |
| 33 | + "guesses": ["12345", "45612", "34567"], |
| 34 | + "feedback": [(3, 2), (2, 3), (0, 5)], |
| 35 | + }, |
| 36 | + { |
| 37 | + "game_mode": "AIvH", |
| 38 | + "number_of_dots": 4, |
| 39 | + "number_of_colors": 6, |
| 40 | + "amount_attempted": 5, |
| 41 | + "amount_allowed": 10, |
| 42 | + "win_status": None, |
| 43 | + "guesses": ["1234", "4561"], |
| 44 | + "feedback": [(4, 0), (3, 1)], |
| 45 | + }, |
| 46 | + ] |
| 47 | + |
| 48 | + def test_save_game(self): |
| 49 | + """Test the save_game method""" |
| 50 | + mock_user_data_manager = create_autospec(UserDataManager, instance=True) |
| 51 | + mock_user_data_manager.saved_games = [] |
| 52 | + |
| 53 | + with patch("mastermind.main.game_history.userdata", new=mock_user_data_manager): |
| 54 | + self._set_up_game_and_test_saved(mock_user_data_manager) |
| 55 | + |
| 56 | + def test_save_game_with_empty_list(self): |
| 57 | + """Test the save_game method when the saved_games list is empty""" |
| 58 | + mock_user_data_manager = create_autospec(UserDataManager, instance=True) |
| 59 | + mock_user_data_manager.saved_games = None |
| 60 | + |
| 61 | + with patch("mastermind.main.game_history.userdata", new=mock_user_data_manager): |
| 62 | + self._set_up_game_and_test_saved(mock_user_data_manager) |
| 63 | + |
| 64 | + def _set_up_game_and_test_saved(self, mock_user_data_manager): |
| 65 | + game = Game(4, 6, 10, "HvH") |
| 66 | + game_metadata = GameHistoryManager.generate_meta_data(game) |
| 67 | + GameHistoryManager.save_game(game) |
| 68 | + self.assertIn(game_metadata, mock_user_data_manager.saved_games) |
| 69 | + |
| 70 | + def test_retrieve_game_history_with_no_games(self): |
| 71 | + """Test the retrieve_game_history method when there are no games""" |
| 72 | + mock_user_data_manager = create_autospec(UserDataManager, instance=True) |
| 73 | + mock_user_data_manager.saved_games = None |
| 74 | + |
| 75 | + with patch("mastermind.main.game_storage.userdata", new=mock_user_data_manager): |
| 76 | + game_history = GameHistoryManager.retrieve_game_history() |
| 77 | + self.assertIsNone(game_history) |
| 78 | + |
| 79 | + def test_retrieve_continuable_games_with_no_games(self): |
| 80 | + """Test the retrieve_continuable_games method when there are no games""" |
| 81 | + mock_user_data_manager = create_autospec(UserDataManager, instance=True) |
| 82 | + mock_user_data_manager.saved_games = None |
| 83 | + |
| 84 | + with patch("mastermind.main.game_storage.userdata", new=mock_user_data_manager): |
| 85 | + continuable_games = GameHistoryManager.retrieve_continuable_games() |
| 86 | + self.assertIsNone(continuable_games) |
| 87 | + |
| 88 | + @patch("mastermind.main.game_storage.retrieve_stored_games", return_value=None) |
| 89 | + def test_game_list_to_pandas_with_no_games(self, mock_retrieve_stored_games): |
| 90 | + """Test the game_list_to_pandas function when there are no games""" |
| 91 | + dataframe = game_list_to_pandas([]) |
| 92 | + self.assertIsNone(dataframe) |
| 93 | + |
| 94 | + def test_game_list_to_pandas(self): |
| 95 | + """Test the game_list_to_pandas function""" |
| 96 | + mock_user_data_manager = create_autospec(UserDataManager, instance=True) |
| 97 | + mock_user_data_manager.saved_games = self.sample_games |
| 98 | + |
| 99 | + with patch("mastermind.main.game_history.userdata", new=mock_user_data_manager): |
| 100 | + dataframe = game_list_to_pandas(self.sample_games) |
| 101 | + self.assertIsInstance(dataframe, pd.DataFrame) |
| 102 | + self.assertEqual(len(dataframe), 3) |
| 103 | + self.assertListEqual( |
| 104 | + list(dataframe.columns), ["Mode", "Dimension", "Attempts"] |
| 105 | + ) |
| 106 | + self.assertListEqual(list(dataframe["Mode"]), ["HvH", "HvAI", "AIvH"]) |
| 107 | + self.assertListEqual(list(dataframe["Dimension"]), ["6x4", "8x5", "6x4"]) |
| 108 | + self.assertListEqual( |
| 109 | + list(dataframe["Attempts"]), ["W 8/10", "L 12/15", " 5/10"] |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + unittest.main() |
0 commit comments