|
17 | 17 | from datetime import datetime, timedelta |
18 | 18 | from unittest.mock import MagicMock, patch |
19 | 19 |
|
20 | | -import issue_metrics |
21 | 20 | from issue_metrics import ( |
22 | 21 | IssueWithMetrics, |
23 | 22 | get_env_vars, |
24 | | - get_owner_and_repository, |
| 23 | + get_owners_and_repositories, |
25 | 24 | get_per_issue_metrics, |
26 | 25 | measure_time_to_close, |
27 | 26 | measure_time_to_first_response, |
@@ -52,43 +51,62 @@ def test_search_issues(self): |
52 | 51 | mock_connection.search_issues.return_value = mock_issues |
53 | 52 |
|
54 | 53 | # Call search_issues and check that it returns the correct issues |
55 | | - issues = search_issues( |
56 | | - "is:open", mock_connection, "fakeowner", "fakerepository" |
57 | | - ) |
| 54 | + repo_with_owner = {"owner": "owner1", "repository": "repo1"} |
| 55 | + owners_and_repositories = [repo_with_owner] |
| 56 | + issues = search_issues("is:open", mock_connection, owners_and_repositories) |
58 | 57 | self.assertEqual(issues, mock_issues) |
59 | 58 |
|
60 | 59 |
|
61 | 60 | class TestGetOwnerAndRepository(unittest.TestCase): |
62 | | - """Unit tests for the get_owner_and_repository function. |
| 61 | + """Unit tests for the get_owners_and_repositories function. |
63 | 62 |
|
64 | | - This class contains unit tests for the get_owner_and_repository function in the |
| 63 | + This class contains unit tests for the get_owners_and_repositories function in the |
65 | 64 | issue_metrics module. The tests use the unittest module and the unittest.mock |
66 | 65 | module to mock the GitHub API and test the function in isolation. |
67 | 66 |
|
68 | 67 | Methods: |
69 | | - test_get_owner_with_owner_and_repo_in_query: Test get both owner and repo. |
70 | | - test_get_owner_and_repository_with_repo_in_query: Test get just owner. |
71 | | - test_get_owner_and_repository_without_either_in_query: Test get neither. |
72 | | -
|
| 68 | + test_get_owners_with_owner_and_repo_in_query: Test get both owner and repo. |
| 69 | + test_get_owners_and_repositories_with_repo_in_query: Test get just owner. |
| 70 | + test_get_owners_and_repositories_without_either_in_query: Test get neither. |
| 71 | + test_get_owners_and_repositories_with_multiple_entries: Test get multiple entries. |
73 | 72 | """ |
74 | 73 |
|
75 | | - def test_get_owner_with_owner_and_repo_in_query(self): |
| 74 | + def test_get_owners_with_owner_and_repo_in_query(self): |
76 | 75 | """Test get both owner and repo.""" |
77 | | - result = get_owner_and_repository("repo:owner1/repo1") |
78 | | - self.assertEqual(result.get("owner"), "owner1") |
79 | | - self.assertEqual(result.get("repository"), "repo1") |
| 76 | + result = get_owners_and_repositories("repo:owner1/repo1") |
| 77 | + self.assertEqual(result[0].get("owner"), "owner1") |
| 78 | + self.assertEqual(result[0].get("repository"), "repo1") |
80 | 79 |
|
81 | | - def test_get_owner_and_repository_with_repo_in_query(self): |
| 80 | + def test_get_owner_and_repositories_with_repo_in_query(self): |
82 | 81 | """Test get just owner.""" |
83 | | - result = get_owner_and_repository("org:owner1") |
84 | | - self.assertEqual(result.get("owner"), "owner1") |
85 | | - self.assertIsNone(result.get("repository")) |
| 82 | + result = get_owners_and_repositories("org:owner1") |
| 83 | + self.assertEqual(result[0].get("owner"), "owner1") |
| 84 | + self.assertIsNone(result[0].get("repository")) |
86 | 85 |
|
87 | | - def test_get_owner_and_repository_without_either_in_query(self): |
| 86 | + def test_get_owners_and_repositories_without_either_in_query(self): |
88 | 87 | """Test get neither.""" |
89 | | - result = get_owner_and_repository("is:blah") |
90 | | - self.assertIsNone(result.get("owner")) |
91 | | - self.assertIsNone(result.get("repository")) |
| 88 | + result = get_owners_and_repositories("is:blah") |
| 89 | + self.assertEqual(result, []) |
| 90 | + |
| 91 | + def test_get_owners_and_repositories_with_multiple_entries(self): |
| 92 | + """Test get multiple entries.""" |
| 93 | + result = get_owners_and_repositories("repo:owner1/repo1 org:owner2") |
| 94 | + self.assertEqual(result[0].get("owner"), "owner1") |
| 95 | + self.assertEqual(result[0].get("repository"), "repo1") |
| 96 | + self.assertEqual(result[1].get("owner"), "owner2") |
| 97 | + self.assertIsNone(result[1].get("repository")) |
| 98 | + |
| 99 | + def test_get_owners_and_repositories_with_org(self): |
| 100 | + """Test get org as owner.""" |
| 101 | + result = get_owners_and_repositories("org:owner1") |
| 102 | + self.assertEqual(result[0].get("owner"), "owner1") |
| 103 | + self.assertIsNone(result[0].get("repository")) |
| 104 | + |
| 105 | + def test_get_owners_and_repositories_with_user(self): |
| 106 | + """Test get user as owner.""" |
| 107 | + result = get_owners_and_repositories("user:owner1") |
| 108 | + self.assertEqual(result[0].get("owner"), "owner1") |
| 109 | + self.assertIsNone(result[0].get("repository")) |
92 | 110 |
|
93 | 111 |
|
94 | 112 | class TestGetEnvVars(unittest.TestCase): |
@@ -120,115 +138,6 @@ def test_get_env_vars_missing_query(self): |
120 | 138 | get_env_vars(test=True) |
121 | 139 |
|
122 | 140 |
|
123 | | -class TestMain(unittest.TestCase): |
124 | | - """Unit tests for the main function. |
125 | | -
|
126 | | - This class contains unit tests for the main function in the issue_metrics |
127 | | - module. The tests use the unittest module and the unittest.mock module to |
128 | | - mock the GitHub API and test the function in isolation. |
129 | | -
|
130 | | - Methods: |
131 | | - test_main: Test that main runs without errors. |
132 | | - test_main_no_issues_found: Test that main handles when no issues are found |
133 | | -
|
134 | | - """ |
135 | | - |
136 | | - @patch("issue_metrics.auth_to_github") |
137 | | - @patch("issue_metrics.search_issues") |
138 | | - @patch("issue_metrics.measure_time_to_first_response") |
139 | | - @patch("issue_metrics.get_stats_time_to_first_response") |
140 | | - @patch.dict( |
141 | | - os.environ, |
142 | | - { |
143 | | - "SEARCH_QUERY": "is:open repo:user/repo", |
144 | | - "GH_TOKEN": "test_token", |
145 | | - }, |
146 | | - ) |
147 | | - def test_main( |
148 | | - self, |
149 | | - mock_get_stats_time_to_first_response, |
150 | | - mock_measure_time_to_first_response, |
151 | | - mock_search_issues, |
152 | | - mock_auth_to_github, |
153 | | - ): |
154 | | - """Test that main runs without errors.""" |
155 | | - # Set up the mock GitHub connection object |
156 | | - mock_connection = MagicMock() |
157 | | - mock_auth_to_github.return_value = mock_connection |
158 | | - |
159 | | - # Set up the mock search_issues function |
160 | | - mock_issues = MagicMock( |
161 | | - items=[ |
162 | | - MagicMock(title="Issue 1"), |
163 | | - MagicMock(title="Issue 2"), |
164 | | - ] |
165 | | - ) |
166 | | - |
167 | | - mock_search_issues.return_value = mock_issues |
168 | | - |
169 | | - # Set up the mock measure_time_to_first_response function |
170 | | - mock_issues_with_ttfr = [ |
171 | | - ( |
172 | | - "Issue 1", |
173 | | - "https://github.com/user/repo/issues/1", |
174 | | - "alice", |
175 | | - timedelta(days=1, hours=2, minutes=30), |
176 | | - ), |
177 | | - ( |
178 | | - "Issue 2", |
179 | | - "https://github.com/user/repo/issues/2", |
180 | | - "bob", |
181 | | - timedelta(days=3, hours=4, minutes=30), |
182 | | - ), |
183 | | - ] |
184 | | - mock_measure_time_to_first_response.return_value = mock_issues_with_ttfr |
185 | | - |
186 | | - # Set up the mock get_stats_time_to_first_response function |
187 | | - mock_stats_time_to_first_response = 15 |
188 | | - mock_get_stats_time_to_first_response.return_value = ( |
189 | | - mock_stats_time_to_first_response |
190 | | - ) |
191 | | - |
192 | | - # Call main and check that it runs without errors |
193 | | - issue_metrics.main() |
194 | | - |
195 | | - # Remove the markdown file created by main |
196 | | - os.remove("issue_metrics.md") |
197 | | - |
198 | | - @patch("issue_metrics.auth_to_github") |
199 | | - @patch("issue_metrics.search_issues") |
200 | | - @patch("issue_metrics.write_to_markdown") |
201 | | - @patch.dict( |
202 | | - os.environ, |
203 | | - { |
204 | | - "SEARCH_QUERY": "is:open repo:org/repo", |
205 | | - "GH_TOKEN": "test_token", |
206 | | - }, |
207 | | - ) |
208 | | - def test_main_no_issues_found( |
209 | | - self, |
210 | | - mock_write_to_markdown, |
211 | | - mock_search_issues, |
212 | | - mock_auth_to_github, |
213 | | - ): |
214 | | - """Test that main writes 'No issues found' to the |
215 | | - console and calls write_to_markdown with None.""" |
216 | | - |
217 | | - # Set up the mock GitHub connection object |
218 | | - mock_connection = MagicMock() |
219 | | - mock_auth_to_github.return_value = mock_connection |
220 | | - |
221 | | - # Set up the mock search_issues function to return an empty list of issues |
222 | | - mock_issues = MagicMock(items=[]) |
223 | | - mock_search_issues.return_value = mock_issues |
224 | | - |
225 | | - # Call main and check that it writes 'No issues found' |
226 | | - issue_metrics.main() |
227 | | - mock_write_to_markdown.assert_called_once_with( |
228 | | - None, None, None, None, None, None, None, None |
229 | | - ) |
230 | | - |
231 | | - |
232 | 141 | class TestGetPerIssueMetrics(unittest.TestCase): |
233 | 142 | """Test suite for the get_per_issue_metrics function.""" |
234 | 143 |
|
|
0 commit comments