e91780169e
- 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
127 lines
4.3 KiB
Python
127 lines
4.3 KiB
Python
from unittest.mock import MagicMock
|
|
from gitea.client import GiteaClient
|
|
from gitea.tools.file_tools import FileTools
|
|
|
|
|
|
def _create_mock_client() -> MagicMock:
|
|
"""Create a mock GiteaClient with sub-client attributes."""
|
|
mock_client: MagicMock = MagicMock(spec=GiteaClient)
|
|
mock_client.files = MagicMock()
|
|
return mock_client
|
|
|
|
|
|
def test_get_file_content_string_success() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.get_file_content.return_value = "file content here"
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.get_file_content("owner", "repo", "path/to/file")
|
|
assert res == "1: file content here"
|
|
mock_client.files.get_file_content.assert_called_once_with(
|
|
"owner", "repo", "path/to/file"
|
|
)
|
|
|
|
|
|
def test_get_file_content_list_success() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.get_file_content.return_value = ["line1", "line2"]
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.get_file_content("owner", "repo", "path/to/file")
|
|
assert res == "1: line1\n2: line2"
|
|
|
|
|
|
def test_get_file_content_failure() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.get_file_content.side_effect = Exception("API Error")
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.get_file_content("owner", "repo", "path/to/file")
|
|
assert "Error getting file content: API Error" in res
|
|
|
|
|
|
def test_get_file_content_with_ref_string_success() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.get_file_content.return_value = "file content here"
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.get_file_content_with_ref(
|
|
"owner", "repo", "path/to/file", "main"
|
|
)
|
|
assert res == "1: file content here"
|
|
mock_client.files.get_file_content.assert_called_once_with(
|
|
"owner", "repo", "path/to/file", "main"
|
|
)
|
|
|
|
|
|
def test_get_file_content_with_ref_list_success() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.get_file_content.return_value = ["line1", "line2"]
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.get_file_content_with_ref(
|
|
"owner", "repo", "path/to/file", "main"
|
|
)
|
|
assert res == "1: line1\n2: line2"
|
|
|
|
|
|
def test_get_file_content_with_ref_failure() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.get_file_content.side_effect = Exception("API Error")
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.get_file_content_with_ref(
|
|
"owner", "repo", "path/to/file", "main"
|
|
)
|
|
assert "Error getting file content: API Error" in res
|
|
|
|
|
|
def test_commit_file_success() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.update_file.return_value = {}
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.commit_file(
|
|
"owner", "repo", "path/to/file", "msg", "content", "branch"
|
|
)
|
|
assert "committed successfully" in res
|
|
mock_client.files.update_file.assert_called_once_with(
|
|
"owner", "repo", "path/to/file", "msg", "content", "branch"
|
|
)
|
|
|
|
|
|
def test_commit_file_failure() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.update_file.side_effect = Exception("API Error")
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.commit_file(
|
|
"owner", "repo", "path/to/file", "msg", "content", "branch"
|
|
)
|
|
assert "Error committing file: API Error" in res
|
|
|
|
|
|
def test_update_file_success() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.update_file.return_value = {}
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.update_file(
|
|
"owner", "repo", "path/to/file", "msg", "content", "branch"
|
|
)
|
|
assert "updated in" in res
|
|
mock_client.files.update_file.assert_called_once_with(
|
|
"owner", "repo", "path/to/file", "msg", "content", "branch"
|
|
)
|
|
|
|
|
|
def test_update_file_failure() -> None:
|
|
mock_client = _create_mock_client()
|
|
mock_client.files.update_file.side_effect = Exception("API Error")
|
|
|
|
file_tools: FileTools = FileTools(mock_client)
|
|
res: str = file_tools.update_file(
|
|
"owner", "repo", "path/to/file", "msg", "content", "branch"
|
|
)
|
|
assert "Error updating file: API Error" in res
|