Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dana-yaish committed Nov 8, 2023
1 parent 34dea1e commit 85d7c67
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion codecov_cli/helpers/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ def is_fork_pr(pr_num, slug, service):
git_service = get_git_service(service)
if git_service:
pull_dict = git_service.get_pull_request(decoded_slug, pr_num)
if pull_dict and pull_dict["head"]["slug"] != decoded_slug:
if pull_dict and pull_dict["head"]["slug"] != pull_dict["base"]["slug"]:
return True
return False
73 changes: 73 additions & 0 deletions tests/helpers/git_services/test_github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import json

import pytest
import requests
from requests import Response

from codecov_cli.helpers import git
from codecov_cli.helpers.git_services.github import Github


def test_get_pull_request(mocker):
def mock_request(*args, headers={}, **kwargs):
assert headers["X-GitHub-Api-Version"] == "2022-11-28"
res = {
"url": "https://api.github.com/repos/codecov/codecov-cli/pulls/1",
"head": {
"sha": "123",
"label": "codecov-cli:branch",
"ref": "branch",
"repo": {"full_name": "user_forked_repo/codecov-cli"},
},
"base": {
"sha": "123",
"label": "codecov-cli:main",
"ref": "main",
"repo": {"full_name": "codecov/codecov-cli"},
},
}
response = Response()
response.status_code = 200
response._content = json.dumps(res).encode("utf-8")
return response

mocker.patch.object(
requests,
"get",
side_effect=mock_request,
)
slug = "codecov/codecov-cli"
response = Github().get_pull_request(slug, 1)
assert response == {
"url": "https://api.github.com/repos/codecov/codecov-cli/pulls/1",
"head": {
"sha": "123",
"label": "codecov-cli:branch",
"ref": "branch",
"slug": "user_forked_repo/codecov-cli",
},
"base": {
"sha": "123",
"label": "codecov-cli:main",
"ref": "main",
"slug": "codecov/codecov-cli",
},
}

def test_get_pull_request_404(mocker):
def mock_request(*args, headers={}, **kwargs):
assert headers["X-GitHub-Api-Version"] == "2022-11-28"
res = {}
response = Response()
response.status_code = 404
response._content = json.dumps(res).encode("utf-8")
return response

mocker.patch.object(
requests,
"get",
side_effect=mock_request,
)
slug = "codecov/codecov-cli"
response = Github().get_pull_request(slug, 1)
assert response == None

0 comments on commit 85d7c67

Please sign in to comment.