| 1 |
from builtins import next |
| 2 |
from builtins import object |
| 3 |
from collections import namedtuple |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
from dateutil.tz import datetime |
| 7 |
from dateutil.tz.tz import tzutc |
| 8 |
|
| 9 |
from bugwarrior.services.jira import JiraService |
| 10 |
from .base import ServiceTest, AbstractServiceTest |
| 11 |
|
| 12 |
|
| 13 |
class FakeJiraClient(object): |
| 14 |
def __init__(self, arbitrary_record): |
| 15 |
self.arbitrary_record = arbitrary_record |
| 16 |
|
| 17 |
def search_issues(self, *args, **kwargs): |
| 18 |
Case = namedtuple('Case', ['raw', 'key']) |
| 19 |
return [Case(self.arbitrary_record, self.arbitrary_record['key'])] |
| 20 |
|
| 21 |
def comments(self, *args, **kwargs): |
| 22 |
return None |
| 23 |
|
| 24 |
class TestJiraIssue(AbstractServiceTest, ServiceTest): |
| 25 |
SERVICE_CONFIG = { |
| 26 |
'jira.username': 'one', |
| 27 |
'jira.base_uri': 'two', |
| 28 |
'jira.password': 'three', |
| 29 |
} |
| 30 |
|
| 31 |
arbitrary_estimation = 3600 |
| 32 |
arbitrary_id = '10' |
| 33 |
arbitrary_subtask_ids = ['11', '12'] |
| 34 |
arbitrary_project = 'DONUT' |
| 35 |
arbitrary_summary = 'lkjaldsfjaldf' |
| 36 |
|
| 37 |
arbitrary_record = { |
| 38 |
'fields': { |
| 39 |
'priority': 'Blocker', |
| 40 |
'summary': arbitrary_summary, |
| 41 |
'timeestimate': arbitrary_estimation, |
| 42 |
'created': '2016-06-06T06:07:08.123-0700', |
| 43 |
'fixVersions': [{'name': '1.2.3'}], |
| 44 |
'issuetype': {'name': 'Epic'}, |
| 45 |
'status': {'name': 'Open'}, |
| 46 |
'subtasks': [{'key': 'DONUT-%s' % subtask} for subtask in arbitrary_subtask_ids] |
| 47 |
}, |
| 48 |
'key': '%s-%s' % (arbitrary_project, arbitrary_id, ), |
| 49 |
} |
| 50 |
|
| 51 |
arbitrary_record_with_due = arbitrary_record.copy() |
| 52 |
arbitrary_record_with_due['fields']=arbitrary_record_with_due['fields'].copy() |
| 53 |
arbitrary_record_with_due['fields']['Sprint']=['com.atlassian.greenhopper.service.sprint.Sprint@4c9c41a5[id=2322,rapidViewId=1173,\ |
| 54 |
state=ACTIVE,name=Sprint 1,startDate=2016-09-06T16:08:07.4\ |
| 55 |
55Z,endDate=2016-09-23T16:08:00.000Z,completeDate=<null>,sequence=2322]'] |
| 56 |
|
| 57 |
def setUp(self): |
| 58 |
super(TestJiraIssue, self).setUp() |
| 59 |
with mock.patch('jira.client.JIRA._get_json'): |
| 60 |
self.service = self.get_mock_service(JiraService) |
| 61 |
|
| 62 |
def get_mock_service(self, *args, **kwargs): |
| 63 |
service = super(TestJiraIssue, self).get_mock_service(*args, **kwargs) |
| 64 |
service.jira = FakeJiraClient(self.arbitrary_record) |
| 65 |
service.sprint_field_names = ['Sprint'] |
| 66 |
service.import_sprints_as_tags = True |
| 67 |
return service |
| 68 |
|
| 69 |
def test_to_taskwarrior(self): |
| 70 |
arbitrary_url = 'http://one' |
| 71 |
arbitrary_extra = { |
| 72 |
'jira_version': 5, |
| 73 |
'annotations': ['an annotation'], |
| 74 |
} |
| 75 |
|
| 76 |
issue = self.service.get_issue_for_record( |
| 77 |
self.arbitrary_record, arbitrary_extra |
| 78 |
) |
| 79 |
|
| 80 |
expected_output = { |
| 81 |
'project': self.arbitrary_project, |
| 82 |
'priority': ( |
| 83 |
issue.PRIORITY_MAP[self.arbitrary_record['fields']['priority']] |
| 84 |
), |
| 85 |
'annotations': arbitrary_extra['annotations'], |
| 86 |
'due': None, |
| 87 |
'tags': [], |
| 88 |
'entry': datetime.datetime(2016, 6, 6, 13, 7, 8, tzinfo=tzutc()), |
| 89 |
'jirafixversion': '1.2.3', |
| 90 |
'jiraissuetype': 'Epic', |
| 91 |
'jirastatus': 'Open', |
| 92 |
'jirasubtasks': 'DONUT-11,DONUT-12', |
| 93 |
|
| 94 |
issue.URL: arbitrary_url, |
| 95 |
issue.FOREIGN_ID: self.arbitrary_record['key'], |
| 96 |
issue.SUMMARY: self.arbitrary_summary, |
| 97 |
issue.DESCRIPTION: None, |
| 98 |
issue.ESTIMATE: self.arbitrary_estimation / 60 / 60 |
| 99 |
} |
| 100 |
|
| 101 |
def get_url(*args): |
| 102 |
return arbitrary_url |
| 103 |
|
| 104 |
with mock.patch.object(issue, 'get_url', side_effect=get_url): |
| 105 |
actual_output = issue.to_taskwarrior() |
| 106 |
|
| 107 |
self.assertEqual(actual_output, expected_output) |
| 108 |
|
| 109 |
def test_to_taskwarrior_sprint_with_goal(self): |
| 110 |
record_with_goal = self.arbitrary_record.copy() |
| 111 |
record_with_goal['fields'] = self.arbitrary_record_with_due['fields'].copy() |
| 112 |
record_with_goal['fields']['Sprint'] = [ |
| 113 |
'com.atlassian.greenhopper.service.sprint.Sprint@4c9c41a5[id=2322,rapidViewId=1173,\ |
| 114 |
state=ACTIVE,name=Sprint 1,goal=Do foo, bar, baz,startDate=2016-09-06T16:08:07.4\ |
| 115 |
55Z,endDate=2016-09-23T16:08:00.000Z,completeDate=<null>,sequence=2322]' |
| 116 |
] |
| 117 |
arbitrary_url = 'http://one' |
| 118 |
arbitrary_extra = { |
| 119 |
'jira_version': 5, |
| 120 |
'annotations': ['an annotation'], |
| 121 |
} |
| 122 |
|
| 123 |
issue = self.service.get_issue_for_record( |
| 124 |
record_with_goal, arbitrary_extra |
| 125 |
) |
| 126 |
|
| 127 |
expected_output = { |
| 128 |
'project': self.arbitrary_project, |
| 129 |
'priority': ( |
| 130 |
issue.PRIORITY_MAP[record_with_goal['fields']['priority']] |
| 131 |
), |
| 132 |
'annotations': arbitrary_extra['annotations'], |
| 133 |
'due': datetime.datetime(2016, 9, 23, 16, 8, tzinfo=tzutc()), |
| 134 |
'tags': ['Sprint1'], |
| 135 |
'entry': datetime.datetime(2016, 6, 6, 13, 7, 8, tzinfo=tzutc()), |
| 136 |
'jirafixversion': '1.2.3', |
| 137 |
'jiraissuetype': 'Epic', |
| 138 |
'jirastatus': 'Open', |
| 139 |
'jirasubtasks': 'DONUT-11,DONUT-12', |
| 140 |
|
| 141 |
issue.URL: arbitrary_url, |
| 142 |
issue.FOREIGN_ID: record_with_goal['key'], |
| 143 |
issue.SUMMARY: self.arbitrary_summary, |
| 144 |
issue.DESCRIPTION: None, |
| 145 |
issue.ESTIMATE: self.arbitrary_estimation / 60 / 60 |
| 146 |
} |
| 147 |
|
| 148 |
def get_url(*args): |
| 149 |
return arbitrary_url |
| 150 |
|
| 151 |
with mock.patch.object(issue, 'get_url', side_effect=get_url): |
| 152 |
actual_output = issue.to_taskwarrior() |
| 153 |
|
| 154 |
self.assertEqual(actual_output, expected_output) |
| 155 |
|
| 156 |
|
| 157 |
def test_issues(self): |
| 158 |
issue = next(self.service.issues()) |
| 159 |
|
| 160 |
expected = { |
| 161 |
'annotations': [], |
| 162 |
'due': None, |
| 163 |
'description': '(bw)Is#10 - lkjaldsfjaldf .. two/browse/DONUT-10', |
| 164 |
'entry': datetime.datetime(2016, 6, 6, 13, 7, 8, tzinfo=tzutc()), |
| 165 |
'jiradescription': None, |
| 166 |
'jiraestimate': 1, |
| 167 |
'jirafixversion': '1.2.3', |
| 168 |
'jiraid': 'DONUT-10', |
| 169 |
'jiraissuetype': 'Epic', |
| 170 |
'jirastatus': 'Open', |
| 171 |
'jirasummary': 'lkjaldsfjaldf', |
| 172 |
'jiraurl': 'two/browse/DONUT-10', |
| 173 |
'jirasubtasks': 'DONUT-11,DONUT-12', |
| 174 |
'priority': 'H', |
| 175 |
'project': 'DONUT', |
| 176 |
'tags': []} |
| 177 |
|
| 178 |
self.assertEqual(issue.get_taskwarrior_record(), expected) |
| 179 |
|
| 180 |
def test_get_due(self): |
| 181 |
issue = self.service.get_issue_for_record( |
| 182 |
self.arbitrary_record_with_due |
| 183 |
) |
| 184 |
self.assertEqual(issue.get_due(), datetime.datetime(2016, 9, 23, 16, 8, tzinfo=tzutc())) |