25473ed684
- Create gitea/issues_client.py with IssuesClient class (9 methods) - Create gitea/prs_client.py with PullRequestsClient class (17 methods) - Create gitea/files_client.py with FilesClient class (4 methods) - Create gitea/notifications_client.py with NotificationsClient class (2 methods) - Create gitea/repos_client.py with ReposClient class (2 methods) - Create gitea/__init__.py to export all client classes - Remove delegation methods from GiteaClient (now ~70 lines) - Update all callers to use sub-clients (client.issues, client.prs, etc.) - Update test files to mock sub-client attributes GiteaClient is now a facade that provides access to focused sub-clients: - repos: Repository operations (ReposClient) - issues: Issue operations (IssuesClient) - prs: Pull request operations (PullRequestsClient) - files: File and git ref operations (FilesClient) - notifications: Notification operations (NotificationsClient) Refs: #godclass-refactor
160 lines
5.3 KiB
Python
160 lines
5.3 KiB
Python
from unittest.mock import MagicMock, patch
|
|
from gitea.client import GiteaClient
|
|
|
|
|
|
def test_gitea_client_list_repo_issues() -> None:
|
|
client: GiteaClient = GiteaClient()
|
|
with patch("httpx.Client.get") as mock_get:
|
|
mock_response: MagicMock = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json.return_value = []
|
|
mock_get.return_value = mock_response
|
|
|
|
# Test default parameter ("open")
|
|
client.issues.list_repo_issues("owner", "repo")
|
|
mock_get.assert_called_once()
|
|
args, _ = mock_get.call_args
|
|
assert "type=issues" in args[0]
|
|
assert "state=open" in args[0]
|
|
|
|
mock_get.reset_mock()
|
|
|
|
# Test custom parameter ("closed")
|
|
client.issues.list_repo_issues("owner", "repo", state="closed")
|
|
mock_get.assert_called_once()
|
|
args, _ = mock_get.call_args
|
|
assert "type=issues" in args[0]
|
|
assert "state=closed" in args[0]
|
|
|
|
|
|
def test_gitea_client_list_repo_pull_requests() -> None:
|
|
client: GiteaClient = GiteaClient()
|
|
with patch("httpx.Client.get") as mock_get:
|
|
mock_response: MagicMock = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json.return_value = []
|
|
mock_get.return_value = mock_response
|
|
|
|
# Test default parameter ("open")
|
|
client.prs.list_repo_pull_requests("owner", "repo")
|
|
mock_get.assert_called_once()
|
|
args, _ = mock_get.call_args
|
|
assert "state=open" in args[0]
|
|
|
|
mock_get.reset_mock()
|
|
|
|
# Test custom parameter ("closed")
|
|
client.prs.list_repo_pull_requests("owner", "repo", state="closed")
|
|
mock_get.assert_called_once()
|
|
args, _ = mock_get.call_args
|
|
assert "state=closed" in args[0]
|
|
|
|
|
|
def test_gitea_client_list_assigned_issues() -> None:
|
|
client: GiteaClient = GiteaClient()
|
|
user_mock: MagicMock = MagicMock()
|
|
user_mock.login = "testuser"
|
|
|
|
with (
|
|
patch.object(client.repos, "get_authenticated_user", return_value=user_mock),
|
|
patch.object(client.issues, "_get_user", return_value=user_mock),
|
|
patch("httpx.Client.get") as mock_get,
|
|
):
|
|
mock_response: MagicMock = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json.return_value = []
|
|
mock_get.return_value = mock_response
|
|
|
|
client.issues.list_assigned_issues("owner", "repo")
|
|
mock_get.assert_called_once()
|
|
args, _ = mock_get.call_args
|
|
assert "type=issues" in args[0]
|
|
assert "state=open" in args[0]
|
|
|
|
|
|
def test_gitea_client_list_assigned_pull_requests() -> None:
|
|
client: GiteaClient = GiteaClient()
|
|
user_mock: MagicMock = MagicMock()
|
|
user_mock.login = "testuser"
|
|
|
|
with (
|
|
patch.object(client.repos, "get_authenticated_user", return_value=user_mock),
|
|
patch.object(client.prs, "_get_user", return_value=user_mock),
|
|
patch("httpx.Client.get") as mock_get,
|
|
):
|
|
mock_response: MagicMock = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json.return_value = [
|
|
{
|
|
"number": 1,
|
|
"title": "PR 1",
|
|
"assignee": {"login": "testuser"},
|
|
"user": {"login": "otheruser"},
|
|
},
|
|
{
|
|
"number": 2,
|
|
"title": "PR 2",
|
|
"assignee": None,
|
|
"user": {"login": "testuser"},
|
|
},
|
|
{
|
|
"number": 3,
|
|
"title": "PR 3",
|
|
"assignee": {"login": "otheruser"},
|
|
"user": {"login": "otheruser"},
|
|
},
|
|
]
|
|
mock_get.return_value = mock_response
|
|
|
|
res = client.prs.list_assigned_pull_requests("owner", "repo")
|
|
mock_get.assert_called_once()
|
|
assert len(res) == 2
|
|
numbers = [pr.number for pr in res]
|
|
assert 1 in numbers
|
|
assert 2 in numbers
|
|
assert 3 not in numbers
|
|
|
|
|
|
def test_gitea_client_list_unread_notifications() -> None:
|
|
client: GiteaClient = GiteaClient()
|
|
with patch("httpx.Client.get") as mock_get:
|
|
mock_response: MagicMock = MagicMock()
|
|
mock_response.status_code = 200
|
|
mock_response.json.return_value = [
|
|
{"id": 1, "repository": {"owner": {"login": "meeks"}}},
|
|
{"id": 2, "repository": {"owner": {"login": "other"}}},
|
|
]
|
|
mock_get.return_value = mock_response
|
|
|
|
# Test without since
|
|
res = client.notifications.list_unread_notifications()
|
|
mock_get.assert_called_once()
|
|
_, kwargs = mock_get.call_args
|
|
assert kwargs.get("params") == {"all": "false"}
|
|
assert len(res) == 1
|
|
assert res[0]["id"] == 1
|
|
|
|
mock_get.reset_mock()
|
|
|
|
# Test with since
|
|
res = client.notifications.list_unread_notifications(
|
|
since="2026-06-30T21:41:16+02:00"
|
|
)
|
|
mock_get.assert_called_once()
|
|
_, kwargs = mock_get.call_args
|
|
assert kwargs.get("params") == {
|
|
"all": "false",
|
|
"since": "2026-06-30T21:41:16+02:00",
|
|
}
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
def test_gitea_client_get_authenticated_user_failure() -> None:
|
|
client: GiteaClient = GiteaClient()
|
|
with patch("httpx.Client.get") as mock_get:
|
|
mock_get.side_effect = Exception("Connection error")
|
|
with pytest.raises(RuntimeError, match="Could not get authenticated user"):
|
|
client.repos.get_authenticated_user()
|