155 lines
5.0 KiB
Python
155 lines
5.0 KiB
Python
from unittest.mock import MagicMock, patch
|
|
|
|
from aider_gitea.gitea_client import GiteaClient
|
|
|
|
|
|
class TestAssigneeReviewerFunctionality:
|
|
def setup_method(self):
|
|
self.client = GiteaClient('https://gitea.example.com', 'fake_token')
|
|
|
|
@patch('requests.Session.post')
|
|
def test_create_pull_request_with_assignees(self, mock_post):
|
|
"""Test creating a pull request with assignees."""
|
|
# Mock the PR creation response
|
|
pr_response = MagicMock()
|
|
pr_response.status_code = 201
|
|
expected_result = {
|
|
'number': 123,
|
|
'title': 'Test PR',
|
|
'html_url': 'https://gitea.example.com/owner/repo/pulls/123',
|
|
}
|
|
pr_response.json.return_value = expected_result
|
|
mock_post.return_value = pr_response
|
|
|
|
# Call the method with assignees
|
|
result = self.client.create_pull_request(
|
|
owner='owner',
|
|
repo='repo',
|
|
title='Test PR',
|
|
body='Test body',
|
|
head='feature-branch',
|
|
base='main',
|
|
assignees=['user1', 'user2'],
|
|
)
|
|
|
|
# Verify PR creation call
|
|
assert mock_post.call_count == 1
|
|
pr_call_args = mock_post.call_args_list[0]
|
|
assert (
|
|
pr_call_args[0][0]
|
|
== 'https://gitea.example.com/api/v1/repos/owner/repo/pulls'
|
|
)
|
|
assert pr_call_args[1]['json']['title'] == 'Test PR'
|
|
assert pr_call_args[1]['json']['assignees'] == ['user1', 'user2']
|
|
|
|
# Verify the result
|
|
assert result == expected_result
|
|
|
|
@patch('requests.Session.post')
|
|
def test_create_pull_request_without_assignees(self, mock_post):
|
|
"""Test creating a pull request without assignees."""
|
|
# Mock the PR creation response
|
|
pr_response = MagicMock()
|
|
pr_response.status_code = 201
|
|
expected_result = {
|
|
'number': 123,
|
|
'title': 'Test PR',
|
|
'html_url': 'https://gitea.example.com/owner/repo/pulls/123',
|
|
}
|
|
pr_response.json.return_value = expected_result
|
|
mock_post.return_value = pr_response
|
|
|
|
# Call the method without assignees
|
|
result = self.client.create_pull_request(
|
|
owner='owner',
|
|
repo='repo',
|
|
title='Test PR',
|
|
body='Test body',
|
|
head='feature-branch',
|
|
base='main',
|
|
)
|
|
|
|
# Verify PR creation call
|
|
assert mock_post.call_count == 1
|
|
pr_call_args = mock_post.call_args_list[0]
|
|
json_data = pr_call_args[1]['json']
|
|
assert 'assignees' not in json_data
|
|
|
|
# Verify the result
|
|
assert result == expected_result
|
|
|
|
@patch('requests.Session.post')
|
|
def test_assign_reviewers_success(self, mock_post):
|
|
"""Test assigning reviewers to a pull request successfully."""
|
|
# Mock successful reviewer assignment response
|
|
reviewer_response = MagicMock()
|
|
reviewer_response.status_code = 201
|
|
mock_post.return_value = reviewer_response
|
|
|
|
# Call the method
|
|
result = self.client.assign_reviewers(
|
|
owner='owner',
|
|
repo='repo',
|
|
pull_number=123,
|
|
reviewers=['reviewer1', 'reviewer2'],
|
|
)
|
|
|
|
# Verify reviewer assignment call
|
|
assert mock_post.call_count == 1
|
|
reviewer_call_args = mock_post.call_args_list[0]
|
|
assert (
|
|
reviewer_call_args[0][0]
|
|
== 'https://gitea.example.com/api/v1/repos/owner/repo/pulls/123/requested_reviewers'
|
|
)
|
|
assert reviewer_call_args[1]['json']['reviewers'] == ['reviewer1', 'reviewer2']
|
|
|
|
# Verify the result
|
|
assert result is True
|
|
|
|
@patch('requests.Session.post')
|
|
def test_assign_reviewers_not_supported(self, mock_post):
|
|
"""Test assigning reviewers when API endpoint is not supported."""
|
|
# Mock 404 response (not supported)
|
|
reviewer_response = MagicMock()
|
|
reviewer_response.status_code = 404
|
|
mock_post.return_value = reviewer_response
|
|
|
|
# Call the method
|
|
result = self.client.assign_reviewers(
|
|
owner='owner',
|
|
repo='repo',
|
|
pull_number=123,
|
|
reviewers=['reviewer1'],
|
|
)
|
|
|
|
# Verify the result
|
|
assert result is False
|
|
|
|
def test_assign_reviewers_empty_list(self):
|
|
"""Test assigning reviewers with empty list returns True."""
|
|
result = self.client.assign_reviewers(
|
|
owner='owner',
|
|
repo='repo',
|
|
pull_number=123,
|
|
reviewers=[],
|
|
)
|
|
|
|
# Verify the result
|
|
assert result is True
|
|
|
|
@patch('requests.Session.post')
|
|
def test_assign_reviewers_none_list(self, mock_post):
|
|
"""Test assigning reviewers with None returns True."""
|
|
result = self.client.assign_reviewers(
|
|
owner='owner',
|
|
repo='repo',
|
|
pull_number=123,
|
|
reviewers=None,
|
|
)
|
|
|
|
# Verify no API call was made
|
|
assert mock_post.call_count == 0
|
|
|
|
# Verify the result
|
|
assert result is True
|