5
5
6
6
class TestListCmds (unittest .TestCase ):
7
7
"""
8
- Unit test class for testing list_cmds.
8
+ Unit test class for testing list_cmds functions .
9
9
"""
10
10
11
11
def setUp (self ):
@@ -24,26 +24,231 @@ def setUp(self):
24
24
@patch ("git_py_stats.list_cmds.print" )
25
25
def test_branch_tree (self , mock_print , mock_run_git_command ) -> None :
26
26
"""
27
- Test case for the branch_tree function in the list_cmds module.
28
-
29
- Checks if `branch_tree` executes without errors and returns `None`.
30
- The print function is mocked to prevent actual output during testing.
31
-
32
- The test verifies that the function runs without raising any exceptions and
33
- calls the print function at least once, indicating that some output was generated.
27
+ Test case for the branch_tree function.
34
28
"""
35
- # Mock git command output to provide a sample branch tree
36
29
mock_run_git_command .return_value = (
37
30
"* 12345 Commit message\n "
38
31
"| * 67890 Another commit message\n "
39
32
"| * abcde Yet another commit message\n "
40
33
)
41
34
42
- # Call function with mock configuration
43
35
list_cmds .branch_tree (self .mock_config )
44
36
45
- # Assert that print was called at least once
46
37
mock_print .assert_called ()
38
+ mock_run_git_command .assert_called_once ()
39
+
40
+ @patch ("git_py_stats.list_cmds.run_git_command" )
41
+ @patch ("git_py_stats.list_cmds.print" )
42
+ def test_branch_tree_no_data (self , mock_print , mock_run_git_command ) -> None :
43
+ """
44
+ Test case for branch_tree with no data.
45
+ """
46
+ mock_run_git_command .return_value = ""
47
+ list_cmds .branch_tree (self .mock_config )
48
+
49
+ mock_print .assert_called_with ("No data available." )
50
+
51
+ @patch ("git_py_stats.list_cmds.run_git_command" )
52
+ @patch ("git_py_stats.list_cmds.print" )
53
+ def test_branches_by_date (self , mock_print , mock_run_git_command ) -> None :
54
+ """
55
+ Test case for branches_by_date function.
56
+ """
57
+ mock_run_git_command .return_value = (
58
+ "[2021-01-01] Author1 branch1\n " "[2021-01-02] Author2 branch2\n "
59
+ )
60
+ list_cmds .branches_by_date ()
61
+
62
+ mock_print .assert_called ()
63
+ mock_run_git_command .assert_called_once ()
64
+
65
+ @patch ("git_py_stats.list_cmds.run_git_command" )
66
+ @patch ("git_py_stats.list_cmds.print" )
67
+ def test_branches_by_date_no_data (self , mock_print , mock_run_git_command ) -> None :
68
+ """
69
+ Test case for branches_by_date with no data.
70
+ """
71
+ mock_run_git_command .return_value = ""
72
+ list_cmds .branches_by_date ()
73
+
74
+ mock_print .assert_called_with ("No commits found." )
75
+
76
+ @patch ("git_py_stats.list_cmds.run_git_command" )
77
+ @patch ("git_py_stats.list_cmds.print" )
78
+ def test_contributors (self , mock_print , mock_run_git_command ) -> None :
79
+ """
80
+ Test case for the contributors function.
81
+ """
82
+ mock_run_git_command .return_value = "Author1\n Author2\n Author3\n "
83
+ list_cmds .contributors (self .mock_config )
84
+
85
+ mock_print .assert_called ()
86
+ mock_run_git_command .assert_called_once ()
87
+
88
+ @patch ("git_py_stats.list_cmds.run_git_command" )
89
+ @patch ("git_py_stats.list_cmds.print" )
90
+ def test_contributors_no_data (self , mock_print , mock_run_git_command ) -> None :
91
+ """
92
+ Test case for contributors with no data.
93
+ """
94
+ mock_run_git_command .return_value = ""
95
+ list_cmds .contributors (self .mock_config )
96
+
97
+ mock_print .assert_called_with ("No contributors found." )
98
+
99
+ @patch ("git_py_stats.list_cmds.run_git_command" )
100
+ @patch ("git_py_stats.list_cmds.print" )
101
+ def test_new_contributors (self , mock_print , mock_run_git_command ) -> None :
102
+ """
103
+ Test case for new_contributors function.
104
+ """
105
+ mock_run_git_command .
return_value = "[email protected] |1577836800\n "
106
+ list_cmds .new_contributors (self .mock_config , "2020-01-01" )
107
+
108
+ mock_print .assert_called ()
109
+ mock_run_git_command .assert_called_once ()
110
+
111
+ @patch ("git_py_stats.list_cmds.run_git_command" )
112
+ @patch ("git_py_stats.list_cmds.print" )
113
+ def test_new_contributors_invalid_date (self , mock_print , mock_run_git_command ) -> None :
114
+ """
115
+ Test case for new_contributors with invalid date.
116
+ """
117
+ list_cmds .new_contributors (self .mock_config , "invalid-date" )
118
+
119
+ mock_print .assert_called_with ("Invalid date format. Please use YYYY-MM-DD." )
120
+ mock_run_git_command .assert_not_called ()
121
+
122
+ @patch ("git_py_stats.list_cmds.run_git_command" )
123
+ @patch ("git_py_stats.list_cmds.print" )
124
+ def test_git_commits_per_author (self , mock_print , mock_run_git_command ) -> None :
125
+ """
126
+ Test case for git_commits_per_author function.
127
+ """
128
+ mock_run_git_command .
return_value = "Author:Author1 <[email protected] >\n "
129
+ list_cmds .git_commits_per_author (self .mock_config )
130
+
131
+ mock_print .assert_called ()
132
+ mock_run_git_command .assert_called_once ()
133
+
134
+ @patch ("git_py_stats.list_cmds.run_git_command" )
135
+ @patch ("git_py_stats.list_cmds.print" )
136
+ def test_git_commits_per_author_no_data (self , mock_print , mock_run_git_command ) -> None :
137
+ """
138
+ Test case for git_commits_per_author with no data.
139
+ """
140
+ mock_run_git_command .return_value = ""
141
+ list_cmds .git_commits_per_author (self .mock_config )
142
+
143
+ mock_print .assert_called_with ("No commits found." )
144
+
145
+ @patch ("git_py_stats.list_cmds.run_git_command" )
146
+ @patch ("git_py_stats.list_cmds.print" )
147
+ def test_git_commits_per_date (self , mock_print , mock_run_git_command ) -> None :
148
+ """
149
+ Test case for git_commits_per_date function.
150
+ """
151
+ mock_run_git_command .return_value = "2021-01-01\n 2021-01-01\n 2021-01-02\n "
152
+ list_cmds .git_commits_per_date (self .mock_config )
153
+
154
+ mock_print .assert_called ()
155
+ mock_run_git_command .assert_called_once ()
156
+
157
+ @patch ("git_py_stats.list_cmds.run_git_command" )
158
+ @patch ("git_py_stats.list_cmds.print" )
159
+ def test_git_commits_per_date_no_data (self , mock_print , mock_run_git_command ) -> None :
160
+ """
161
+ Test case for git_commits_per_date with no data.
162
+ """
163
+ mock_run_git_command .return_value = ""
164
+ list_cmds .git_commits_per_date (self .mock_config )
165
+
166
+ mock_print .assert_called_with ("No commits found." )
167
+
168
+ @patch ("git_py_stats.list_cmds.run_git_command" )
169
+ @patch ("git_py_stats.list_cmds.print" )
170
+ def test_git_commits_per_month (self , mock_print , mock_run_git_command ) -> None :
171
+ """
172
+ Test case for git_commits_per_month function.
173
+ """
174
+ mock_run_git_command .return_value = "Jan\n Jan\n Feb\n "
175
+ list_cmds .git_commits_per_month (self .mock_config )
176
+
177
+ mock_print .assert_called ()
178
+ mock_run_git_command .assert_called_once ()
179
+
180
+ @patch ("git_py_stats.list_cmds.run_git_command" )
181
+ @patch ("git_py_stats.list_cmds.print" )
182
+ def test_git_commits_per_year (self , mock_print , mock_run_git_command ) -> None :
183
+ """
184
+ Test case for git_commits_per_year function.
185
+ """
186
+ mock_run_git_command .return_value = "2020\n 2021\n 2021\n 2022\n "
187
+ list_cmds .git_commits_per_year (self .mock_config )
188
+
189
+ mock_print .assert_any_call ("Git commits by year:\n " )
190
+ self .assertGreater (mock_print .call_count , 1 )
191
+ mock_run_git_command .assert_called_once ()
192
+
193
+ @patch ("git_py_stats.list_cmds.run_git_command" )
194
+ @patch ("git_py_stats.list_cmds.print" )
195
+ def test_git_commits_per_year_empty (self , mock_print , mock_run_git_command ) -> None :
196
+ """
197
+ Test case for git_commits_per_year with empty data.
198
+ """
199
+ mock_run_git_command .return_value = "" # No output
200
+ list_cmds .git_commits_per_year (self .mock_config )
201
+
202
+ mock_print .assert_called_with ("No commits found." )
203
+ mock_run_git_command .assert_called_once ()
204
+
205
+ @patch ("git_py_stats.list_cmds.run_git_command" )
206
+ @patch ("git_py_stats.list_cmds.print" )
207
+ def test_git_commits_per_year_invalid_data (self , mock_print , mock_run_git_command ) -> None :
208
+ """
209
+ Test case for git_commits_per_year with invalid data.
210
+ """
211
+ mock_run_git_command .return_value = "\n \n \n " # Invalid output, just new lines
212
+ list_cmds .git_commits_per_year (self .mock_config )
213
+
214
+ mock_print .assert_called_with ("No valid years found in commits." )
215
+ mock_run_git_command .assert_called_once ()
216
+
217
+ @patch ("git_py_stats.list_cmds.run_git_command" )
218
+ @patch ("git_py_stats.list_cmds.print" )
219
+ def test_git_commits_per_weekday (self , mock_print , mock_run_git_command ) -> None :
220
+ """
221
+ Test case for git_commits_per_weekday function.
222
+ """
223
+ mock_run_git_command .return_value = "Mon\n Tue\n Wed\n "
224
+ list_cmds .git_commits_per_weekday (self .mock_config )
225
+
226
+ mock_print .assert_called ()
227
+ mock_run_git_command .assert_called_once ()
228
+
229
+ @patch ("git_py_stats.list_cmds.run_git_command" )
230
+ @patch ("git_py_stats.list_cmds.print" )
231
+ def test_git_commits_per_hour (self , mock_print , mock_run_git_command ) -> None :
232
+ """
233
+ Test case for git_commits_per_hour function.
234
+ """
235
+ mock_run_git_command .return_value = "10\n 11\n 12\n "
236
+ list_cmds .git_commits_per_hour (self .mock_config )
237
+
238
+ mock_print .assert_called ()
239
+ mock_run_git_command .assert_called_once ()
240
+
241
+ @patch ("git_py_stats.list_cmds.run_git_command" )
242
+ @patch ("git_py_stats.list_cmds.print" )
243
+ def test_git_commits_per_timezone (self , mock_print , mock_run_git_command ) -> None :
244
+ """
245
+ Test case for git_commits_per_timezone function.
246
+ """
247
+ mock_run_git_command .return_value = "+0200\n -0500\n +0200\n "
248
+ list_cmds .git_commits_per_timezone (self .mock_config )
249
+
250
+ mock_print .assert_called ()
251
+ mock_run_git_command .assert_called_once ()
47
252
48
253
49
254
if __name__ == "__main__" :
0 commit comments