|
17 | 17 | """ |
18 | 18 |
|
19 | 19 | import io |
| 20 | +import json |
20 | 21 | import os |
21 | 22 | import unittest |
22 | 23 | from datetime import datetime, timedelta, timezone |
23 | 24 | from unittest.mock import MagicMock, call, patch |
24 | 25 |
|
25 | 26 | import github3.github |
26 | | -from stale_repos import auth_to_github, get_inactive_repos, write_to_markdown |
| 27 | + |
| 28 | +from stale_repos import ( |
| 29 | + auth_to_github, |
| 30 | + get_inactive_repos, |
| 31 | + output_to_json, |
| 32 | + write_to_markdown, |
| 33 | +) |
27 | 34 |
|
28 | 35 |
|
29 | 36 | class AuthToGithubTestCase(unittest.TestCase): |
@@ -198,20 +205,29 @@ def test_print_inactive_repos_with_no_inactive_repos(self): |
198 | 205 | exceed the specified threshold. |
199 | 206 |
|
200 | 207 | """ |
201 | | - github_connection = MagicMock() |
| 208 | + mock_github = MagicMock() |
| 209 | + mock_org = MagicMock() |
202 | 210 |
|
203 | 211 | # Create a mock repository object with a last push time of 30 days ago |
204 | 212 | thirty_days_ago = datetime.now(timezone.utc) - timedelta(days=30) |
205 | | - mock_repo = MagicMock() |
206 | | - mock_repo.pushed_at = thirty_days_ago.isoformat() |
207 | | - mock_repo.html_url = "https://github.com/example/repo" |
208 | | - github_connection.repositories_by.return_value = [mock_repo] |
| 213 | + mock_repo1 = MagicMock() |
| 214 | + mock_repo1.pushed_at = thirty_days_ago.isoformat() |
| 215 | + mock_repo1.html_url = "https://github.com/example/repo" |
| 216 | + mock_repo2 = MagicMock() |
| 217 | + mock_repo2.pushed_at = None |
| 218 | + mock_repo2.html_url = "https://github.com/example/repo2" |
| 219 | + |
| 220 | + mock_github.organization.return_value = mock_org |
| 221 | + mock_org.repositories.return_value = [ |
| 222 | + mock_repo1, |
| 223 | + mock_repo2, |
| 224 | + ] |
209 | 225 |
|
210 | 226 | # Call the function with a threshold of 40 days |
211 | 227 | inactive_days_threshold = 40 |
212 | 228 | organization = "example" |
213 | 229 | with patch("sys.stdout", new_callable=io.StringIO) as mock_stdout: |
214 | | - get_inactive_repos(github_connection, inactive_days_threshold, organization) |
| 230 | + get_inactive_repos(mock_github, inactive_days_threshold, organization) |
215 | 231 | output = mock_stdout.getvalue() |
216 | 232 |
|
217 | 233 | # Check that the output contains the expected repo URL and days inactive |
@@ -262,5 +278,37 @@ def test_write_to_markdown(self): |
262 | 278 | mock_file.__enter__.return_value.assert_has_calls(expected_calls) |
263 | 279 |
|
264 | 280 |
|
| 281 | +class OutputToJson(unittest.TestCase): |
| 282 | + """ |
| 283 | + Unit test case for the output_to_json() function. |
| 284 | + """ |
| 285 | + |
| 286 | + def test_output_to_json(self): |
| 287 | + """Test that output_to_json returns the expected json string. |
| 288 | +
|
| 289 | + This test creates a list of inactive repos and calls the |
| 290 | + output_to_json function with the list. It then checks that the |
| 291 | + function returns the expected json string. |
| 292 | +
|
| 293 | + """ |
| 294 | + # Create a list of inactive repos |
| 295 | + inactive_repos = [ |
| 296 | + ("https://github.com/example/repo1", 31), |
| 297 | + ("https://github.com/example/repo2", 30), |
| 298 | + ("https://github.com/example/repo3", 29), |
| 299 | + ] |
| 300 | + |
| 301 | + # Call the output_to_json function with the list of inactive repos |
| 302 | + expected_json = json.dumps( |
| 303 | + [ |
| 304 | + {"url": "https://github.com/example/repo1", "daysInactive": 31}, |
| 305 | + {"url": "https://github.com/example/repo2", "daysInactive": 30}, |
| 306 | + {"url": "https://github.com/example/repo3", "daysInactive": 29}, |
| 307 | + ] |
| 308 | + ) |
| 309 | + actual_json = output_to_json(inactive_repos) |
| 310 | + assert actual_json == expected_json |
| 311 | + |
| 312 | + |
265 | 313 | if __name__ == "__main__": |
266 | 314 | unittest.main() |
0 commit comments