test_issue_handler_error_handling.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import pytest
  2. import requests
  3. from unittest.mock import patch, MagicMock
  4. from openhands.resolver.issue_definitions import PRHandler
  5. from openhands.resolver.github_issue import ReviewThread
  6. def test_handle_nonexistent_issue_reference():
  7. handler = PRHandler("test-owner", "test-repo", "test-token")
  8. # Mock the requests.get to simulate a 404 error
  9. mock_response = MagicMock()
  10. mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError("404 Client Error: Not Found")
  11. with patch('requests.get', return_value=mock_response):
  12. # Call the method with a non-existent issue reference
  13. result = handler._PRHandler__get_context_from_external_issues_references(
  14. closing_issues=[],
  15. closing_issue_numbers=[],
  16. issue_body="This references #999999", # Non-existent issue
  17. review_comments=[],
  18. review_threads=[],
  19. thread_comments=None
  20. )
  21. # The method should return an empty list since the referenced issue couldn't be fetched
  22. assert result == []
  23. def test_handle_rate_limit_error():
  24. handler = PRHandler("test-owner", "test-repo", "test-token")
  25. # Mock the requests.get to simulate a rate limit error
  26. mock_response = MagicMock()
  27. mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError(
  28. "403 Client Error: Rate Limit Exceeded"
  29. )
  30. with patch('requests.get', return_value=mock_response):
  31. # Call the method with an issue reference
  32. result = handler._PRHandler__get_context_from_external_issues_references(
  33. closing_issues=[],
  34. closing_issue_numbers=[],
  35. issue_body="This references #123",
  36. review_comments=[],
  37. review_threads=[],
  38. thread_comments=None
  39. )
  40. # The method should return an empty list since the request was rate limited
  41. assert result == []
  42. def test_handle_network_error():
  43. handler = PRHandler("test-owner", "test-repo", "test-token")
  44. # Mock the requests.get to simulate a network error
  45. with patch('requests.get', side_effect=requests.exceptions.ConnectionError("Network Error")):
  46. # Call the method with an issue reference
  47. result = handler._PRHandler__get_context_from_external_issues_references(
  48. closing_issues=[],
  49. closing_issue_numbers=[],
  50. issue_body="This references #123",
  51. review_comments=[],
  52. review_threads=[],
  53. thread_comments=None
  54. )
  55. # The method should return an empty list since the network request failed
  56. assert result == []
  57. def test_successful_issue_reference():
  58. handler = PRHandler("test-owner", "test-repo", "test-token")
  59. # Mock a successful response
  60. mock_response = MagicMock()
  61. mock_response.raise_for_status.return_value = None
  62. mock_response.json.return_value = {"body": "This is the referenced issue body"}
  63. with patch('requests.get', return_value=mock_response):
  64. # Call the method with an issue reference
  65. result = handler._PRHandler__get_context_from_external_issues_references(
  66. closing_issues=[],
  67. closing_issue_numbers=[],
  68. issue_body="This references #123",
  69. review_comments=[],
  70. review_threads=[],
  71. thread_comments=None
  72. )
  73. # The method should return a list with the referenced issue body
  74. assert result == ["This is the referenced issue body"]