Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
from unittest.mock import MagicMock
|
||||
from gitea.client import GiteaClient
|
||||
from gitea.tools.file_tools import FileTools
|
||||
|
||||
|
||||
def test_get_file_content_string_success() -> None:
|
||||
mock_client: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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.get_file_content.assert_called_once_with("owner", "repo", "path/to/file")
|
||||
|
||||
|
||||
def test_get_file_content_list_success() -> None:
|
||||
mock_client: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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.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: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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.update_file.assert_called_once_with("owner", "repo", "path/to/file", "msg", "content", "branch")
|
||||
|
||||
|
||||
def test_commit_file_failure() -> None:
|
||||
mock_client: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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.update_file.assert_called_once_with("owner", "repo", "path/to/file", "msg", "content", "branch")
|
||||
|
||||
|
||||
def test_update_file_failure() -> None:
|
||||
mock_client: MagicMock = MagicMock(spec=GiteaClient)
|
||||
mock_client.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
|
||||
Reference in New Issue
Block a user