-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_generate_cmds.py
More file actions
314 lines (255 loc) · 11.8 KB
/
test_generate_cmds.py
File metadata and controls
314 lines (255 loc) · 11.8 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import unittest
from unittest.mock import patch, mock_open
import json
from git_py_stats import generate_cmds
class TestGenerateCmds(unittest.TestCase):
"""
Unit test class for testing the functionality of the generate_cmds module
"""
def setUp(self):
# Mock configuration for testing
self.mock_config = {
"since": "--since=2020-01-01",
"until": "--until=2024-12-31",
"merges": "--no-merges",
"log_options": "",
"pathspec": "--",
"limit": 10, # Ensure limit is an integer
"menu_theme": "",
}
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_detailed_git_stats(self, mock_print, mock_run_git_command):
"""
Test detailed_git_stats function with sample git output.
"""
# Mock git command output
mock_output = (
"abc123\tJohn Doe\tjohn@example.com\t1609459200\n"
"10\t2\tsomefile.py\n"
"def456\tJane Smith\tjane@example.com\t1609545600\n"
"5\t3\tanotherfile.py\n"
)
mock_run_git_command.return_value = mock_output
generate_cmds.detailed_git_stats(self.mock_config)
# Check that print was called
self.assertTrue(mock_print.called)
# You can add more detailed assertions based on the expected outputs
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_detailed_git_stats_no_output(self, mock_print, mock_run_git_command):
"""
Test detailed_git_stats when git command returns no output.
"""
mock_run_git_command.return_value = ""
generate_cmds.detailed_git_stats(self.mock_config)
# Should not raise an error and print nothing
self.assertFalse(mock_print.called)
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_changelogs(self, mock_print, mock_run_git_command):
"""
Test changelogs function with sample git output.
"""
mock_run_git_command.side_effect = [
"2021-01-02\n2021-01-01", # Dates output
"* Commit message 1 (John Doe)\n* Commit message 2 (Jane Smith)", # First date commits
"* Commit message 3 (John Doe)", # Second date commits
]
generate_cmds.changelogs(self.mock_config)
self.assertTrue(mock_print.called)
# You can add more detailed assertions based on the expected outputs
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_changelogs_no_commits(self, mock_print, mock_run_git_command):
"""
Test changelogs when git command returns no commits.
"""
mock_run_git_command.return_value = ""
generate_cmds.changelogs(self.mock_config)
# Verify that "No commits found." was printed
mock_print.assert_any_call("No commits found.")
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_changelogs_with_author(self, mock_print, mock_run_git_command):
"""
Test changelogs function with an author specified.
"""
mock_run_git_command.side_effect = [
"2021-01-01", # Dates output
"* Commit message 1 (John Doe)", # Commits for date
]
generate_cmds.changelogs(self.mock_config, author="John Doe")
# Verify that the author option was included in the command
called_cmd = mock_run_git_command.call_args_list[0][0][0]
self.assertIn("--author=John Doe", called_cmd)
self.assertTrue(mock_print.called)
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_commits_calendar_by_author(self, mock_print, mock_run_git_command):
"""
Test commits_calendar_by_author function with an author specified.
"""
# Mock git command outputs
mock_run_git_command.side_effect = [
"", # git diff output (no changes)
"John Doe", # git config user.name
"", # git log output (no commits)
]
generate_cmds.commits_calendar_by_author(self.mock_config, author="John Doe")
# Verify that the author option was included in the command
called_cmd = mock_run_git_command.call_args_list[0][0][0]
self.assertIn("--author=John Doe", called_cmd)
self.assertTrue(mock_print.called)
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_my_daily_status(self, mock_print, mock_run_git_command):
"""
Test my_daily_status function with sample git output.
"""
# Mock git command outputs
mock_run_git_command.side_effect = [
"1 file changed, 10 insertions(+), 2 deletions(-)", # git diff output
"John Doe", # git config user.name
"abc123\ndef456", # git log output with commit hashes
]
generate_cmds.my_daily_status(self.mock_config)
calls = [call.args[0] for call in mock_print.call_args_list]
self.assertIn("My daily status:", calls)
self.assertIn("\t1 file changed", calls)
self.assertIn("\t10 insertions(+)", calls)
self.assertIn("\t2 deletions(-)", calls)
self.assertIn("\t2 commits", calls)
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_my_daily_status_no_changes(self, mock_print, mock_run_git_command):
"""
Test my_daily_status when there are no changes or commits.
"""
# Mock git command outputs
mock_run_git_command.side_effect = [
"", # git diff output (no changes)
"John Doe", # git config user.name
"", # git log output (no commits)
]
generate_cmds.my_daily_status(self.mock_config)
calls = [call.args[0] for call in mock_print.call_args_list]
self.assertIn("My daily status:", calls)
self.assertIn("\tNo changes in the last day.", calls)
self.assertIn("\t0 commits", calls)
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.input", return_value="")
@patch("builtins.print")
def test_output_daily_stats_csv(self, mock_print, mock_input, mock_run_git_command):
"""
Test output_daily_stats_csv function with sample git output.
"""
mock_run_git_command.return_value = "2021-01-01\n2021-01-01\n2021-01-02\n2021-01-03\n"
# Mock open to prevent actual file creation
with patch("builtins.open", mock_open()) as mocked_file:
generate_cmds.output_daily_stats_csv(self.mock_config)
# Check that file was written
mocked_file.assert_called_with("daily_stats.csv", "w", newline="")
# Check that print was called
self.assertTrue(mock_print.called)
mock_print.assert_any_call("Daily stats saved to daily_stats.csv")
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.input", return_value="")
@patch("builtins.print")
def test_output_daily_stats_csv_no_data(self, mock_print, mock_input, mock_run_git_command):
"""
Test output_daily_stats_csv when git command returns no data.
"""
mock_run_git_command.return_value = ""
generate_cmds.output_daily_stats_csv(self.mock_config)
mock_print.assert_called_once_with("No data available.")
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_save_git_log_output_json(self, mock_print, mock_run_git_command):
"""
Test save_git_log_output_json function with sample git output.
"""
mock_run_git_command.return_value = (
"abc123|John Doe|2021-01-01 12:00:00 +0000|Commit message 1\n"
"def456|Jane Smith|2021-01-02 13:00:00 +0000|Commit message 2\n"
)
# Mock open to prevent actual file creation
with patch("builtins.open", mock_open()) as mocked_file:
generate_cmds.save_git_log_output_json(self.mock_config)
# Check that file was written
mocked_file.assert_called_with("git_log.json", "w")
# Verify that json.dump was called
handle = mocked_file()
handle.write.assert_called()
written_data = "".join(call.args[0] for call in handle.write.call_args_list)
data = json.loads(written_data)
self.assertEqual(len(data), 2)
self.assertEqual(data[0]["hash"], "abc123")
# Check that print was called
self.assertTrue(mock_print.called)
mock_print.assert_any_call("Git log saved to git_log.json")
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_save_git_log_output_json_no_data(self, mock_print, mock_run_git_command):
"""
Test save_git_log_output_json when git command returns no data.
"""
mock_run_git_command.return_value = ""
generate_cmds.save_git_log_output_json(self.mock_config)
mock_print.assert_called_once_with("No log data available.")
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_detailed_git_stats_handles_invalid_lines(self, mock_print, mock_run_git_command):
"""
Test detailed_git_stats with invalid lines in git output.
"""
mock_output = (
"invalid line without tabs\n"
"abc123\tJohn Doe\tjohn@example.com\t1609459200\n"
"invalid\tdata\n"
"5\t3\tfile.py\n"
)
mock_run_git_command.return_value = mock_output
generate_cmds.detailed_git_stats(self.mock_config)
self.assertTrue(mock_print.called)
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_my_daily_status_user_unknown(self, mock_print, mock_run_git_command):
"""
Test my_daily_status when git user.name is not set.
"""
# Mock git command outputs
mock_run_git_command.side_effect = [
"", # git diff output (no changes)
None, # git config user.name returns None
"", # git log output (no commits)
]
generate_cmds.my_daily_status(self.mock_config)
# Check that '--author=unknown' is used in the git log command
log_cmd = mock_run_git_command.call_args_list[2][0][0]
self.assertIn("--author=unknown", log_cmd)
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.input", return_value="")
@patch("builtins.print")
def test_output_daily_stats_csv_io_error(self, mock_print, mock_input, mock_run_git_command):
"""
Test output_daily_stats_csv when an IOError occurs during file writing.
"""
mock_run_git_command.return_value = "2021-01-01\n2021-01-02"
with patch("builtins.open", side_effect=IOError("Disk full")):
generate_cmds.output_daily_stats_csv(self.mock_config)
mock_print.assert_any_call("Failed to write to daily_stats.csv: Disk full")
@patch("git_py_stats.generate_cmds.run_git_command")
@patch("builtins.print")
def test_save_git_log_output_json_io_error(self, mock_print, mock_run_git_command):
"""
Test save_git_log_output_json when an IOError occurs during file writing.
"""
mock_run_git_command.return_value = (
"abc123|John Doe|2021-01-01 12:00:00 +0000|Commit message 1\n"
)
with patch("builtins.open", side_effect=IOError("Disk full")):
generate_cmds.save_git_log_output_json(self.mock_config)
mock_print.assert_any_call("Failed to write to git_log.json: Disk full")
if __name__ == "__main__":
unittest.main()