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