Skip to content

Commit af4efb8

Browse files
committed
test: harden repr assertions
Signed-off-by: Venu Vardhan Reddy Tekula <venuvrtekula@gmail.com>
1 parent 4ae74ab commit af4efb8

4 files changed

Lines changed: 52 additions & 24 deletions

File tree

contributor_stats.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ def __repr__(self) -> str:
5959
f"contributor_stats(username={self.username}, "
6060
f"new_contributor={self.new_contributor}, "
6161
f"avatar_url={self.avatar_url}, "
62-
f"contribution_count={self.contribution_count}, commit_url={self.commit_url})"
62+
f"contribution_count={self.contribution_count}, "
63+
f"commit_url={self.commit_url}, "
6364
f"sponsor_info={self.sponsor_info})"
6465
)
6566

test_contributor_stats.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ def test_init(self):
4747

4848
def test_repr(self):
4949
"""Test the __repr__ method includes key fields."""
50-
representation = repr(self.contributor)
51-
self.assertIn("contributor_stats(username=zkoppert", representation)
52-
self.assertIn("new_contributor=False", representation)
53-
self.assertIn(
54-
"avatar_url=https://avatars.githubusercontent.com/u/29484535?v=4",
55-
representation,
50+
expected = (
51+
"contributor_stats(username=zkoppert, "
52+
"new_contributor=False, "
53+
"avatar_url=https://avatars.githubusercontent.com/u/29484535?v=4, "
54+
"contribution_count=1261, "
55+
"commit_url=commit_url5, "
56+
"sponsor_info=)"
5657
)
57-
self.assertIn("contribution_count=1261", representation)
58+
self.assertEqual(repr(self.contributor), expected)
5859

5960
def test_merge_contributors(self):
6061
"""

test_contributors.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import runpy
44
import unittest
5-
from unittest.mock import MagicMock, patch
5+
from unittest.mock import MagicMock, call, patch
66

77
import contributors as contributors_module
88
from contributor_stats import ContributorStats
9-
from contributors import get_all_contributors, get_contributors
109

1110

1211
class TestContributors(unittest.TestCase):
@@ -27,7 +26,7 @@ def test_get_contributors(self, mock_contributor_stats):
2726
mock_repo.contributors.return_value = [mock_user]
2827
mock_repo.full_name = "owner/repo"
2928

30-
get_contributors(mock_repo, "2022-01-01", "2022-12-31", "")
29+
contributors_module.get_contributors(mock_repo, "2022-01-01", "2022-12-31", "")
3130

3231
mock_contributor_stats.assert_called_once_with(
3332
"user",
@@ -60,8 +59,8 @@ def test_get_all_contributors_with_organization(self, mock_get_contributors):
6059
]
6160
ghe = ""
6261

63-
result = get_all_contributors(
64-
"org", "", "2022-01-01", "2022-12-31", mock_github_connection, ghe
62+
result = contributors_module.get_all_contributors(
63+
"org", [], "2022-01-01", "2022-12-31", mock_github_connection, ghe
6564
)
6665

6766
self.assertEqual(
@@ -99,7 +98,7 @@ def test_get_all_contributors_with_repository(self, mock_get_contributors):
9998
]
10099
ghe = ""
101100

102-
result = get_all_contributors(
101+
result = contributors_module.get_all_contributors(
103102
"", ["owner/repo"], "2022-01-01", "2022-12-31", mock_github_connection, ghe
104103
)
105104

@@ -135,14 +134,22 @@ def test_get_contributors_skip_users_with_no_commits(self, mock_contributor_stat
135134
mock_user2.avatar_url = "https://avatars.githubusercontent.com/u/12345679?v=4"
136135
mock_user2.contributions_count = 102
137136

138-
mock_repo.contributors.return_value = [mock_user]
137+
mock_repo.contributors.return_value = [mock_user, mock_user2]
139138
mock_repo.full_name = "owner/repo"
140-
mock_repo.get_commits.side_effect = StopIteration
139+
mock_repo.commits.side_effect = [
140+
iter([object()]), # user has commits in range
141+
iter([]), # user2 has no commits in range and should be skipped
142+
]
141143
ghe = ""
142144

143-
get_contributors(mock_repo, "2022-01-01", "2022-12-31", ghe)
145+
contributors_module.get_contributors(mock_repo, "2022-01-01", "2022-12-31", ghe)
144146

145-
# Note that only user is returned and user2 is not returned here because there were no commits in the date range
147+
mock_repo.commits.assert_has_calls(
148+
[
149+
call(author="user", since="2022-01-01", until="2022-12-31"),
150+
call(author="user2", since="2022-01-01", until="2022-12-31"),
151+
]
152+
)
146153
mock_contributor_stats.assert_called_once_with(
147154
"user",
148155
False,
@@ -168,10 +175,10 @@ def test_get_contributors_skip_bot(self, mock_contributor_stats):
168175
mock_repo.get_commits.side_effect = StopIteration
169176
ghe = ""
170177

171-
get_contributors(mock_repo, "2022-01-01", "2022-12-31", ghe)
178+
contributors_module.get_contributors(mock_repo, "2022-01-01", "2022-12-31", ghe)
172179

173-
# Note that only user is returned and user2 is not returned here because there were no commits in the date range
174-
mock_contributor_stats.isEmpty()
180+
# Ensure that the bot user is skipped and ContributorStats is never instantiated
181+
mock_contributor_stats.assert_not_called()
175182

176183
@patch("contributors.contributor_stats.ContributorStats")
177184
def test_get_contributors_no_commit_end_date(self, mock_contributor_stats):
@@ -189,7 +196,7 @@ def test_get_contributors_no_commit_end_date(self, mock_contributor_stats):
189196
mock_repo.get_commits.side_effect = StopIteration
190197
ghe = ""
191198

192-
get_contributors(mock_repo, "2022-01-01", "", ghe)
199+
contributors_module.get_contributors(mock_repo, "2022-01-01", "", ghe)
193200

194201
# Note that only user is returned and user2 is not returned here because there were no commits in the date range
195202
mock_contributor_stats.assert_called_once_with(
@@ -212,7 +219,9 @@ def test_get_contributors_skips_when_no_commits_in_range(self):
212219
mock_repo.full_name = "owner/repo"
213220
mock_repo.commits.return_value = iter([])
214221

215-
result = get_contributors(mock_repo, "2022-01-01", "2022-12-31", "")
222+
result = contributors_module.get_contributors(
223+
mock_repo, "2022-01-01", "2022-12-31", ""
224+
)
216225

217226
self.assertEqual(result, [])
218227

@@ -230,7 +239,9 @@ def __iter__(self):
230239
mock_repo.contributors.return_value = BoomIterable()
231240

232241
with patch("builtins.print") as mock_print:
233-
result = get_contributors(mock_repo, "2022-01-01", "2022-12-31", "")
242+
result = contributors_module.get_contributors(
243+
mock_repo, "2022-01-01", "2022-12-31", ""
244+
)
234245

235246
self.assertIsNone(result)
236247
mock_print.assert_any_call(

test_env.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,21 @@ def test_get_env_vars_valid_date_range(self):
294294
self.assertEqual(start_date, "2024-01-01")
295295
self.assertEqual(end_date, "2025-01-01")
296296

297+
@patch.dict(os.environ, {"TEST_INT": "12.34"}, clear=True)
298+
def test_get_int_env_var_returns_none_for_invalid_int(self):
299+
"""Test that invalid integer env values return None."""
300+
self.assertIsNone(env.get_int_env_var("TEST_INT"))
301+
302+
def test_validate_date_range_invalid_date_format_raises(self):
303+
"""Test that invalid date formats raise a ValueError."""
304+
with self.assertRaises(ValueError) as cm:
305+
env.validate_date_range("2024/01/01", "2024-02-01")
306+
the_exception = cm.exception
307+
self.assertEqual(
308+
str(the_exception),
309+
"start_date and end_date must be in the format YYYY-MM-DD",
310+
)
311+
297312

298313
if __name__ == "__main__":
299314
unittest.main()

0 commit comments

Comments
 (0)