-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…5062) Co-authored-by: Graham Neubig <[email protected]>
- Loading branch information
1 parent
a679fcc
commit 7074e45
Showing
3 changed files
with
157 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import pytest | ||
import requests | ||
from unittest.mock import patch, MagicMock | ||
|
||
from openhands.resolver.issue_definitions import PRHandler | ||
from openhands.resolver.github_issue import ReviewThread | ||
|
||
|
||
def test_handle_nonexistent_issue_reference(): | ||
handler = PRHandler("test-owner", "test-repo", "test-token") | ||
|
||
# Mock the requests.get to simulate a 404 error | ||
mock_response = MagicMock() | ||
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError("404 Client Error: Not Found") | ||
|
||
with patch('requests.get', return_value=mock_response): | ||
# Call the method with a non-existent issue reference | ||
result = handler._PRHandler__get_context_from_external_issues_references( | ||
closing_issues=[], | ||
closing_issue_numbers=[], | ||
issue_body="This references #999999", # Non-existent issue | ||
review_comments=[], | ||
review_threads=[], | ||
thread_comments=None | ||
) | ||
|
||
# The method should return an empty list since the referenced issue couldn't be fetched | ||
assert result == [] | ||
|
||
|
||
def test_handle_rate_limit_error(): | ||
handler = PRHandler("test-owner", "test-repo", "test-token") | ||
|
||
# Mock the requests.get to simulate a rate limit error | ||
mock_response = MagicMock() | ||
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError( | ||
"403 Client Error: Rate Limit Exceeded" | ||
) | ||
|
||
with patch('requests.get', return_value=mock_response): | ||
# Call the method with an issue reference | ||
result = handler._PRHandler__get_context_from_external_issues_references( | ||
closing_issues=[], | ||
closing_issue_numbers=[], | ||
issue_body="This references #123", | ||
review_comments=[], | ||
review_threads=[], | ||
thread_comments=None | ||
) | ||
|
||
# The method should return an empty list since the request was rate limited | ||
assert result == [] | ||
|
||
|
||
def test_handle_network_error(): | ||
handler = PRHandler("test-owner", "test-repo", "test-token") | ||
|
||
# Mock the requests.get to simulate a network error | ||
with patch('requests.get', side_effect=requests.exceptions.ConnectionError("Network Error")): | ||
# Call the method with an issue reference | ||
result = handler._PRHandler__get_context_from_external_issues_references( | ||
closing_issues=[], | ||
closing_issue_numbers=[], | ||
issue_body="This references #123", | ||
review_comments=[], | ||
review_threads=[], | ||
thread_comments=None | ||
) | ||
|
||
# The method should return an empty list since the network request failed | ||
assert result == [] | ||
|
||
|
||
def test_successful_issue_reference(): | ||
handler = PRHandler("test-owner", "test-repo", "test-token") | ||
|
||
# Mock a successful response | ||
mock_response = MagicMock() | ||
mock_response.raise_for_status.return_value = None | ||
mock_response.json.return_value = {"body": "This is the referenced issue body"} | ||
|
||
with patch('requests.get', return_value=mock_response): | ||
# Call the method with an issue reference | ||
result = handler._PRHandler__get_context_from_external_issues_references( | ||
closing_issues=[], | ||
closing_issue_numbers=[], | ||
issue_body="This references #123", | ||
review_comments=[], | ||
review_threads=[], | ||
thread_comments=None | ||
) | ||
|
||
# The method should return a list with the referenced issue body | ||
assert result == ["This is the referenced issue body"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from openhands.resolver.issue_definitions import IssueHandler | ||
|
||
|
||
def test_extract_issue_references(): | ||
handler = IssueHandler("test-owner", "test-repo", "test-token") | ||
|
||
# Test basic issue reference | ||
assert handler._extract_issue_references("Fixes #123") == [123] | ||
|
||
# Test multiple issue references | ||
assert handler._extract_issue_references("Fixes #123, #456") == [123, 456] | ||
|
||
# Test issue references in code blocks should be ignored | ||
assert handler._extract_issue_references(""" | ||
Here's a code block: | ||
```python | ||
# This is a comment with #123 | ||
def func(): | ||
pass # Another #456 | ||
``` | ||
But this #789 should be extracted | ||
""") == [789] | ||
|
||
# Test issue references in inline code should be ignored | ||
assert handler._extract_issue_references("This `#123` should be ignored but #456 should be extracted") == [456] | ||
|
||
# Test issue references in URLs should be ignored | ||
assert handler._extract_issue_references("Check http://example.com/#123 but #456 should be extracted") == [456] | ||
|
||
# Test issue references in markdown links should be extracted | ||
assert handler._extract_issue_references("[Link to #123](http://example.com) and #456") == [123, 456] | ||
|
||
# Test issue references with text around them | ||
assert handler._extract_issue_references("Issue #123 is fixed and #456 is pending") == [123, 456] |