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