forked from release-engineering/Sync2Jira
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_downstream_pr.py
More file actions
717 lines (615 loc) · 26.3 KB
/
Copy pathtest_downstream_pr.py
File metadata and controls
717 lines (615 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
import unittest
import unittest.mock as mock
from unittest.mock import MagicMock
import sync2jira.downstream_pr as d
PATH = "sync2jira.downstream_pr."
class TestDownstreamPR(unittest.TestCase):
"""
This class tests the downstream_pr.py file under sync2jira
"""
def setUp(self):
"""
Setting up the testing environment
"""
self.mock_pr = MagicMock()
self.mock_pr.jira_key = "JIRA-1234"
self.mock_pr.suffix = "mock_suffix"
self.mock_pr.title = "mock_title"
self.mock_pr.url = "mock_url"
self.mock_pr.reporter = "mock_reporter"
self.mock_pr.downstream = {
"pr_updates": [
{"merge_transition": "CUSTOM_TRANSITION1"},
{"link_transition": "CUSTOM_TRANSITION2"},
]
}
self.mock_config = {
"sync2jira": {
"default_jira_instance": "another_jira_instance",
"jira_username": "mock_user",
"jira": {
"mock_jira_instance": {"mock_jira": "mock_jira"},
"another_jira_instance": {
"basic_auth": ("email", "mock_token"),
"options": {"server": "mock_server"},
},
},
"testing": False,
"legacy_matching": False,
"admins": [{"mock_admin": "mock_email"}],
"develop": False,
},
}
self.mock_client = MagicMock()
mock_user = MagicMock()
mock_user.displayName = "mock_reporter"
mock_user.key = "mock_key"
self.mock_client.search_users.return_value = [mock_user]
self.mock_client.search_issues.return_value = ["mock_existing"]
self.mock_existing = MagicMock()
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "d_issue")
def test_sync_with_jira_link(self, mock_d_issue, mock_update_jira_issue):
"""
This function tests 'sync_with_jira'
"""
# Set up return values
mock_d_issue.get_jira_client.return_value = self.mock_client
# Call the function
d.sync_with_jira(self.mock_pr, self.mock_config)
# Assert everything was called correctly
mock_update_jira_issue.assert_called_with(
"mock_existing", self.mock_pr, self.mock_client
)
self.mock_client.search_issues.assert_called_with("Key = JIRA-1234")
mock_d_issue.get_jira_client.assert_called_with(self.mock_pr, self.mock_config)
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "d_issue")
def test_sync_with_jira_merged(self, mock_d_issue, mock_update_jira_issue):
"""
This function tests 'sync_with_jira'
"""
# Set up return values
mock_client = MagicMock()
mock_client.search_issues.return_value = ["mock_existing"]
mock_d_issue.get_jira_client.return_value = mock_client
self.mock_pr.suffix = "merged"
# Call the function
d.sync_with_jira(self.mock_pr, self.mock_config)
# Assert everything was called correctly
mock_update_jira_issue.assert_called_with(
"mock_existing", self.mock_pr, mock_client
)
mock_client.search_issues.assert_called_with("Key = JIRA-1234")
mock_d_issue.get_jira_client.assert_called_with(self.mock_pr, self.mock_config)
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "d_issue")
def test_sync_with_jira_no_match(self, mock_d_issue, mock_update_jira_issue):
"""
This function tests 'sync_with_jira' when the PR contains no matched issue.
"""
# Set up return values
mock_d_issue.get_jira_client.return_value = self.mock_client
self.mock_pr.jira_key = None
# Call the function
d.sync_with_jira(self.mock_pr, self.mock_config)
# Assert everything was called correctly
mock_d_issue.get_jira_client.assert_called_with(self.mock_pr, self.mock_config)
self.mock_client.search_issues.assert_not_called()
mock_update_jira_issue.assert_not_called()
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "d_issue")
def test_sync_with_jira_no_issues_found(self, mock_d_issue, mock_update_jira_issue):
"""
This function tests 'sync_with_jira' where no issues are found
"""
# Set up return values
self.mock_client.search_issues.return_value = []
mock_d_issue.get_jira_client.return_value = self.mock_client
# Call the function
d.sync_with_jira(self.mock_pr, self.mock_config)
# Assert everything was called correctly
mock_update_jira_issue.assert_not_called()
self.mock_client.search_issues.assert_called_with("Key = JIRA-1234")
mock_d_issue.get_jira_client.assert_called_with(self.mock_pr, self.mock_config)
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "d_issue")
def test_sync_with_jira_testing(self, mock_d_issue, mock_update_jira_issue):
"""
This function tests 'sync_with_jira' where no issues are found
"""
# Set up return values
mock_client = MagicMock()
mock_client.search_issues.return_value = []
self.mock_config["sync2jira"]["testing"] = True
mock_d_issue.get_jira_client.return_value = mock_client
# Call the function
d.sync_with_jira(self.mock_pr, self.mock_config)
# Assert everything was called correctly
mock_update_jira_issue.assert_not_called()
mock_client.search_issues.assert_not_called()
mock_d_issue.get_jira_client.assert_not_called()
@mock.patch(PATH + "comment_exists")
@mock.patch(PATH + "format_comment")
@mock.patch(PATH + "d_issue.attach_link")
@mock.patch(PATH + "issue_link_exists")
def test_update_jira_issue_link(
self,
mock_issue_link_exists,
mock_attach_link,
mock_format_comment,
mock_comment_exists,
):
"""
This function tests 'update_jira_issue'
"""
# Set up return values
mock_format_comment.return_value = "mock_formatted_comment"
mock_comment_exists.return_value = False
mock_issue_link_exists.return_value = False
# Call the function
d.update_jira_issue("mock_existing", self.mock_pr, self.mock_client)
# Assert everything was called correctly
self.mock_client.add_comment.assert_called_with(
"mock_existing", "mock_formatted_comment"
)
mock_format_comment.assert_called_with(
self.mock_pr, self.mock_pr.suffix, self.mock_client
)
mock_comment_exists.assert_called_with(
self.mock_client, "mock_existing", "mock_formatted_comment"
)
mock_attach_link.assert_called_with(
self.mock_client,
"mock_existing",
{"url": "mock_url", "title": "[PR] mock_title"},
)
def test_issue_link_exists_false(self):
"""
This function tests 'issue_link_exists' where it does not exist
"""
# Set up return values
mock_issue_link = MagicMock()
mock_issue_link.object.url = "bad_url"
self.mock_client.remote_links.return_value = [mock_issue_link]
# Call the function
ret = d.issue_link_exists(self.mock_client, self.mock_existing, self.mock_pr)
# Assert everything was called correctly
self.mock_client.remote_links.assert_called_with(self.mock_existing)
self.assertEqual(ret, False)
def test_issue_link_exists_true(self):
"""
This function tests 'issue_link_exists' where it does exist
"""
# Set up return values
mock_issue_link = MagicMock()
mock_issue_link.object.url = self.mock_pr.url
self.mock_client.remote_links.return_value = [mock_issue_link]
# Call the function
ret = d.issue_link_exists(self.mock_client, self.mock_existing, self.mock_pr)
# Assert everything was called correctly
self.mock_client.remote_links.assert_called_with(self.mock_existing)
self.assertEqual(ret, True)
@mock.patch(PATH + "format_comment")
@mock.patch(PATH + "comment_exists")
@mock.patch(PATH + "d_issue.attach_link")
@mock.patch(PATH + "issue_link_exists")
def test_update_jira_issue_exists(
self,
mock_issue_link_exists,
mock_attach_link,
mock_comment_exists,
mock_format_comment,
):
"""
This function tests 'update_jira_issue' where the comment already exists
"""
# Set up return values
mock_format_comment.return_value = "mock_formatted_comment"
mock_comment_exists.return_value = True
mock_issue_link_exists.return_value = True
# Call the function
d.update_jira_issue("mock_existing", self.mock_pr, self.mock_client)
# Assert everything was called correctly
self.mock_client.add_comment.assert_not_called()
mock_format_comment.assert_called_with(
self.mock_pr, self.mock_pr.suffix, self.mock_client
)
mock_comment_exists.assert_called_with(
self.mock_client, "mock_existing", "mock_formatted_comment"
)
mock_attach_link.assert_not_called()
mock_issue_link_exists.assert_called_with(
self.mock_client, "mock_existing", self.mock_pr
)
def test_comment_exists_false(self):
"""
This function tests 'comment_exists' where the comment does not exists
"""
# Set up return values
mock_comment = MagicMock()
mock_comment.body = "not_mock_new_comment"
self.mock_client.comments.return_value = [mock_comment]
# Call the function
response = d.comment_exists(
self.mock_client, "mock_existing", "mock_new_comment"
)
# Assert Everything was called correctly
self.mock_client.comments.assert_called_with("mock_existing")
self.assertEqual(response, False)
def test_comment_exists_true(self):
"""
This function tests 'comment_exists' where the comment exists
"""
# Set up return values
mock_comment = MagicMock()
mock_comment.body = "mock_new_comment"
self.mock_client.comments.return_value = [mock_comment]
# Call the function
response = d.comment_exists(
self.mock_client, "mock_existing", "mock_new_comment"
)
# Assert Everything was called correctly
self.mock_client.comments.assert_called_with("mock_existing")
self.assertEqual(response, True)
def test_format_comment_closed(self):
"""
This function tests 'format_comment' where the PR is closed
"""
# Call the function
response = d.format_comment(self.mock_pr, "closed", self.mock_client)
# Assert Everything was called correctly
self.assertEqual(response, "Merge request [mock_title| mock_url] was closed.")
def test_format_comment_reopened(self):
"""
This function tests 'format_comment' where the PR is reopened
"""
# Call the function
response = d.format_comment(self.mock_pr, "reopened", self.mock_client)
# Assert Everything was called correctly
self.assertEqual(response, "Merge request [mock_title| mock_url] was reopened.")
def test_format_comment_merged(self):
"""
This function tests 'format_comment' where the PR is merged
"""
# Call the function
response = d.format_comment(self.mock_pr, "merged", self.mock_client)
# Assert Everything was called correctly
self.assertEqual(response, "Merge request [mock_title| mock_url] was merged!")
def test_format_comment_open(self):
"""
This function tests 'format_comment' where the PR is open
"""
# Call the function
response = d.format_comment(self.mock_pr, "open", self.mock_client)
# Assert Everything was called correctly
self.assertEqual(
response,
"[~mock_key] mentioned this issue in merge request [mock_title| mock_url].",
)
def test_format_comment_open_no_user_found(self):
"""
This function tests 'format_comment' where the PR is open and search_users returns nothing
"""
# Set up return values
self.mock_client.search_users.return_value = []
# Call the function
response = d.format_comment(self.mock_pr, "open", self.mock_client)
# Assert Everything was called correctly
self.assertEqual(
response,
"mock_reporter mentioned this issue in merge request [mock_title| mock_url].",
)
@mock.patch(PATH + "d_issue")
def test_update_transition(self, mock_d_issue):
"""
This function tests 'update_transition'
"""
# Set up return values
mock_client = MagicMock()
# Call the function
d.update_transition(
mock_client, self.mock_existing, self.mock_pr, "merge_transition"
)
# Assert everything was called correctly
mock_d_issue.change_status.assert_called_with(
mock_client, self.mock_existing, "CUSTOM_TRANSITION1", self.mock_pr
)
@mock.patch(PATH + "update_jira")
@mock.patch(PATH + "d_issue")
def test_sync_with_jira_create_pr_issue_enabled(
self, mock_d_issue, mock_update_jira
):
"""
Test 'sync_with_jira' when create_pr_issue is enabled and no JIRA key is found.
"""
# Set up return values
self.mock_pr.jira_key = None
self.mock_pr.match = None
self.mock_pr.downstream = {"create_pr_issue": True}
mock_client = MagicMock()
mock_d_issue.get_jira_client.return_value = mock_client
# Call the function
d.sync_with_jira(self.mock_pr, self.mock_config)
# Assert update_jira was called
mock_update_jira.assert_called_with(mock_client, self.mock_config, self.mock_pr)
@mock.patch(PATH + "update_jira")
@mock.patch(PATH + "d_issue")
def test_sync_with_jira_create_pr_issue_disabled(
self, mock_d_issue, mock_update_jira
):
"""
Test 'sync_with_jira' when create_pr_issue is disabled and no JIRA key is found.
Should skip and return early.
"""
# Set up return values
self.mock_pr.jira_key = None
self.mock_pr.match = None
self.mock_pr.downstream = {"create_pr_issue": False}
# Call the function
d.sync_with_jira(self.mock_pr, self.mock_config)
# Assert everything was called correctly
mock_update_jira.assert_not_called()
@mock.patch(PATH + "d_issue")
def test_update_jira_service_unavailable(self, mock_d_issue):
"""Test 'update_jira' when in development mode and the Jira service is unavailable."""
# Set up return values
self.mock_config["sync2jira"]["develop"] = False
mock_d_issue.check_jira_status.return_value = False
# Call the function
with self.assertRaises(RuntimeError):
d.update_jira(self.mock_client, self.mock_config, self.mock_pr)
@mock.patch(PATH + "_create_jira_issue_from_pr")
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "matcher")
@mock.patch(PATH + "d_issue")
def test_update_jira_with_no_key_create_enabled_no_issue(
self,
mock_d_issue,
mock_matcher,
mock_update_jira_issue,
mock_create_jira_issue_from_pr,
):
"""Test 'update_jira' when no JIRA key is found, issue creation is
enabled, and the issue is not found.
"""
# Set up return values
mock_d_issue.check_jira_status.return_value = True
mock_matcher.return_value = self.mock_pr.jira_key = None
self.mock_pr.downstream = {"create_pr_issue": True}
mock_d_issue.get_existing_jira_issue.return_value = None
# Call the function
d.update_jira(self.mock_client, self.mock_config, self.mock_pr)
# Assert everything was called correctly
mock_update_jira_issue.assert_not_called()
mock_create_jira_issue_from_pr.assert_called()
@mock.patch(PATH + "_create_jira_issue_from_pr")
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "matcher")
@mock.patch(PATH + "d_issue")
def test_update_jira_with_no_key_create_enabled_issue_exists(
self,
mock_d_issue,
mock_matcher,
mock_update_jira_issue,
mock_create_jira_issue_from_pr,
):
"""Test 'update_jira' when no JIRA key is found, issue creation is
enabled, and the issue exists.
"""
# Set up return values
mock_d_issue.check_jira_status.return_value = True
mock_matcher.return_value = self.mock_pr.jira_key = None
self.mock_pr.downstream = {"create_pr_issue": True}
mock_d_issue.get_existing_jira_issue.return_value = self.mock_existing
# Call the function
d.update_jira(self.mock_client, self.mock_config, self.mock_pr)
# Assert everything was called correctly
mock_update_jira_issue.assert_called()
mock_create_jira_issue_from_pr.assert_not_called()
@mock.patch(PATH + "_create_jira_issue_from_pr")
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "matcher")
@mock.patch(PATH + "d_issue")
def test_update_jira_with_no_key_create_disabled(
self,
mock_d_issue,
mock_matcher,
mock_update_jira_issue,
mock_create_jira_issue_from_pr,
):
"""Test 'update_jira' when no JIRA key is found and issue creation is disabled."""
# Set up return values
mock_d_issue.check_jira_status.return_value = True
mock_matcher.return_value = self.mock_pr.jira_key = None
self.mock_pr.downstream = {"create_pr_issue": False}
# Call the function
d.update_jira(self.mock_client, self.mock_config, self.mock_pr)
# Assert everything was (not) called correctly
mock_update_jira_issue.assert_not_called()
mock_create_jira_issue_from_pr.assert_not_called()
@mock.patch(PATH + "_create_jira_issue_from_pr")
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "matcher")
@mock.patch(PATH + "d_issue")
def test_update_jira_with_no_key_create_unspecified(
self,
mock_d_issue,
mock_matcher,
mock_update_jira_issue,
mock_create_jira_issue_from_pr,
):
"""Test 'update_jira' when no JIRA key is found and issue creation is unspecified."""
# Set up return values
mock_d_issue.check_jira_status.return_value = True
mock_matcher.return_value = self.mock_pr.jira_key = None
# Call the function
d.update_jira(self.mock_client, self.mock_config, self.mock_pr)
# Assert everything was (not) called correctly
mock_update_jira_issue.assert_not_called()
mock_create_jira_issue_from_pr.assert_not_called()
@mock.patch(PATH + "_create_jira_issue_from_pr")
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "matcher")
@mock.patch(PATH + "d_issue")
def test_update_jira_with_key_found_and_unique_issue(
self,
mock_d_issue,
mock_matcher,
mock_update_jira_issue,
mock_create_jira_issue_from_pr,
):
"""Test 'update_jira' when JIRA key is found and the issue is found."""
# Set up return values
mock_d_issue.check_jira_status.return_value = True
mock_matcher.return_value = self.mock_pr.jira_key
# Call the function
d.update_jira(self.mock_client, self.mock_config, self.mock_pr)
# Assert everything was called correctly
mock_update_jira_issue.assert_called()
mock_create_jira_issue_from_pr.assert_not_called()
@mock.patch(PATH + "_create_jira_issue_from_pr")
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "matcher")
@mock.patch(PATH + "d_issue")
def test_update_jira_with_key_found_and_no_issue(
self,
mock_d_issue,
mock_matcher,
mock_update_jira_issue,
mock_create_jira_issue_from_pr,
):
"""Test 'update_jira' when JIRA key is found and the issue is not found."""
# Set up return values
mock_d_issue.check_jira_status.return_value = True
mock_matcher.return_value = self.mock_pr.jira_key
self.mock_client.search_issues.return_value = []
# Call the function
d.update_jira(self.mock_client, self.mock_config, self.mock_pr)
# Assert everything was (not) called correctly
mock_update_jira_issue.assert_not_called()
mock_create_jira_issue_from_pr.assert_not_called()
@mock.patch(PATH + "_create_jira_issue_from_pr")
@mock.patch(PATH + "update_jira_issue")
@mock.patch(PATH + "matcher")
@mock.patch(PATH + "d_issue")
def test_update_jira_with_key_found_and_multiple_issues(
self,
mock_d_issue,
mock_matcher,
mock_update_jira_issue,
mock_create_jira_issue_from_pr,
):
"""Test 'update_jira' when JIRA key is found and the search returns multiple issues."""
# Set up return values
mock_d_issue.check_jira_status.return_value = True
mock_matcher.return_value = self.mock_pr.jira_key
self.mock_client.search_issues.return_value = ["MOCK-123", "MOCK-456"]
# Call the function
d.update_jira(self.mock_client, self.mock_config, self.mock_pr)
# Assert everything was called correctly: should exit without calling subroutines
mock_update_jira_issue.assert_not_called()
mock_create_jira_issue_from_pr.assert_not_called()
def _setup_pr_for_issue_creation(self, **overrides):
"""
Helper to set up mock_pr with common attributes for _create_jira_issue_from_pr tests.
Override specific attributes via kwargs.
"""
# Set defaults from setUp
self.mock_pr._title = "Test PR Title"
self.mock_pr.url = "https://github.com/test/repo/pull/1"
self.mock_pr.upstream = "test/repo"
self.mock_pr.comments = []
self.mock_pr.priority = None
self.mock_pr.content = "PR description"
self.mock_pr.reporter = "testuser"
self.mock_pr.assignee = []
self.mock_pr.status = None
self.mock_pr.id = "1"
self.mock_pr.downstream = {"project": "TEST", "type": "Task"}
# Apply any overrides
for key, value in overrides.items():
setattr(self.mock_pr, key, value)
def _assert_issue_created_with_pr_fields(self, mock_issue_class, **field_overrides):
"""
Helper to assert Issue was created with correct fields from self.mock_pr.
Only checks fields that differ from defaults or are specified in field_overrides.
"""
call_args = mock_issue_class.call_args
self.assertIsNotNone(call_args, "Issue class should have been called")
kwargs = call_args[1]
# Common assertions using self.mock_pr fields
self.assertEqual(kwargs["source"], self.mock_pr.source)
self.assertEqual(kwargs["title"], self.mock_pr._title)
self.assertEqual(kwargs["url"], self.mock_pr.url)
self.assertEqual(kwargs["upstream"], self.mock_pr.upstream)
self.assertEqual(kwargs["status"], self.mock_pr.status)
self.assertEqual(kwargs["id_"], self.mock_pr.id)
# Handle reporter conversion
if isinstance(self.mock_pr.reporter, dict):
self.assertEqual(kwargs["reporter"], self.mock_pr.reporter)
else:
self.assertEqual(
kwargs["reporter"], {"fullname": str(self.mock_pr.reporter)}
)
# Apply field-specific overrides
for key, expected_value in field_overrides.items():
self.assertEqual(kwargs[key], expected_value)
@mock.patch(PATH + "d_issue._create_jira_issue")
@mock.patch(PATH + "Issue")
def test_create_jira_issue_from_pr(self, mock_issue_class, mock_create_jira_issue):
"""
Test '_create_jira_issue_from_pr' converts PR to Issue-like object and creates JIRA issue.
"""
# Set up return values
mock_client = MagicMock()
mock_created_issue = MagicMock()
mock_create_jira_issue.return_value = mock_created_issue
# Set up PR object
self._setup_pr_for_issue_creation()
# Call the function
result = d._create_jira_issue_from_pr(
mock_client, self.mock_pr, self.mock_config
)
# Assert Issue was created with correct parameters
self._assert_issue_created_with_pr_fields(mock_issue_class)
# Assert _create_jira_issue was called and result returned
self.assertEqual(result, mock_created_issue)
@mock.patch(PATH + "d_issue._create_jira_issue")
@mock.patch(PATH + "Issue")
def test_create_jira_issue_from_pr_no_content(
self, mock_issue_class, mock_create_jira_issue
):
"""
Test '_create_jira_issue_from_pr' when PR has no content (uses fallback).
"""
# Set up return values
mock_client = MagicMock()
mock_created_issue = MagicMock()
mock_create_jira_issue.return_value = mock_created_issue
self._setup_pr_for_issue_creation(content=None)
# Call the function
d._create_jira_issue_from_pr(mock_client, self.mock_pr, self.mock_config)
self._assert_issue_created_with_pr_fields(
mock_issue_class, content=f"PR: {self.mock_pr.url}"
)
@mock.patch(PATH + "d_issue._create_jira_issue")
@mock.patch(PATH + "Issue")
def test_create_jira_issue_from_pr_reporter_dict(
self, mock_issue_class, mock_create_jira_issue
):
"""
Test '_create_jira_issue_from_pr' when reporter is already a dict (not a string).
"""
# Set up return values
mock_client = MagicMock()
mock_created_issue = MagicMock()
mock_create_jira_issue.return_value = mock_created_issue
# Set up PR object with dict reporter
self._setup_pr_for_issue_creation(
reporter={"fullname": "testuser", "email": "test@example.com"}
)
# Call the function
d._create_jira_issue_from_pr(mock_client, self.mock_pr, self.mock_config)
# Assert Issue was created with dict reporter (not wrapped)
self._assert_issue_created_with_pr_fields(mock_issue_class)