Skip to content

Commit

Permalink
Add unit test for GitHelper class
Browse files Browse the repository at this point in the history
  • Loading branch information
pguimon committed Jun 5, 2024
1 parent 2becac3 commit b1d49f6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
19 changes: 11 additions & 8 deletions helpers/GitHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GitHelper(object):

def create_pull_request(self, repository, title, description, head_branch, base_branch, git_token):
"""Creates the pull request for the head_branch against the base_branch"""
git_pulls_api = "https://github.com/api/v3/repos/{repository}/pulls".format(repository)
git_pulls_api = "https://github.com/api/v3/repos/{repository}/pulls".format(repository=repository)

headers = {
"Authorization": "token {0}".format(git_token),
Expand All @@ -24,13 +24,16 @@ def create_pull_request(self, repository, title, description, head_branch, base_
"base": base_branch,
}

r = requests.post(
git_pulls_api,
headers=headers,
data=json.dumps(payload))

if not r.ok:
print("Request Failed: {0}".format(r.text))
try:
r = requests.post(
git_pulls_api,
headers=headers,
data=json.dumps(payload))
if not r.ok:
print("Request Failed: {0}".format(r.text))
except requests.exceptions.RequestException as e:
print("Request Failed: {0}".format(e))
raise e

def push_changes_in_pull_request(self, repository, message, target_branch, head_branch, token, clone_path):

Expand Down
38 changes: 38 additions & 0 deletions tests/helpers/test_GitHelper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import unittest
import requests
from helpers.GitHelper import GitHelper
from unittest import mock

# -------------------------------------------------------------------------------------------
# Mocks used in the below unit test class
# -------------------------------------------------------------------------------------------
def mocked_pull_request_failure(*args, **kwargs):
"""
Mock function to mimic a failure to create a pull request
"""
raise requests.exceptions.HTTPError

# -------------------------------------------------------------------------------------------
# Unit test class
# -------------------------------------------------------------------------------------------
class TestGitHelper(unittest.TestCase):

# Mock some methods. The mock object is passed in to our test case method.
@mock.patch('helpers.GitHelper.requests.post', side_effect=mocked_pull_request_failure)
def test_create_pull_request_ko(self, mock_requests_post):
git_helper = GitHelper()
repository = "repository"
title = "title"
description = "description"
head_branch = "head_branch"
base_branch = "base_branch"
git_token = "git_token"
self.assertRaises(requests.exceptions.HTTPError,
lambda: git_helper.create_pull_request(repository, title, description, head_branch,
base_branch, git_token))

# -------------------------------------------------------------------------------------------
# Main
# -------------------------------------------------------------------------------------------
if __name__ == "__main__":
unittest.main()

0 comments on commit b1d49f6

Please sign in to comment.