Skip to content

Commit

Permalink
Cover all code paths in search_commits
Browse files Browse the repository at this point in the history
The search_commits method was only very lightly tested, meaning over
half of it was not covered. Write another test case, covering all code
paths.
  • Loading branch information
s-t-e-v-e-n-k committed Oct 21, 2021
1 parent 93b92cd commit 69df7c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 3 additions & 7 deletions github/MainClass.py
Expand Up @@ -598,19 +598,15 @@ def search_commits(
"""
assert isinstance(query, str), query
url_parameters = dict()
if (
sort is not github.GithubObject.NotSet
): # pragma no branch (Should be covered)
if sort is not github.GithubObject.NotSet:
assert sort in ("author-date", "committer-date"), sort
url_parameters["sort"] = sort
if (
order is not github.GithubObject.NotSet
): # pragma no branch (Should be covered)
if order is not github.GithubObject.NotSet:
assert order in ("asc", "desc"), order
url_parameters["order"] = order

query_chunks = []
if query: # pragma no branch (Should be covered)
if query:
query_chunks.append(query)

for qualifier, value in qualifiers.items():
Expand Down
11 changes: 11 additions & 0 deletions tests/ReplayData/Search.testSearchCommits.txt
@@ -0,0 +1,11 @@
https
GET
api.github.com
None
/search/commits?sort=author-date&order=asc&q=hash%3A1265747e992ba7d34a469b6b2f527809f8bf7067+merge%3Afalse&per_page=1
{'Accept': 'application/vnd.github.cloak-preview', 'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('Server', 'GitHub.com'), ('Date', 'Thu, 21 Oct 2021 04:54:24 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'no-cache'), ('Vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With'), ('X-OAuth-Scopes', 'admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete:packages, delete_repo, gist, notifications, repo, user, workflow, write:discussion, write:packages'), ('X-Accepted-OAuth-Scopes', ''), ('X-GitHub-Media-Type', 'github.v3; param=cloak-preview'), ('Link', '<https://api.github.com/search/commits?sort=author-date&order=asc&q=hash%3A1265747e992ba7d34a469b6b2f527809f8bf7067+merge%3Afalse&per_page=1&page=2>; rel="next", <https://api.github.com/search/commits?sort=author-date&order=asc&q=hash%3A1265747e992ba7d34a469b6b2f527809f8bf7067+merge%3Afalse&per_page=1&page=2>; rel="last"'), ('X-RateLimit-Limit', '30'), ('X-RateLimit-Remaining', '28'), ('X-RateLimit-Reset', '1634792071'), ('X-RateLimit-Used', '2'), ('X-RateLimit-Resource', 'search'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'DB8E:3107:5AE85E:63CFBA:6170F280')]
{"total_count":2,"incomplete_results":false,"items":[{"url":"https://api.github.com/repos/PyGithub/PyGithub/commits/1265747e992ba7d34a469b6b2f527809f8bf7067","sha":"1265747e992ba7d34a469b6b2f527809f8bf7067","node_id":"MDY6Q29tbWl0MzU0NDQ5MDoxMjY1NzQ3ZTk5MmJhN2QzNGE0NjliNmIyZjUyNzgwOWY4YmY3MDY3","html_url":"https://github.com/PyGithub/PyGithub/commit/1265747e992ba7d34a469b6b2f527809f8bf7067","comments_url":"https://api.github.com/repos/PyGithub/PyGithub/commits/1265747e992ba7d34a469b6b2f527809f8bf7067/comments","commit":{"url":"https://api.github.com/repos/PyGithub/PyGithub/git/commits/1265747e992ba7d34a469b6b2f527809f8bf7067","author":{"date":"2021-06-02T15:00:00.000+10:00","name":"Steve Kowalik","email":"steven@wedontsleep.org"},"committer":{"date":"2021-06-02T15:00:00.000+10:00","name":"GitHub","email":"noreply@github.com"},"message":"Do not transform requestHeaders when logging (#1965)\n\nRequester.__log() sanitizes the headers of the request so that\r\nauthentication details are not logged, but this has the side effect of\r\nmeaning that future requests that use the same Requester object will\r\nfail. Usually, this is perfectly fine, since almost every method will\r\nonly make one request -- where this falls down is when we make another\r\nafter a redirect. Make a copy of the requestHeaders, and sanitize those.\r\n\r\nFixes #1959","tree":{"url":"https://api.github.com/repos/PyGithub/PyGithub/git/trees/6e5b9e20c26a6b3ba8c5481a8e2896bb72a91bbb","sha":"6e5b9e20c26a6b3ba8c5481a8e2896bb72a91bbb"},"comment_count":0},"author":{"login":"s-t-e-v-e-n-k","id":15225059,"node_id":"MDQ6VXNlcjE1MjI1MDU5","avatar_url":"https://avatars.githubusercontent.com/u/15225059?v=4","gravatar_id":"","url":"https://api.github.com/users/s-t-e-v-e-n-k","html_url":"https://github.com/s-t-e-v-e-n-k","followers_url":"https://api.github.com/users/s-t-e-v-e-n-k/followers","following_url":"https://api.github.com/users/s-t-e-v-e-n-k/following{/other_user}","gists_url":"https://api.github.com/users/s-t-e-v-e-n-k/gists{/gist_id}","starred_url":"https://api.github.com/users/s-t-e-v-e-n-k/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/s-t-e-v-e-n-k/subscriptions","organizations_url":"https://api.github.com/users/s-t-e-v-e-n-k/orgs","repos_url":"https://api.github.com/users/s-t-e-v-e-n-k/repos","events_url":"https://api.github.com/users/s-t-e-v-e-n-k/events{/privacy}","received_events_url":"https://api.github.com/users/s-t-e-v-e-n-k/received_events","type":"User","site_admin":false},"committer":{"login":"web-flow","id":19864447,"node_id":"MDQ6VXNlcjE5ODY0NDQ3","avatar_url":"https://avatars.githubusercontent.com/u/19864447?v=4","gravatar_id":"","url":"https://api.github.com/users/web-flow","html_url":"https://github.com/web-flow","followers_url":"https://api.github.com/users/web-flow/followers","following_url":"https://api.github.com/users/web-flow/following{/other_user}","gists_url":"https://api.github.com/users/web-flow/gists{/gist_id}","starred_url":"https://api.github.com/users/web-flow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/web-flow/subscriptions","organizations_url":"https://api.github.com/users/web-flow/orgs","repos_url":"https://api.github.com/users/web-flow/repos","events_url":"https://api.github.com/users/web-flow/events{/privacy}","received_events_url":"https://api.github.com/users/web-flow/received_events","type":"User","site_admin":false},"parents":[{"url":"https://api.github.com/repos/PyGithub/PyGithub/commits/ed7d0fe94f063d228a7dd95b7df5ae1bedb0707a","html_url":"https://github.com/PyGithub/PyGithub/commit/ed7d0fe94f063d228a7dd95b7df5ae1bedb0707a","sha":"ed7d0fe94f063d228a7dd95b7df5ae1bedb0707a"}],"repository":{"id":3544490,"node_id":"MDEwOlJlcG9zaXRvcnkzNTQ0NDkw","name":"PyGithub","full_name":"PyGithub/PyGithub","private":false,"owner":{"login":"PyGithub","id":11288996,"node_id":"MDEyOk9yZ2FuaXphdGlvbjExMjg4OTk2","avatar_url":"https://avatars.githubusercontent.com/u/11288996?v=4","gravatar_id":"","url":"https://api.github.com/users/PyGithub","html_url":"https://github.com/PyGithub","followers_url":"https://api.github.com/users/PyGithub/followers","following_url":"https://api.github.com/users/PyGithub/following{/other_user}","gists_url":"https://api.github.com/users/PyGithub/gists{/gist_id}","starred_url":"https://api.github.com/users/PyGithub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PyGithub/subscriptions","organizations_url":"https://api.github.com/users/PyGithub/orgs","repos_url":"https://api.github.com/users/PyGithub/repos","events_url":"https://api.github.com/users/PyGithub/events{/privacy}","received_events_url":"https://api.github.com/users/PyGithub/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/PyGithub/PyGithub","description":"Typed interactions with the GitHub API v3","fork":false,"url":"https://api.github.com/repos/PyGithub/PyGithub","forks_url":"https://api.github.com/repos/PyGithub/PyGithub/forks","keys_url":"https://api.github.com/repos/PyGithub/PyGithub/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PyGithub/PyGithub/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PyGithub/PyGithub/teams","hooks_url":"https://api.github.com/repos/PyGithub/PyGithub/hooks","issue_events_url":"https://api.github.com/repos/PyGithub/PyGithub/issues/events{/number}","events_url":"https://api.github.com/repos/PyGithub/PyGithub/events","assignees_url":"https://api.github.com/repos/PyGithub/PyGithub/assignees{/user}","branches_url":"https://api.github.com/repos/PyGithub/PyGithub/branches{/branch}","tags_url":"https://api.github.com/repos/PyGithub/PyGithub/tags","blobs_url":"https://api.github.com/repos/PyGithub/PyGithub/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PyGithub/PyGithub/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PyGithub/PyGithub/git/refs{/sha}","trees_url":"https://api.github.com/repos/PyGithub/PyGithub/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PyGithub/PyGithub/statuses/{sha}","languages_url":"https://api.github.com/repos/PyGithub/PyGithub/languages","stargazers_url":"https://api.github.com/repos/PyGithub/PyGithub/stargazers","contributors_url":"https://api.github.com/repos/PyGithub/PyGithub/contributors","subscribers_url":"https://api.github.com/repos/PyGithub/PyGithub/subscribers","subscription_url":"https://api.github.com/repos/PyGithub/PyGithub/subscription","commits_url":"https://api.github.com/repos/PyGithub/PyGithub/commits{/sha}","git_commits_url":"https://api.github.com/repos/PyGithub/PyGithub/git/commits{/sha}","comments_url":"https://api.github.com/repos/PyGithub/PyGithub/comments{/number}","issue_comment_url":"https://api.github.com/repos/PyGithub/PyGithub/issues/comments{/number}","contents_url":"https://api.github.com/repos/PyGithub/PyGithub/contents/{+path}","compare_url":"https://api.github.com/repos/PyGithub/PyGithub/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PyGithub/PyGithub/merges","archive_url":"https://api.github.com/repos/PyGithub/PyGithub/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PyGithub/PyGithub/downloads","issues_url":"https://api.github.com/repos/PyGithub/PyGithub/issues{/number}","pulls_url":"https://api.github.com/repos/PyGithub/PyGithub/pulls{/number}","milestones_url":"https://api.github.com/repos/PyGithub/PyGithub/milestones{/number}","notifications_url":"https://api.github.com/repos/PyGithub/PyGithub/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PyGithub/PyGithub/labels{/name}","releases_url":"https://api.github.com/repos/PyGithub/PyGithub/releases{/id}","deployments_url":"https://api.github.com/repos/PyGithub/PyGithub/deployments"},"score":1.0}]}

9 changes: 9 additions & 0 deletions tests/Search.py
Expand Up @@ -195,6 +195,15 @@ def testPaginateSearchCommits(self):
)
self.assertEqual(commits.totalCount, 3)

def testSearchCommits(self):
commits = self.g.search_commits(
query="hash:1265747e992ba7d34a469b6b2f527809f8bf7067",
sort="author-date",
order="asc",
merge="false",
)
self.assertEqual(commits.totalCount, 2)

def testSearchTopics(self):
topics = self.g.search_topics("python", repositories=">950")
self.assertListKeyBegin(
Expand Down

0 comments on commit 69df7c7

Please sign in to comment.