Skip to content

Commit

Permalink
feat: add comprehensive unit tests for handle_even
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] committed Apr 8, 2024
1 parent cbbf580 commit b92f5c2
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions sweepai/tests/api_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import unittest
from unittest.mock import MagicMock, patch

from sweepai.api import handle_event


class TestHandleEvent(unittest.TestCase):
def test_handle_event_disabled_repo(self):
request_dict = {"repository": {"full_name": "sweepai/disabled_repo"}}
event = "check_run"
expected_response = {"success": False, "error_message": "Repo is disabled"}
response = handle_event(request_dict, event)
self.assertEqual(response, expected_response)

@patch("sweepai.api.get_github_client")
@patch("sweepai.api.logger")
def test_handle_event_check_run_completed(self, mock_logger, mock_get_github_client):
mock_client = MagicMock()
mock_repo = MagicMock()
mock_pull = MagicMock()
mock_get_github_client.return_value = (None, mock_client)
mock_client.get_repo.return_value = mock_repo
mock_repo.get_pull.return_value = mock_pull
mock_pull.created_at.timestamp.return_value = 100
request_dict = {"action": "completed", "repository": {"full_name": "sweepai/test_repo"}, "installation": {"id": 1}, "check_run": {"pull_requests": [{"number": 1}]}}
event = "check_run"
response = handle_event(request_dict, event)
mock_get_github_client.assert_called_once()
mock_repo.get_pull.assert_called_once_with(1)
self.assertIn("success", response)

@patch("sweepai.api.get_rules")
@patch("sweepai.api.logger")
def test_handle_event_pull_request_opened(self, mock_logger, mock_get_rules):
mock_get_rules.return_value = ["rule1", "rule2"]
request_dict = {"action": "opened", "repository": {"full_name": "sweepai/test_repo"}, "pull_request": {"number": 1}}
event = "pull_request"
response = handle_event(request_dict, event)
mock_get_rules.assert_called_once()
self.assertIn("success", response)

@patch("sweepai.api.get_github_client")
@patch("sweepai.api.logger")
def test_handle_event_error_cases(self, mock_logger, mock_get_github_client):
mock_get_github_client.side_effect = Exception("GitHub API error")
request_dict = {"action": "completed", "repository": {"full_name": "sweepai/test_repo"}, "installation": {"id": 1}, "check_run": {"pull_requests": [{"number": 1}]}}
event = "check_run"
with self.assertRaises(Exception):
handle_event(request_dict, event)
mock_get_github_client.assert_called_once()

if __name__ == '__main__':
unittest.main()

0 comments on commit b92f5c2

Please sign in to comment.