-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPlayer.py
More file actions
147 lines (130 loc) · 5 KB
/
TestPlayer.py
File metadata and controls
147 lines (130 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import unittest
from unittest.mock import mock_open, patch
from Player import Player
class TestPlayer(unittest.TestCase):
"""
Unit tests for the Player class.
"""
def setUp(self):
"""
Set up the test environment by creating a Player instance.
"""
self.player = Player("test_user", "test_password")
def test_initialization(self):
"""
Test the initialization of a Player object.
"""
self.assertEqual(self.player.name, "test_user")
self.assertEqual(self.player.password, "test_password")
self.assertEqual(self.player.level, 0)
self.assertEqual(self.player.addition, 0)
self.assertEqual(self.player.subtraction, 0)
self.assertEqual(self.player.multiplication, 0)
self.assertEqual(self.player.division, 0)
self.assertEqual(self.player.bosses, 0)
@patch('csv.reader')
def test_load_player(self, mock_csv_reader):
"""
Test loading a player's information from a CSV file.
"""
mock_csv_reader.return_value = iter([["test_user", "test_password", "5", "4", "3", "2", "1"]])
self.player.load_player()
self.assertEqual(self.player.addition, "5")
self.assertEqual(self.player.subtraction, "4")
self.assertEqual(self.player.multiplication, "3")
self.assertEqual(self.player.division, "2")
self.assertEqual(self.player.bosses, "1")
@patch('csv.writer')
@patch('csv.reader')
def test_save_info(self, mock_csv_reader, mock_csv_writer):
"""
Test saving a player's information to a CSV file.
"""
mock_file = mock_open()
mock_csv_reader.return_value = iter([["test_user", "test_password", "1", "2", "3", "4", "5"]])
with patch("builtins.open", mock_file):
self.player.save_info()
# Check if writerows was called with the expected data
mock_csv_writer.return_value.writerows.assert_called_with([["test_user", "test_password", 0, 0, 0, 0, 0]])
def test_update_bosses(self):
"""
Test updating a player's boss score and saving the information.
"""
new_score = 10
with patch.object(Player, 'save_info') as mock_save_info:
self.player.update_bosses(new_score)
self.assertEqual(self.player.bosses, new_score)
mock_save_info.assert_called_once()
def test_update_add(self):
"""
Test updating a player's addition score and saving the information.
"""
new_score = 10
with patch.object(Player, 'save_info') as mock_save_info:
self.player.update_add(new_score)
self.assertEqual(self.player.addition, new_score)
mock_save_info.assert_called_once()
def test_update_mul(self):
"""
Test updating a player's multiplication score and saving the information.
"""
new_score = 10
with patch.object(Player, 'save_info') as mock_save_info:
self.player.update_mul(new_score)
self.assertEqual(self.player.multiplication, new_score)
mock_save_info.assert_called_once()
def test_update_div(self):
"""
Test updating a player's division score and saving the information.
"""
new_score = 10
with patch.object(Player, 'save_info') as mock_save_info:
self.player.update_div(new_score)
self.assertEqual(self.player.division, new_score)
mock_save_info.assert_called_once()
def test_update_sub(self):
"""
Test updating a player's subtraction score and saving the information.
"""
new_score = 10
with patch.object(Player, 'save_info') as mock_save_info:
self.player.update_sub(new_score)
self.assertEqual(self.player.subtraction, new_score)
mock_save_info.assert_called_once()
def test_get_name(self):
"""
Test retrieving a player's name.
"""
self.assertEqual(self.player.get_name(), "test_user")
def test_get_bosses(self):
"""
Test retrieving a player's boss score.
"""
self.player.bosses = 5
self.assertEqual(self.player.get_bosses(), 5)
def test_get_add(self):
"""
Test retrieving a player's addition score.
"""
self.player.addition = 5
self.assertEqual(self.player.get_add(), 5)
def test_get_mul(self):
"""
Test retrieving a player's multiplication score.
"""
self.player.multiplication = 5
self.assertEqual(self.player.get_mul(), 5)
def test_get_div(self):
"""
Test retrieving a player's division score.
"""
self.player.division = 5
self.assertEqual(self.player.get_div(), 5)
def test_get_sub(self):
"""
Test retrieving a player's subtraction score.
"""
self.player.subtraction = 5
self.assertEqual(self.player.get_sub(), 5)
if __name__ == '__main__':
unittest.main()