-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
416 lines (365 loc) · 16.8 KB
/
tests.py
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
import configparser
import datetime
import io
import itertools
import pathlib
import re
import pytest
import requests_mock
import gtimelog2jira
def test_parse_timelog():
entries = [
gtimelog2jira.Entry(datetime.datetime(2014, 3, 31, 14, 48),
datetime.datetime(2014, 3, 31, 17, 10),
'project2: ABC-1 some work'),
gtimelog2jira.Entry(datetime.datetime(2014, 3, 31, 17, 48),
datetime.datetime(2014, 3, 31, 18, 10),
'project3: XYZ-1 other work'),
gtimelog2jira.Entry(datetime.datetime(2014, 3, 31, 18, 48),
datetime.datetime(2014, 3, 31, 19, 10),
'project2: not working on ABC-1 actually **'),
gtimelog2jira.Entry(datetime.datetime(2014, 3, 31, 19, 48),
datetime.datetime(2014, 3, 31, 20, 10),
'project2: meeting prep (ABC-MISC)'),
]
aliases = {
'ABC-MISC': 'ABC-42',
}
assert list(gtimelog2jira.parse_timelog(entries, ['ABC'], aliases, set())) == [
gtimelog2jira.WorkLog(entries[0], 'ABC-1', 'some work'),
gtimelog2jira.WorkLog(entries[-1], 'ABC-42', 'meeting prep (ABC-MISC)'),
]
def test_parse_timelog_with_allowlisting():
entries = [
gtimelog2jira.Entry(datetime.datetime(2014, 3, 31, 14, 48),
datetime.datetime(2014, 3, 31, 17, 10),
'project2: ABC-1 some work'),
gtimelog2jira.Entry(datetime.datetime(2014, 3, 31, 17, 48),
datetime.datetime(2014, 3, 31, 18, 10),
'project3: XYZ-1 other work'),
gtimelog2jira.Entry(datetime.datetime(2014, 3, 31, 18, 48),
datetime.datetime(2014, 3, 31, 19, 10),
'project2: not working on ABC-1 actually **'),
gtimelog2jira.Entry(datetime.datetime(2014, 3, 31, 19, 48),
datetime.datetime(2014, 3, 31, 20, 10),
'project2: meeting prep (ABC-MISC)'),
]
aliases = {
'ABC-MISC': 'ABC-42',
}
include = {'ABC-42'}
assert list(gtimelog2jira.parse_timelog(entries, ['ABC'], aliases, include)) == [
gtimelog2jira.WorkLog(entries[-1], 'ABC-42', 'meeting prep (ABC-MISC)'),
]
def test_parse_timelog_alias_clash():
entries = [
gtimelog2jira.Entry(datetime.datetime(2014, 3, 31, 14, 48),
datetime.datetime(2014, 3, 31, 17, 10),
'project2: meeting about something (MEET-SPLAT)'),
]
projects = ['SSPACE', 'SPLAT']
aliases = {
'MEET': 'SSPACE-192',
'MEET-SPLAT': 'SPLAT-9',
}
assert list(gtimelog2jira.parse_timelog(entries, projects, aliases, set())) == [
gtimelog2jira.WorkLog(entries[0], 'SPLAT-9', 'meeting about something (MEET-SPLAT)'),
]
class Route:
def __init__(self, handler, params=None):
self.handler = handler
self.params = params or {}
self.pattern = None
class JiraApi:
def __init__(self, mock, user='User Name'):
self.mock = mock
self.url = 'https://jira.example.com'
self.base = '/rest/api/2'
self.idseq = map(str, itertools.count(1))
self.dtformat = '%Y-%m-%dT%H:%M:%S.000%z'
self.routes = {
'get /myself': Route(self.myself),
'get /issue/{issue}/worklog': Route(self.list_worklog, {
'issue': r'[A-Z]+-[0-9]+',
}),
'post /issue/{issue}/worklog': Route(self.create_worklog, {
'issue': r'[A-Z]+-[0-9]+',
}),
}
for key, route in self.routes.items():
method, path = key.split(None, 1)
route.pattern = re.compile(self.base + path.format(**{
k: '(?P<%s>%s)' % (k, v)
for k, v in route.params.items()
}))
self.mock.register_uri(method, route.pattern, json=route.handler)
self.db = {
'user': user,
'issues': {
'BAR-24': {'issueId': next(self.idseq), 'worklog': {}},
'FOO-42': {'issueId': next(self.idseq), 'worklog': {}},
'FOO-64': {'issueId': next(self.idseq), 'worklog': {}},
},
}
self._add_worklog('Someone Else', 'FOO-64', datetime.datetime(2014, 4, 16, 11, 0).astimezone(), 300,
'did some work')
def _get_url_params(self, request, name):
return self.routes[name].pattern.search(request.url).groups()
def _get_user(self, name=None):
name = name or self.db['user']
username = name.lower().replace(' ', '.') + '@example.com'
return {
'active': True,
'displayName': name,
'emailAddress': username,
'key': username,
'accountId': hash(username),
'name': username,
'self': self.url + self.base + '/user?username' + username,
'timeZone': 'Europe/Helsinki',
}
def _add_worklog(self, user, issue, started, seconds, comment):
now = datetime.datetime.now()
worklog_id = next(self.idseq)
self.db['issues'][issue]['worklog'][worklog_id] = {
'id': worklog_id,
'author': self._get_user(user),
'comment': comment,
'started': started if isinstance(started, str) else started.strftime(self.dtformat),
'timeSpent': gtimelog2jira.human_readable_time(seconds),
'timeSpentSeconds': seconds,
'created': now.strftime(self.dtformat),
'updated': now.strftime(self.dtformat),
}
return worklog_id
def myself(self, request, context):
context.headers['content-type'] = 'application/json'
return {
'locale': 'lt_LT',
**self._get_user()
}
def list_worklog(self, request, context):
context.headers['content-type'] = 'application/json'
issue, = self._get_url_params(request, 'get /issue/{issue}/worklog')
if issue not in self.db['issues']:
context.status_code = 404
return {
'errorMessages': ['Issue ' + issue + ' Does Not Exist'],
'errors': {},
}
else:
total = len(self.db['issues'][issue]['worklog'])
return {
'maxResults': total,
'startAt': 0,
'total': total,
'worklogs': [worklog for worklog in self.db['issues'][issue]['worklog'].values()],
}
def create_worklog(self, request, context):
context.headers['content-type'] = 'application/json'
issue, = self._get_url_params(request, 'post /issue/{issue}/worklog')
if issue not in self.db['issues']:
context.status_code = 404
return {
'errorMessages': ['Issue ' + issue + ' Does Not Exist'],
'errors': {},
}
else:
context.status_code = 201
data = request.json()
worklog_id = self._add_worklog(self.db['user'], issue, data['started'], data['timeSpentSeconds'],
data['comment'])
worklog = self.db['issues'][issue]['worklog'][worklog_id]
return {
'author': worklog['author'],
'comment': worklog['comment'],
'created': worklog['created'],
'id': worklog_id,
'issueId': self.db['issues'][issue]['issueId'],
'self': self.url + self.base + '/issue/' + self.db['issues'][issue]['issueId'] + '/worklog/' + worklog_id,
'started': worklog['started'],
'timeSpent': worklog['timeSpent'],
'timeSpentSeconds': worklog['timeSpentSeconds'],
'updateAuthor': worklog['author'],
'updated': worklog['created'],
}
class Env:
def __init__(self, path, mocker, jira):
self.stdout = None
self.path = pathlib.Path(path)
self.gtimelogrc = path / 'gtimelogrc'
self.timelog = path / 'timelog.txt'
self.jiralog = path / 'jira.log'
self.jira = jira
mocker.patch('getpass.getpass', return_value='secret')
config = configparser.ConfigParser()
config.read_dict({
'gtimelog2jira': {
'jira': 'https://jira.example.com/',
'username': '[email protected]',
'password': '',
'timelog': str(self.timelog),
'jiralog': str(self.jiralog),
'projects': 'FOO BAR BAZ',
}
})
with self.gtimelogrc.open('w') as f:
config.write(f)
self.log([
'2014-03-24 14:15: arrived',
'2014-03-24 18:14: project1: some work',
'',
'2014-03-31 08:00: arrived'
'2014-03-31 15:48: project1: FOO-42 some work',
'2014-03-31 17:10: project2: ABC-1 some work',
'2014-03-31 17:38: project1: BAR-24 some work',
'2014-03-31 18:51: project1: FOO-42 some more work'
'',
'2014-04-01 13:54: arrived',
'2014-04-01 15:41: project1: FOO-42 some work',
'2014-04-01 16:04: tea **',
'2014-04-01 18:00: project1: FOO-42 some more work',
'',
'2014-04-16 10:30: arrived',
'2014-04-16 11:25: project1: FOO-64 initial work',
'2014-04-16 12:30: project1: FOO-00 missing issue',
])
def log(self, lines):
with self.timelog.open('a') as f:
for line in lines:
f.write(line + '\n')
def run(self, argv=None):
self.stdout = io.StringIO()
argv = ['-c', str(self.gtimelogrc)] + (argv or [])
return gtimelog2jira.main(argv, self.stdout)
def get_worklog(self):
user = self.jira._get_user()
return [
(worklog['started'], worklog['timeSpentSeconds'], issue_id, worklog['comment'])
for issue_id, issue in self.jira.db['issues'].items()
for worklog_id, worklog in issue['worklog'].items()
if worklog['author']['name'] == user['name']
]
def get_jiralog(self):
with self.jiralog.open() as f:
return [tuple(line.strip().split(',', 7)[1:]) for line in f]
def get_stdout(self):
return self.stdout.getvalue().splitlines()
@pytest.fixture
def env(tmpdir, mocker):
with requests_mock.Mocker() as mock:
jira = JiraApi(mock)
yield Env(tmpdir, mocker, jira)
def test_no_args(env, mocker):
mocker.patch('gtimelog2jira.get_now', return_value=datetime.datetime(2014, 4, 18).astimezone())
assert env.run() is None
env.log([
'',
'2014-04-17 10:30: arrived',
'2014-04-17 11:25: project1: FOO-64 do more work',
])
assert env.run() is None
assert env.get_worklog() == [
('2014-04-16T10:30:00.000+0300', 3300, 'FOO-64', 'initial work'),
('2014-04-17T10:30:00.000+0300', 3300, 'FOO-64', 'do more work'),
]
assert env.get_jiralog() == [
('2014-04-16T11:25+03:00', '3900', 'FOO-00', '', 'error', 'Issue FOO-00 Does Not Exist'),
('2014-04-16T10:30+03:00', '3300', 'FOO-64', '5', 'add', 'initial work'),
('2014-04-16T11:25+03:00', '3900', 'FOO-00', '', 'error', 'Issue FOO-00 Does Not Exist'),
('2014-04-16T10:30+03:00', '3300', 'FOO-64', '5', 'overlap', 'initial work'),
('2014-04-17T10:30+03:00', '3300', 'FOO-64', '6', 'add', 'do more work'),
]
def test_full_sync(env):
assert env.run(['--since', '2014-01-01']) is None
env.log([
'',
'2014-04-17 10:30: arrived',
'2014-04-17 11:25: project1: FOO-64 do more work',
])
assert env.run(['--since', '2014-01-01']) is None
assert env.get_worklog() == [
('2014-03-31T17:10:00.000+0300', 1680, 'BAR-24', 'some work'),
('2014-03-31T17:38:00.000+0300', 4380, 'FOO-42', 'some more work'),
('2014-04-01T13:54:00.000+0300', 6420, 'FOO-42', 'some work'),
('2014-04-01T16:04:00.000+0300', 6960, 'FOO-42', 'some more work'),
('2014-04-16T10:30:00.000+0300', 3300, 'FOO-64', 'initial work'),
('2014-04-17T10:30:00.000+0300', 3300, 'FOO-64', 'do more work'),
]
assert env.get_jiralog() == [
('2014-03-31T17:10+03:00', '1680', 'BAR-24', '5', 'add', 'some work'),
('2014-04-16T11:25+03:00', '3900', 'FOO-00', '', 'error', 'Issue FOO-00 Does Not Exist'),
('2014-03-31T17:38+03:00', '4380', 'FOO-42', '6', 'add', 'some more work'),
('2014-04-01T13:54+03:00', '6420', 'FOO-42', '7', 'add', 'some work'),
('2014-04-01T16:04+03:00', '6960', 'FOO-42', '8', 'add', 'some more work'),
('2014-04-16T10:30+03:00', '3300', 'FOO-64', '9', 'add', 'initial work'),
('2014-03-31T17:10+03:00', '1680', 'BAR-24', '5', 'overlap', 'some work'),
('2014-04-16T11:25+03:00', '3900', 'FOO-00', '', 'error', 'Issue FOO-00 Does Not Exist'),
('2014-03-31T17:38+03:00', '4380', 'FOO-42', '6', 'overlap', 'some more work'),
('2014-04-01T13:54+03:00', '6420', 'FOO-42', '7', 'overlap', 'some work'),
('2014-04-01T16:04+03:00', '6960', 'FOO-42', '8', 'overlap', 'some more work'),
('2014-04-16T10:30+03:00', '3300', 'FOO-64', '9', 'overlap', 'initial work'),
('2014-04-17T10:30+03:00', '3300', 'FOO-64', '10', 'add', 'do more work'),
]
def test_single_issue(env):
assert env.run(['--issue', 'FOO-42']) is None
env.log([
'',
'2014-04-17 10:30: arrived',
'2014-04-17 11:25: project1: FOO-42 do more work',
'2014-04-17 12:30: project1: FOO-64 do more work',
])
assert env.run(['--issue', 'FOO-42']) is None
assert env.get_worklog() == [
('2014-03-31T17:38:00.000+0300', 4380, 'FOO-42', 'some more work'),
('2014-04-01T13:54:00.000+0300', 6420, 'FOO-42', 'some work'),
('2014-04-01T16:04:00.000+0300', 6960, 'FOO-42', 'some more work'),
('2014-04-17T10:30:00.000+0300', 3300, 'FOO-42', 'do more work'),
]
assert env.get_jiralog() == [
('2014-03-31T17:38+03:00', '4380', 'FOO-42', '5', 'add', 'some more work'),
('2014-04-01T13:54+03:00', '6420', 'FOO-42', '6', 'add', 'some work'),
('2014-04-01T16:04+03:00', '6960', 'FOO-42', '7', 'add', 'some more work'),
('2014-03-31T17:38+03:00', '4380', 'FOO-42', '5', 'overlap', 'some more work'),
('2014-04-01T13:54+03:00', '6420', 'FOO-42', '6', 'overlap', 'some work'),
('2014-04-01T16:04+03:00', '6960', 'FOO-42', '7', 'overlap', 'some more work'),
('2014-04-17T10:30+03:00', '3300', 'FOO-42', '8', 'add', 'do more work'),
]
def test_since_date(env):
assert env.run(['--since', '2014-04-16']) is None
assert env.run(['--since', '2014-04-16']) is None
assert env.get_worklog() == [
('2014-04-16T10:30:00.000+0300', 3300, 'FOO-64', 'initial work')
]
assert env.get_jiralog() == [
('2014-04-16T11:25+03:00', '3900', 'FOO-00', '', 'error', 'Issue FOO-00 Does Not Exist'),
('2014-04-16T10:30+03:00', '3300', 'FOO-64', '5', 'add', 'initial work'),
('2014-04-16T11:25+03:00', '3900', 'FOO-00', '', 'error', 'Issue FOO-00 Does Not Exist'),
('2014-04-16T10:30+03:00', '3300', 'FOO-64', '5', 'overlap', 'initial work'),
]
def test_dry_run(env):
assert env.run(['--dry-run', '--since', '2014-01-01']) is None
assert env.get_worklog() == []
assert env.get_jiralog() == [
('2014-03-31T17:10+03:00', '1680', 'BAR-24', '', 'add (dry run)', 'some work'),
('2014-04-16T11:25+03:00', '3900', 'FOO-00', '', 'add (dry run)', 'missing issue'),
('2014-03-31T17:38+03:00', '4380', 'FOO-42', '', 'add (dry run)', 'some more work'),
('2014-04-01T13:54+03:00', '6420', 'FOO-42', '', 'add (dry run)', 'some work'),
('2014-04-01T16:04+03:00', '6960', 'FOO-42', '', 'add (dry run)', 'some more work'),
('2014-04-16T10:30+03:00', '3300', 'FOO-64', '', 'add (dry run)', 'initial work'),
]
assert env.get_stdout() == [
'',
'ADD: BAR-24 2014-03-31T17:10+03:00 28m: some work',
'ADD: FOO-00 2014-04-16T11:25+03:00 1h 5m: missing issue',
'ADD: FOO-42 2014-03-31T17:38+03:00 1h 13m: some more work',
'ADD: FOO-42 2014-04-01T13:54+03:00 1h 47m: some work',
'ADD: FOO-42 2014-04-01T16:04+03:00 1h 56m: some more work',
'ADD: FOO-64 2014-04-16T10:30+03:00 55m: initial work',
'',
'TOTALS:',
' BAR-24: 28m (1), https://jira.example.com/browse/BAR-24',
' FOO-00: 1h 5m (1), https://jira.example.com/browse/FOO-00',
' FOO-42: 4h 56m (3), https://jira.example.com/browse/FOO-42',
' FOO-64: 55m (1), https://jira.example.com/browse/FOO-64',
]