33import subprocess
44
55import pytest
6+ from jira .exceptions import JIRAError
67from pyhelper_utils .shell import run_command
78from simple_logger .logger import get_logger
89
@@ -73,64 +74,128 @@ def test_process_jira_command_line_config_file_valid_config(mocker):
7374
7475
7576@pytest .mark .parametrize (
76- "jira_id, resolved_status, jira_target_versions, target_version_str, file_name, "
77- "skip_project_ids, expected_jira_error_string, "
78- "test_jira_version" ,
77+ "test_params" ,
7978 [
80- # Test case 1: Issue with no jira target versions and not resolved status
81- ("issue1" , ["resolved" ], [], "1.0" , "file1.txt" , [], "" , "1.0" ),
82- # Test case 2: Issue with no jira target versions, but resolved status
83- (
84- "issue2" ,
85- ["open" ],
86- [],
87- "1.0" ,
88- "file2.txt" ,
89- [],
90- "issue2 current status: open is resolved." ,
91- "1.0" ,
92- ),
93- # Test case 3: Issue with no jira target versions, default resolved status
94- ("issue3" , [], [], "" , "file3.txt" , [], "" , "1.1" ),
95- # Test case 4: Issue with not resolved state, but matching jira target version
96- ("issue4" , ["resolved" ], ["1.0" ], "1.0" , "file4.txt" , [], "" , "1.0" ),
97- # Test case 5: Issue with not resolved state, and not matching jira target version
98- (
99- "issue5" ,
100- ["resolved" ],
101- ["1.1" ],
102- "1.0" ,
103- "file5.txt" ,
104- [],
105- "issue5 target version: 1.0, does not match expected version ['1.1']." ,
106- "1.0" ,
107- ),
108- # Test case 6: Issue that would be skipped for version check because of skip
109- ("issue6" , ["resolved" ], ["1.0" ], "1.0" , "file6.txt" , ["issue" ], "" , "1.1" ),
110- # Test case 7: Issue that would be skipped for version check but fail resolved check
111- (
112- "issue7" ,
113- ["open" ],
114- ["1.0" ],
115- "1.0" ,
116- "file6.txt" ,
117- ["issue" ],
118- "issue7 current status: open is resolved." ,
119- "1.1" ,
120- ),
121- # Test case 8: Issue with unresolved state, and matching jira z target version
122- ("issue8" , [], ["1.2.z" ], "1.2,z" , "file4.txt" , [], "" , "1.2.z" ),
123- # Test case 9: Issue with unresolved state, and jira z target version not matching expected versions
124- (
125- "issue8" ,
126- [],
127- ["1.2.3" ],
128- "" ,
129- "file4.txt" ,
130- [],
131- "issue8 target version: 1.2.z, does not match expected version ['1.2.3']." ,
132- "1.2.z" ,
133- ),
79+ { # Test case 1: Issue with no jira target versions and not resolved status
80+ "jira_id" : "issue1" ,
81+ "resolved_status" : ["resolved" ],
82+ "jira_target_versions" : [],
83+ "target_version_str" : "1.0" ,
84+ "file_name" : "file1.txt" ,
85+ "skip_project_ids" : [],
86+ "expected_jira_error_string" : "" ,
87+ "test_jira_versions" : ["1.0" ],
88+ },
89+ { # Test case 2: Issue with no jira target versions, but resolved status
90+ "jira_id" : "issue2" ,
91+ "resolved_status" : ["open" ],
92+ "jira_target_versions" : [],
93+ "target_version_str" : "1.0" ,
94+ "file_name" : "file2.txt" ,
95+ "skip_project_ids" : [],
96+ "expected_jira_error_string" : "issue2 current status: open is resolved." ,
97+ "test_jira_versions" : ["1.0" ],
98+ },
99+ { # Test case 3: Issue with no jira target versions, default resolved status
100+ "jira_id" : "issue3" ,
101+ "resolved_status" : [],
102+ "jira_target_versions" : [],
103+ "target_version_str" : "" ,
104+ "file_name" : "file3.txt" ,
105+ "skip_project_ids" : [],
106+ "expected_jira_error_string" : "" ,
107+ "test_jira_versions" : ["1.1" ],
108+ },
109+ { # Test case 4: Issue with not resolved state, but matching jira target version
110+ "jira_id" : "issue4" ,
111+ "resolved_status" : ["resolved" ],
112+ "jira_target_versions" : ["1.0" ],
113+ "target_version_str" : "1.0" ,
114+ "file_name" : "file4.txt" ,
115+ "skip_project_ids" : [],
116+ "expected_jira_error_string" : "" ,
117+ "test_jira_versions" : ["1.0" ],
118+ },
119+ { # Test case 5: Issue with not resolved state, and not matching jira target version
120+ "jira_id" : "issue5" ,
121+ "resolved_status" : ["resolved" ],
122+ "jira_target_versions" : ["1.1" ],
123+ "target_version_str" : "1.0" ,
124+ "file_name" : "file5.txt" ,
125+ "skip_project_ids" : [],
126+ "expected_jira_error_string" : "issue5 target versions: ['1.0'], do not match expected version ['1.1']." ,
127+ "test_jira_versions" : ["1.0" ],
128+ },
129+ { # Test case 6: Issue that would be skipped for version check because of skip
130+ "jira_id" : "issue6" ,
131+ "resolved_status" : ["resolved" ],
132+ "jira_target_versions" : ["1.0" ],
133+ "target_version_str" : "1.0" ,
134+ "file_name" : "file6.txt" ,
135+ "skip_project_ids" : ["issue" ],
136+ "expected_jira_error_string" : "" ,
137+ "test_jira_versions" : ["1.1" ],
138+ },
139+ { # Test case 7: Issue that would be skipped for version check but fail resolved check
140+ "jira_id" : "issue7" ,
141+ "resolved_status" : ["open" ],
142+ "jira_target_versions" : ["1.0" ],
143+ "target_version_str" : "1.0" ,
144+ "file_name" : "file6.txt" ,
145+ "skip_project_ids" : ["issue" ],
146+ "expected_jira_error_string" : "issue7 current status: open is resolved." ,
147+ "test_jira_versions" : ["1.1" ],
148+ },
149+ { # Test case 8: Issue with unresolved state, and matching jira z target version
150+ "jira_id" : "issue8" ,
151+ "resolved_status" : [],
152+ "jira_target_versions" : ["1.2.z" ],
153+ "target_version_str" : "1.2.z" ,
154+ "file_name" : "file4.txt" ,
155+ "skip_project_ids" : [],
156+ "expected_jira_error_string" : "" ,
157+ "test_jira_versions" : ["1.2.z" ],
158+ },
159+ { # Test case 9: Issue with unresolved state, and jira z target version not matching expected versions
160+ "jira_id" : "issue9" ,
161+ "resolved_status" : [],
162+ "jira_target_versions" : ["1.2.3" ],
163+ "target_version_str" : "1.2.z" ,
164+ "file_name" : "file4.txt" ,
165+ "skip_project_ids" : [],
166+ "expected_jira_error_string" : "issue9 target versions: ['1.2.z'], do not match expected version ['1.2.3']." ,
167+ "test_jira_versions" : ["1.2.z" ],
168+ },
169+ { # Test case 10: Issue with unresolved state, and matching jira z target versions
170+ "jira_id" : "issue10" ,
171+ "resolved_status" : [],
172+ "jira_target_versions" : ["1.2.z" , "1.3.z" ],
173+ "target_version_str" : "" ,
174+ "file_name" : "file4.txt" ,
175+ "skip_project_ids" : [],
176+ "expected_jira_error_string" : "" ,
177+ "test_jira_versions" : ["1.2.z" , "1.4.0" ],
178+ },
179+ { # Test case 11: Issue with unresolved state, and jira target version not matching expected versions
180+ "jira_id" : "issue11" ,
181+ "resolved_status" : [],
182+ "jira_target_versions" : ["1.2.3" ],
183+ "target_version_str" : "" ,
184+ "file_name" : "file4.txt" ,
185+ "skip_project_ids" : [],
186+ "expected_jira_error_string" : "issue11 target versions: ['1.2.z', '1.4.0'], do not match expected version ['1.2.3']." ,
187+ "test_jira_versions" : ["1.2.z" , "1.4.0" ],
188+ },
189+ { # Test case 12: Issue with unresolved state, and jira target versions not matching expected versions
190+ "jira_id" : "issue12" ,
191+ "resolved_status" : [],
192+ "jira_target_versions" : ["1.2.3" , "1.5.0" ],
193+ "target_version_str" : "" ,
194+ "file_name" : "file4.txt" ,
195+ "skip_project_ids" : [],
196+ "expected_jira_error_string" : "issue12 target versions: ['1.2.z', '1.4.0'], do not match expected version ['1.2.3', '1.5.0']." ,
197+ "test_jira_versions" : ["1.2.z" , "1.4.0" ],
198+ },
134199 ],
135200 ids = [
136201 "test_no_jira_versions_no_resolved_status" ,
@@ -142,51 +207,41 @@ def test_process_jira_command_line_config_file_valid_config(mocker):
142207 "test_skip_version_check_fail_status_check" ,
143208 "test_matching_target_z_version" ,
144209 "test_non_matching_target_z_version" ,
210+ "test_matching_target_version_with_versions" ,
211+ "test_non_matching_target_version_with_versions" ,
212+ "test_matching_target_versions_with_versions" ,
145213 ],
146214)
147- def test_get_jira_information (
148- mocker ,
149- jira_id ,
150- resolved_status ,
151- jira_target_versions ,
152- target_version_str ,
153- file_name ,
154- skip_project_ids ,
155- expected_jira_error_string ,
156- test_jira_version ,
157- ):
215+ def test_get_jira_information (mocker , test_params ):
158216 mock_jira = mocker .MagicMock ()
159217 mock_jira .fields .status .name = "open"
160- jira_version = mocker .MagicMock ()
161- jira_version .name = test_jira_version
162- mock_jira .fixVersions = [jira_version ]
218+ mock_jira .fixVersions = [mocker .MagicMock ()]
163219 mocker .patch ("apps.jira_utils.jira_information.get_issue" , return_value = mock_jira )
164220
165- if jira_target_versions :
221+ if test_jira_versions := test_params . get ( "test_jira_versions" ) :
166222 mocker .patch (
167223 "apps.jira_utils.jira_information.re.findall" ,
168- return_value = [test_jira_version ],
169- )
170- result = get_jira_information (
171- jira_object = mock_jira ,
172- jira_id = jira_id ,
173- resolved_status = resolved_status ,
174- target_version_str = "1.0" ,
175- skip_project_ids = skip_project_ids ,
176- jira_target_versions = jira_target_versions ,
177- file_name = file_name ,
178- )
179- else :
180- result = get_jira_information (
181- jira_object = mock_jira ,
182- jira_id = jira_id ,
183- resolved_status = resolved_status ,
184- target_version_str = "1.0" ,
185- skip_project_ids = skip_project_ids ,
186- jira_target_versions = jira_target_versions ,
187- file_name = file_name ,
224+ return_value = test_jira_versions ,
188225 )
189226
227+ jira_id = test_params .get ("jira_id" )
228+ resolved_status = test_params .get ("resolved_status" )
229+ target_version_str = test_params .get ("target_version_str" )
230+ skip_project_ids = test_params .get ("skip_project_ids" )
231+ jira_target_versions = test_params .get ("jira_target_versions" )
232+ file_name = test_params .get ("file_name" )
233+ expected_jira_error_string = test_params .get ("expected_jira_error_string" )
234+
235+ result = get_jira_information (
236+ jira_object = mock_jira ,
237+ jira_id = jira_id ,
238+ resolved_status = resolved_status ,
239+ target_version_str = target_version_str ,
240+ skip_project_ids = skip_project_ids ,
241+ jira_target_versions = jira_target_versions ,
242+ file_name = file_name ,
243+ )
244+
190245 assert result == (file_name , expected_jira_error_string )
191246
192247
@@ -232,3 +287,22 @@ def test_get_jira_ids_from_file_content(content_and_expected):
232287 jira_url = "https://example.com" ,
233288 )
234289 assert jira_ids == content_and_expected ["expected" ]
290+
291+
292+ def test_jira_api_error (mocker ):
293+ mock_jira = mocker .MagicMock ()
294+ mocker .patch (
295+ "apps.jira_utils.jira_information.get_issue" , side_effect = JIRAError (status_code = 404 , text = "Issue not found" )
296+ )
297+
298+ result = get_jira_information (
299+ jira_object = mock_jira ,
300+ jira_id = "404" ,
301+ resolved_status = [],
302+ target_version_str = "" ,
303+ skip_project_ids = [],
304+ jira_target_versions = [],
305+ file_name = "" ,
306+ )
307+
308+ assert result [1 ] == "404 JiraError status code: 404, details: Issue not found]."
0 commit comments