Skip to content

Commit cee9fe3

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

4 files changed

Lines changed: 40 additions & 20 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: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
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

@@ -140,7 +139,7 @@ def test_get_contributors_skip_users_with_no_commits(self, mock_contributor_stat
140139
mock_repo.get_commits.side_effect = StopIteration
141140
ghe = ""
142141

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

145144
# Note that only user is returned and user2 is not returned here because there were no commits in the date range
146145
mock_contributor_stats.assert_called_once_with(
@@ -168,10 +167,10 @@ def test_get_contributors_skip_bot(self, mock_contributor_stats):
168167
mock_repo.get_commits.side_effect = StopIteration
169168
ghe = ""
170169

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

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()
172+
# Ensure that the bot user is skipped and ContributorStats is never instantiated
173+
mock_contributor_stats.assert_not_called()
175174

176175
@patch("contributors.contributor_stats.ContributorStats")
177176
def test_get_contributors_no_commit_end_date(self, mock_contributor_stats):
@@ -189,7 +188,7 @@ def test_get_contributors_no_commit_end_date(self, mock_contributor_stats):
189188
mock_repo.get_commits.side_effect = StopIteration
190189
ghe = ""
191190

192-
get_contributors(mock_repo, "2022-01-01", "", ghe)
191+
contributors_module.get_contributors(mock_repo, "2022-01-01", "", ghe)
193192

194193
# Note that only user is returned and user2 is not returned here because there were no commits in the date range
195194
mock_contributor_stats.assert_called_once_with(
@@ -212,7 +211,9 @@ def test_get_contributors_skips_when_no_commits_in_range(self):
212211
mock_repo.full_name = "owner/repo"
213212
mock_repo.commits.return_value = iter([])
214213

215-
result = get_contributors(mock_repo, "2022-01-01", "2022-12-31", "")
214+
result = contributors_module.get_contributors(
215+
mock_repo, "2022-01-01", "2022-12-31", ""
216+
)
216217

217218
self.assertEqual(result, [])
218219

@@ -230,7 +231,9 @@ def __iter__(self):
230231
mock_repo.contributors.return_value = BoomIterable()
231232

232233
with patch("builtins.print") as mock_print:
233-
result = get_contributors(mock_repo, "2022-01-01", "2022-12-31", "")
234+
result = contributors_module.get_contributors(
235+
mock_repo, "2022-01-01", "2022-12-31", ""
236+
)
234237

235238
self.assertIsNone(result)
236239
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)