"""GitHub 클라이언트 단위 테스트."""
import pytest
from unittest.mock import patch, Mock
from mcp_github.github_client import GitHubClient
class TestGitHubClient:
"""GitHub 클라이언트 테스트 클래스."""
def test_init_with_token(self, mock_env_vars):
"""토큰으로 클라이언트 초기화 테스트."""
with patch('mcp_github.github_client.Github') as mock_github:
client = GitHubClient()
mock_github.assert_called_once_with("test-token", per_page=100)
def test_init_without_token(self, monkeypatch):
"""토큰 없이 클라이언트 초기화 시 에러 테스트."""
monkeypatch.delenv("GITHUB_TOKEN", raising=False)
# GitHubClient는 __init__에서 토큰을 확인하지 않음
# 실제 API 호출 시점에 에러가 발생
with patch('mcp_github.github_client.Github') as mock_github:
mock_github.side_effect = Exception("Invalid token")
# 초기화 실패를 예상하고 테스트
with pytest.raises(Exception) as exc_info:
GitHubClient()
assert "Invalid token" in str(exc_info.value)
def test_get_repository_success(self, mock_github_client):
"""레포지토리 가져오기 성공 테스트."""
with patch('mcp_github.github_client.Github') as mock_github:
mock_github_instance = Mock()
mock_github_instance.get_repo.return_value = mock_github_client.get_repository.return_value
mock_github.return_value = mock_github_instance
client = GitHubClient()
repo = client.get_repository("test-owner", "test-repo")
assert repo.full_name == "test-owner/test-repo"
assert repo.description == "Test repository for MCP server"
assert repo.language == "Python"
def test_get_repository_error(self):
"""레포지토리 가져오기 실패 테스트."""
with patch('mcp_github.github_client.Github') as mock_github:
mock_github_instance = Mock()
mock_github_instance.get_repo.side_effect = Exception("Repository not found")
mock_github.return_value = mock_github_instance
client = GitHubClient()
with pytest.raises(Exception, match="Repository not found"):
client.get_repository("invalid-owner", "invalid-repo")
def test_connection_error_handling(self):
"""연결 에러 처리 테스트."""
with patch('mcp_github.github_client.Github') as mock_github:
mock_github.side_effect = Exception("Connection failed")
with pytest.raises(Exception, match="Connection failed"):
GitHubClient()
def test_token_validation(self):
"""토큰 유효성 검사 테스트."""
with patch('mcp_github.github_client.Github') as mock_github:
mock_github_instance = Mock()
mock_github_instance.get_user.side_effect = Exception("Invalid token")
mock_github.return_value = mock_github_instance
client = GitHubClient()
# 토큰 유효성 검사는 실제로는 GitHub API 호출 시점에 발생
# 여기서는 기본적인 초기화만 테스트
assert client.github is not None