refactor: replace responses with monkeypatch for testing gitea client

This commit is contained in:
Jon Michael Aanes (aider) 2025-04-15 00:21:15 +02:00
parent fc69e8d811
commit 49fa94d717

View File

@ -1,5 +1,5 @@
import pytest import pytest
import responses from unittest.mock import MagicMock
from aider_gitea.gitea_client import GiteaClient from aider_gitea.gitea_client import GiteaClient
@ -12,8 +12,7 @@ class TestGiteaClient:
self.owner = "test_owner" self.owner = "test_owner"
self.repo = "test_repo" self.repo = "test_repo"
@responses.activate def test_get_pull_request_comments(self, monkeypatch):
def test_get_pull_request_comments(self):
"""Test retrieving comments for a pull request.""" """Test retrieving comments for a pull request."""
pull_number = 123 pull_number = 123
expected_comments = [ expected_comments = [
@ -31,19 +30,21 @@ class TestGiteaClient:
}, },
] ]
# Mock the API response # Create a mock response
url = f"{self.gitea_url}/api/v1/repos/{self.owner}/{self.repo}/pulls/{pull_number}/comments" mock_response = MagicMock()
responses.add( mock_response.json.return_value = expected_comments
responses.GET, mock_response.raise_for_status = MagicMock()
url,
json=expected_comments, # Mock the session.get method
status=200, mock_get = MagicMock(return_value=mock_response)
) monkeypatch.setattr(self.client.session, "get", mock_get)
# Call the method # Call the method
comments = self.client.get_pull_request_comments(self.owner, self.repo, pull_number) comments = self.client.get_pull_request_comments(self.owner, self.repo, pull_number)
# Verify the result # Verify the result
assert comments == expected_comments assert comments == expected_comments
assert len(responses.calls) == 1
assert responses.calls[0].request.url == url # Verify the correct URL was called
expected_url = f"{self.gitea_url}/repos/{self.owner}/{self.repo}/pulls/{pull_number}/comments"
mock_get.assert_called_once_with(expected_url)