Skip to content

Commit 9eae73f

Browse files
committed
test: add test for main package
1 parent 34d9664 commit 9eae73f

File tree

4 files changed

+414
-0
lines changed

4 files changed

+414
-0
lines changed

tests/main/test_game_controller.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import unittest
2+
from io import StringIO
3+
from unittest.mock import call, create_autospec, patch
4+
5+
from mastermind.game.game import Game
6+
from mastermind.main.game_controller import GameController
7+
from mastermind.storage.user_data import UserDataManager
8+
9+
10+
class TestGameController(unittest.TestCase):
11+
"""Unit tests for the GameController class"""
12+
13+
@patch("builtins.input", side_effect=["6", "4", "10", "q"])
14+
@patch("mastermind.main.game_controller.GameHistoryManager.save_game")
15+
def test_start_new_game_classic(self, mock_save_game, mock_input):
16+
"""Test starting a new Classic game"""
17+
with patch("sys.stdout", new=StringIO()):
18+
GameController.start_new_game("HvAI")
19+
20+
mock_input.assert_has_calls(
21+
[
22+
call("\nEnter the number of colors (2-10): "),
23+
call("\nEnter the number of dots (2-10): "),
24+
call("\nEnter the maximum number of attempts: "),
25+
]
26+
)
27+
mock_save_game.assert_called_once()
28+
29+
@patch("builtins.input", side_effect=["8", "5", "15", "q"])
30+
@patch("mastermind.main.game_controller.GameHistoryManager.save_game")
31+
def test_start_new_game_custom(self, mock_save_game, mock_input):
32+
"""Test starting a new Custom game"""
33+
with patch("sys.stdout", new=StringIO()):
34+
GameController.start_new_game("HvAI")
35+
36+
mock_input.assert_has_calls(
37+
[
38+
call("\nEnter the number of colors (2-10): "),
39+
call("\nEnter the number of dots (2-10): "),
40+
call("\nEnter the maximum number of attempts: "),
41+
]
42+
)
43+
mock_save_game.assert_called_once()
44+
45+
def test_resume_game_discard(self):
46+
"""Test resuming a game and discarding it"""
47+
game_mock = create_autospec(Game, instance=True)
48+
game_mock.resume_game.return_value = "d"
49+
mock_user_data_manager = create_autospec(UserDataManager, instance=True)
50+
mock_user_data_manager.saved_games = [
51+
{
52+
"game": game_mock,
53+
"game_mode": "HvH",
54+
"number_of_dots": 4,
55+
"number_of_colors": 6,
56+
"amount_attempted": 8,
57+
"amount_allowed": 10,
58+
"win_status": None,
59+
"guesses": ["1234", "4561", "2312"],
60+
"feedback": [(4, 0), (3, 1), (2, 2)],
61+
}
62+
]
63+
64+
with patch(
65+
"mastermind.main.game_controller.userdata",
66+
new=mock_user_data_manager,
67+
):
68+
GameController.resume_game(0)
69+
70+
self.assertEqual(mock_user_data_manager.saved_games, [])
71+
72+
@patch("builtins.input", side_effect=["q"])
73+
def test_resume_game_update_saved(self, mock_input):
74+
"""Test resuming a game and updating the saved game"""
75+
game = Game(6, 4, 10, "HvAI")
76+
game._state.game_started = True
77+
game._player_logic.initialize_players()
78+
79+
mock_user_data_manager = create_autospec(UserDataManager, instance=True)
80+
mock_user_data_manager.saved_games = [
81+
{
82+
"game": game,
83+
"game_mode": "HvAI",
84+
"number_of_dots": 4,
85+
"number_of_colors": 6,
86+
"amount_attempted": 3,
87+
"amount_allowed": 10,
88+
"win_status": None,
89+
"guesses": ["1234", "4561", "2312"],
90+
"feedback": [(4, 0), (3, 1), (2, 2)],
91+
}
92+
]
93+
94+
with patch(
95+
"mastermind.main.game_controller.userdata", new=mock_user_data_manager
96+
):
97+
with patch("sys.stdout", new=StringIO()):
98+
GameController.resume_game(0)
99+
100+
self.assertEqual(len(mock_user_data_manager.saved_games), 1)
101+
self.assertIsNone(mock_user_data_manager.saved_games[0]["win_status"])
102+
103+
104+
if __name__ == "__main__":
105+
unittest.main()

tests/main/test_game_history.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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()

tests/main/test_game_storage.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import unittest
2+
from unittest.mock import create_autospec, patch
3+
4+
from mastermind.main.game_storage import (
5+
list_continuable_games,
6+
list_continuable_games_index,
7+
retrieve_stored_games,
8+
)
9+
from mastermind.storage.user_data import UserDataManager
10+
11+
12+
class TestMastermindStorage(unittest.TestCase):
13+
"""Unit tests for the mastermind.storage module"""
14+
15+
def setUp(self):
16+
self.sample_games = [
17+
{"game_id": 1, "win_status": None},
18+
{"game_id": 2, "win_status": True},
19+
{"game_id": 3, "win_status": False},
20+
{"game_id": 4, "win_status": None},
21+
] # Note: For testing only! Actual data doesn't looks like this!
22+
23+
def test_retrieve_stored_games(self):
24+
"""Test the retrieve_stored_games function"""
25+
mock_user_data_manager = create_autospec(UserDataManager, instance=True)
26+
mock_user_data_manager.saved_games = self.sample_games
27+
28+
with patch("mastermind.main.game_storage.userdata", new=mock_user_data_manager):
29+
games = retrieve_stored_games()
30+
self.assertEqual(games, self.sample_games)
31+
32+
def test_retrieve_empty_games(self):
33+
"""Test the retrieve_stored_games function"""
34+
mock_user_data_manager = create_autospec(UserDataManager, instance=True)
35+
mock_user_data_manager.saved_games = []
36+
37+
with patch("mastermind.main.game_storage.userdata", new=mock_user_data_manager):
38+
games = retrieve_stored_games()
39+
self.assertEqual(games, [])
40+
41+
def test_list_continuable_games_index(self):
42+
"""Test the list_continuable_games_index function"""
43+
continuable_indexes = list_continuable_games_index(self.sample_games)
44+
self.assertEqual(continuable_indexes, [0, 3])
45+
46+
continuable_indexes = list_continuable_games_index([])
47+
self.assertEqual(continuable_indexes, [])
48+
49+
def test_list_continuable_games(self):
50+
"""Test the list_continuable_games function"""
51+
continuable_games = list_continuable_games(self.sample_games)
52+
self.assertEqual(
53+
continuable_games, [self.sample_games[0], self.sample_games[3]]
54+
)
55+
56+
continuable_games = list_continuable_games([])
57+
self.assertEqual(continuable_games, [])
58+
59+
60+
if __name__ == "__main__":
61+
unittest.main()

0 commit comments

Comments
 (0)