-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_open_contrib_pr.py
139 lines (115 loc) · 4.73 KB
/
test_open_contrib_pr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
""" Tests for the open_contrib_pr.py functions. """
import unittest
from unittest.mock import MagicMock, mock_open, patch
import github3
from open_contrib_pr import clone_repository, create_pull_request, get_repos_json
class TestOpenContribPR(unittest.TestCase):
"""Test case for the open_contrib_pr module."""
@patch(
"builtins.open",
new_callable=mock_open,
read_data='{"repos": ["repo1", "repo2"]}',
)
@patch("os.system")
def test_get_repos_json(self, mock_system, mock_file):
"""
Test the get_repos_json function.
"""
gh_actor = "test_actor"
repos_json_location = "test_location"
token = "test_token"
endpoint = "test_endpoint"
expected_repos = {"repos": ["repo1", "repo2"]}
result = get_repos_json(gh_actor, repos_json_location, token, endpoint)
mock_system.assert_called_once_with(
f"git clone https://{gh_actor}:{token}@{endpoint}/{repos_json_location}"
)
mock_file.assert_called_once_with(
str(repos_json_location), "r", encoding="utf-8"
)
self.assertEqual(result, expected_repos)
class TestCloneRepository(unittest.TestCase):
"""Test case for the clone_repository function."""
@patch("os.system")
def test_clone_repository_success(self, mock_system):
"""
Test the clone_repository function when the clone is successful.
"""
mock_system.return_value = 0 # Simulate successful clone
result = clone_repository(
gh_actor="test_actor",
token="test_token",
endpoint="test_endpoint",
repo={"full_name": "test_actor/test_repo", "name": "test_repo"},
)
mock_system.assert_called_once_with(
"git clone https://test_actor:test_token@test_endpoint/test_actor/test_repo"
)
self.assertEqual(result, "test_repo")
@patch("os.system")
def test_clone_repository_failure(self, mock_system):
"""
Test the clone_repository function when the clone fails.
"""
mock_system.side_effect = OSError("Clone failed") # Simulate clone failure
result = clone_repository(
gh_actor="test_actor",
token="test_token",
endpoint="test_endpoint",
repo={"full_name": "test_actor/test_repo", "name": "test_repo"},
)
mock_system.assert_called_once_with(
"git clone https://test_actor:test_token@test_endpoint/test_actor/test_repo"
)
self.assertIsNone(result)
class TestCreatePullRequest(unittest.TestCase):
"""Test case for the create_pull_request function."""
def test_create_pull_request_success(self):
"""
Test the create_pull_request function when the pull request is created successfully.
"""
github_connection = MagicMock()
github_connection.repository.return_value = MagicMock()
create_pull_request(
organization="test_org",
pr_body="Test PR body",
pr_title="Test PR title",
github_connection=github_connection,
repo_name="test_repo",
branch_name="test_branch",
default_branch="main",
)
github_connection.repository.return_value.create_pull.assert_called_once_with(
title="Test PR title", body="Test PR body", head="test_branch", base="main"
)
def test_create_pull_exceptions(self):
"""
Test the create_pull_request function when an exception occurs.
"""
github_connection = MagicMock()
github_connection.repository.return_value = MagicMock()
for exception, message in [
(
github3.exceptions.UnprocessableEntity(MagicMock()),
"Pull request already exists",
),
(github3.exceptions.ForbiddenError(MagicMock()), "Pull request failed"),
(github3.exceptions.NotFoundError(MagicMock()), "Pull request failed"),
(github3.exceptions.ConnectionError(MagicMock()), "Pull request failed"),
]:
github_connection.repository.return_value.create_pull.side_effect = (
exception
)
with patch("builtins.print") as mock_print:
create_pull_request(
organization="test_org",
pr_body="Test PR body",
pr_title="Test PR title",
github_connection=github_connection,
repo_name="test_repo",
branch_name="test_branch",
default_branch="main",
)
mock_print.assert_called_once_with(message)
if __name__ == "__main__":
unittest.main()