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