Skip to content

Commit

Permalink
Merge pull request #7 from github/tests
Browse files Browse the repository at this point in the history
Improve tests to cover repo and org options
  • Loading branch information
zkoppert committed Oct 10, 2023
2 parents 16148dd + 9f2f862 commit 55a6567
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
8 changes: 4 additions & 4 deletions contributor_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# "username" : "zkoppert",
# "avatar_url" : "https://avatars.githubusercontent.com/u/29484535?v=4",
# "contribution_count" : "1261",
# "commits" : "https://github.com/github/contributors/commits?author=zkoppert&since=2023-10-01&until=2023-10-05"
# "commit_url" : "https://github.com/github/contributors/commits?author=zkoppert&since=2023-10-01&until=2023-10-05"
# }
# ]

Expand All @@ -17,17 +17,17 @@ def __new__(cls, *args, **kwargs): # pylint: disable=unused-argument
"""Create a new contributor_stats object"""
return super().__new__(cls)

def __init__(self, username, avatar_url, contribution_count, commits):
def __init__(self, username, avatar_url, contribution_count, commit_url):
"""Initialize the contributor_stats object"""
self.username = username
self.avatar_url = avatar_url
self.contribution_count = contribution_count
self.commits = commits
self.commit_url = commit_url

def __repr__(self) -> str:
"""Return the representation of the contributor_stats object"""
return (
f"contributor_stats(username={self.username}, "
f"avatar_url={self.avatar_url}, "
f"contribution_count={self.contribution_count}, commits={self.commits})"
f"contribution_count={self.contribution_count}, commit_url={self.commit_url})"
)
4 changes: 2 additions & 2 deletions markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def write_to_markdown(collaborators, filename):
for collaborator in repo:
username = collaborator.username
contribution_count = collaborator.contribution_count
commits = collaborator.commits
commit_url = collaborator.commit_url

row = f"| {username} | {contribution_count} | {commits} |\n"
row = f"| {username} | {contribution_count} | {commit_url} |\n"
table += row

with open(filename, "w", encoding="utf-8") as markdown_file:
Expand Down
12 changes: 3 additions & 9 deletions test_contributor_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ def setUp(self):
"zkoppert",
"https://avatars.githubusercontent.com/u/29484535?v=4",
"1261",
{
"744d20e": "2023-06-29 09:43:24 -0700",
"5c622f9": "2023-06-29 15:55:38 -0700",
},
"commit_url5",
)

def test_init(self):
Expand All @@ -34,11 +31,8 @@ def test_init(self):
)
self.assertEqual(self.contributor.contribution_count, "1261")
self.assertEqual(
self.contributor.commits,
{
"744d20e": "2023-06-29 09:43:24 -0700",
"5c622f9": "2023-06-29 15:55:38 -0700",
},
self.contributor.commit_url,
"commit_url5",
)


Expand Down
67 changes: 66 additions & 1 deletion test_contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import unittest
from unittest.mock import patch, MagicMock
from contributors import get_contributors
from contributors import get_contributors, get_all_contributors


class TestContributors(unittest.TestCase):
Expand Down Expand Up @@ -32,6 +32,71 @@ def test_get_contributors(self, mock_contributor_stats):
"https://github.com/owner/repo/commits?author=user&since=2022-01-01&until=2022-12-31",
)

@patch("contributors.get_contributors")
def test_get_all_contributors_with_organization(self, mock_get_contributors):
"""
Test the get_all_contributors function when an organization is provided.
"""
mock_github_connection = MagicMock()
mock_github_connection.organization().repositories.return_value = [
"repo1",
"repo2",
]
mock_get_contributors.return_value = [
{"username": "user", "contribution_count": "100", "commits": "commit_url"}
]

result = get_all_contributors(
"org", "", "2022-01-01", "2022-12-31", mock_github_connection
)

self.assertEqual(
result,
[
[
{
"username": "user",
"contribution_count": "100",
"commits": "commit_url",
}
]
]
* 2,
)
mock_get_contributors.assert_any_call("repo1", "2022-01-01", "2022-12-31")
mock_get_contributors.assert_any_call("repo2", "2022-01-01", "2022-12-31")

@patch("contributors.get_contributors")
def test_get_all_contributors_with_repository(self, mock_get_contributors):
"""
Test the get_all_contributors function when a repository is provided.
"""
mock_github_connection = MagicMock()
mock_github_connection.repository.return_value = "repo"
mock_get_contributors.return_value = [
{"username": "user", "contribution_count": "100", "commits": "commit_url2"}
]

result = get_all_contributors(
"", "owner/repo", "2022-01-01", "2022-12-31", mock_github_connection
)

self.assertEqual(
result,
[
[
{
"username": "user",
"contribution_count": "100",
"commits": "commit_url2",
}
]
],
)
mock_get_contributors.assert_called_once_with(
"repo", "2022-01-01", "2022-12-31"
)


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

0 comments on commit 55a6567

Please sign in to comment.