6
6
from unittest .mock import Mock , patch
7
7
import tempfile
8
8
import os
9
+ import sys
9
10
from atlassian .errors import ApiError
10
11
11
12
# Mock the atlassian import to avoid real network calls
16
17
"atlassian.errors" : Mock (),
17
18
},
18
19
):
20
+ # Import modules after mocking
19
21
from src .config import JiraConfig , AppConfig # noqa
20
22
from src .models import JiraIssueData , JiraSubtaskData , CSVRow
21
23
from src .csv_processor import CSVProcessor
@@ -214,10 +216,9 @@ class TestJiraClient(unittest.TestCase):
214
216
215
217
def setUp (self ):
216
218
"""Set up test fixtures."""
217
- self .jira_patcher = patch ("src.jira_client.Jira" )
218
- self .mock_jira_class = self .jira_patcher .start ()
219
- self .mock_jira_instance = self .mock_jira_class .return_value
220
-
219
+ # Don't patch during setup to avoid PyO3 issues
220
+ # We'll patch in individual test methods instead
221
+
221
222
with patch .dict (
222
223
os .environ ,
223
224
{
@@ -226,81 +227,93 @@ def setUp(self):
226
227
"JIRA_API_TOKEN" : "test-token" ,
227
228
},
228
229
):
230
+ # Import JiraClient here to avoid PyO3 issues
231
+ from src .jira_client import JiraClient
229
232
self .client = JiraClient ()
230
- self .client .jira = self .mock_jira_instance
231
-
232
- def tearDown (self ):
233
- """Tear down test fixtures."""
234
- self .jira_patcher .stop ()
235
233
236
234
def test_test_connection_success (self ):
237
235
"""Test successful connection test."""
238
- self .mock_jira_instance .server_info .return_value = {
239
- "serverTitle" : "Test Server"
240
- }
241
- self .assertTrue (self .client .test_connection ())
236
+ with patch ("src.jira_client.Jira" ) as mock_jira_class :
237
+ mock_jira_instance = mock_jira_class .return_value
238
+ mock_jira_instance .server_info .return_value = {
239
+ "serverTitle" : "Test Server"
240
+ }
241
+ self .client .jira = mock_jira_instance
242
+ self .assertTrue (self .client .test_connection ())
242
243
243
244
def test_test_connection_failure (self ):
244
245
"""Test failed connection test."""
245
- self .mock_jira_instance .server_info .side_effect = Exception (
246
- "Connection failed"
247
- )
248
- self .assertFalse (self .client .test_connection ())
246
+ with patch ("src.jira_client.Jira" ) as mock_jira_class :
247
+ mock_jira_instance = mock_jira_class .return_value
248
+ mock_jira_instance .server_info .side_effect = Exception (
249
+ "Connection failed"
250
+ )
251
+ self .client .jira = mock_jira_instance
252
+ self .assertFalse (self .client .test_connection ())
249
253
250
254
def test_create_issue_success (self ):
251
255
"""Test successful issue creation."""
252
- self .mock_jira_instance .issue_create .return_value = {
253
- "key" : "TEST-123" ,
254
- "id" : "12345" ,
255
- }
256
-
257
- issue_data = JiraIssueData (
258
- project_key = "TEST" ,
259
- summary = "Test Issue" ,
260
- description = "Test Description" ,
261
- issue_type = "Task" ,
262
- )
263
- created_issue = self .client .create_issue (issue_data .model_dump ())
256
+ with patch ("src.jira_client.Jira" ) as mock_jira_class :
257
+ mock_jira_instance = mock_jira_class .return_value
258
+ mock_jira_instance .issue_create .return_value = {
259
+ "key" : "TEST-123" ,
260
+ "id" : "12345" ,
261
+ }
262
+ self .client .jira = mock_jira_instance
263
+
264
+ issue_data = JiraIssueData (
265
+ project_key = "TEST" ,
266
+ summary = "Test Issue" ,
267
+ description = "Test Description" ,
268
+ issue_type = "Task" ,
269
+ )
270
+ created_issue = self .client .create_issue (issue_data .model_dump ())
264
271
265
- self .assertIsNotNone (created_issue )
266
- self .assertEqual (created_issue ["key" ], "TEST-123" )
267
- self . mock_jira_instance .issue_create .assert_called_once ()
272
+ self .assertIsNotNone (created_issue )
273
+ self .assertEqual (created_issue ["key" ], "TEST-123" )
274
+ mock_jira_instance .issue_create .assert_called_once ()
268
275
269
276
def test_create_issue_failure (self ):
270
277
"""Test failed issue creation."""
271
- # Create a mock ApiError that inherits from Exception
272
- mock_api_error = Exception ("Server Error" )
273
- mock_api_error .status_code = 500
274
- self .mock_jira_instance .issue_create .side_effect = mock_api_error
275
-
276
- issue_data = JiraIssueData (
277
- project_key = "TEST" ,
278
- summary = "Test Issue" ,
279
- description = "Test Description" ,
280
- issue_type = "Task" ,
281
- )
282
- with self .assertRaises (Exception ):
283
- self .client .create_issue (issue_data .model_dump ())
278
+ with patch ("src.jira_client.Jira" ) as mock_jira_class :
279
+ mock_jira_instance = mock_jira_class .return_value
280
+ # Create a mock exception with status_code
281
+ mock_exception = Exception ("Server Error" )
282
+ mock_exception .status_code = 500
283
+ mock_jira_instance .issue_create .side_effect = mock_exception
284
+ self .client .jira = mock_jira_instance
285
+
286
+ issue_data = JiraIssueData (
287
+ project_key = "TEST" ,
288
+ summary = "Test Issue" ,
289
+ description = "Test Description" ,
290
+ issue_type = "Task" ,
291
+ )
292
+ with self .assertRaises (Exception ):
293
+ self .client .create_issue (issue_data .model_dump ())
284
294
285
295
def test_create_subtask_success (self ):
286
296
"""Test successful subtask creation."""
287
- self .mock_jira_instance .issue_create .return_value = {
288
- "key" : "TEST-124" ,
289
- "id" : "12346" ,
290
- }
291
-
292
- subtask_data = JiraSubtaskData (
293
- project_key = "TEST" ,
294
- summary = "Test Subtask" ,
295
- description = "Test Subtask Description" ,
296
- issue_type = "Sub-task" ,
297
- parent_id = "TEST-123" ,
298
- )
299
- created_subtask = self .client .create_subtask (subtask_data .model_dump ())
297
+ with patch ("src.jira_client.Jira" ) as mock_jira_class :
298
+ mock_jira_instance = mock_jira_class .return_value
299
+ mock_jira_instance .issue_create .return_value = {
300
+ "key" : "TEST-124" ,
301
+ "id" : "12346" ,
302
+ }
303
+ self .client .jira = mock_jira_instance
304
+
305
+ subtask_data = JiraSubtaskData (
306
+ project_key = "TEST" ,
307
+ summary = "Test Subtask" ,
308
+ description = "Test Subtask Description" ,
309
+ issue_type = "Sub-task" ,
310
+ parent_id = "TEST-123" ,
311
+ )
312
+ created_subtask = self .client .create_subtask (subtask_data .model_dump ())
300
313
301
- self .assertIsNotNone (created_subtask )
302
- self .assertEqual (created_subtask ["key" ], "TEST-124" )
303
- self . mock_jira_instance .issue_create .assert_called_once ()
314
+ self .assertIsNotNone (created_subtask )
315
+ self .assertEqual (created_subtask ["key" ], "TEST-124" )
316
+ mock_jira_instance .issue_create .assert_called_once ()
304
317
305
318
306
319
if __name__ == "__main__" :
0 commit comments