test_issue_handler_error_handling.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from unittest.mock import MagicMock, patch
  2. import requests
  3. from openhands.resolver.issue_definitions import PRHandler
  4. def test_handle_nonexistent_issue_reference():
  5. handler = PRHandler('test-owner', 'test-repo', 'test-token')
  6. # Mock the requests.get to simulate a 404 error
  7. mock_response = MagicMock()
  8. mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError(
  9. '404 Client Error: Not Found'
  10. )
  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(
  46. 'requests.get', side_effect=requests.exceptions.ConnectionError('Network Error')
  47. ):
  48. # Call the method with an issue reference
  49. result = handler._PRHandler__get_context_from_external_issues_references(
  50. closing_issues=[],
  51. closing_issue_numbers=[],
  52. issue_body='This references #123',
  53. review_comments=[],
  54. review_threads=[],
  55. thread_comments=None,
  56. )
  57. # The method should return an empty list since the network request failed
  58. assert result == []
  59. def test_successful_issue_reference():
  60. handler = PRHandler('test-owner', 'test-repo', 'test-token')
  61. # Mock a successful response
  62. mock_response = MagicMock()
  63. mock_response.raise_for_status.return_value = None
  64. mock_response.json.return_value = {'body': 'This is the referenced issue body'}
  65. with patch('requests.get', return_value=mock_response):
  66. # Call the method with an issue reference
  67. result = handler._PRHandler__get_context_from_external_issues_references(
  68. closing_issues=[],
  69. closing_issue_numbers=[],
  70. issue_body='This references #123',
  71. review_comments=[],
  72. review_threads=[],
  73. thread_comments=None,
  74. )
  75. # The method should return a list with the referenced issue body
  76. assert result == ['This is the referenced issue body']