-
-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathtest_utils.py
More file actions
384 lines (287 loc) · 10.9 KB
/
test_utils.py
File metadata and controls
384 lines (287 loc) · 10.9 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
# -*- coding: utf-8 -*-
"""Unit tests for the 'utils' module."""
import arrow
import collections as co
import csv
import functools
import json
import os
import datetime
import operator
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
try:
from unittest.mock import patch
except ImportError:
from mock import patch
import pytest
from click.exceptions import Abort
from dateutil.tz import tzutc
from watson.utils import (
apply_weekday_offset,
build_csv,
confirm_project,
confirm_tags,
flatten_report_for_csv,
frames_to_csv,
frames_to_json,
get_start_time_for_period,
make_json_writer,
safe_save,
sorted_groupby,
parse_tags,
PY2,
json_arrow_encoder,
)
from . import mock_datetime
_dt = functools.partial(datetime.datetime, tzinfo=tzutc())
@pytest.mark.parametrize('now, mode, start_time', [
(_dt(2016, 6, 2), 'year', _dt(2016, 1, 1)),
(_dt(2016, 6, 2), 'month', _dt(2016, 6, 1)),
(_dt(2016, 6, 2), 'week', _dt(2016, 5, 30)),
(_dt(2016, 6, 2), 'day', _dt(2016, 6, 2)),
(_dt(2016, 6, 2), 'all', _dt(1970, 1, 1)),
(_dt(2016, 6, 2), 'luna', _dt(2016, 5, 21, 21, 16)),
(_dt(2012, 2, 24), 'year', _dt(2012, 1, 1)),
(_dt(2012, 2, 24), 'month', _dt(2012, 2, 1)),
(_dt(2012, 2, 24), 'week', _dt(2012, 2, 20)),
(_dt(2012, 2, 24), 'day', _dt(2012, 2, 24)),
(_dt(2012, 2, 24), 'all', _dt(1970, 1, 1)),
(_dt(2012, 2, 24), 'luna', _dt(2012, 2, 7, 21, 56)),
])
def test_get_start_time_for_period(now, mode, start_time):
with mock_datetime(now, datetime):
assert get_start_time_for_period(mode).datetime == start_time
@pytest.mark.parametrize("monday_start, week_start, new_start", [
("2018 12 3", "monday", "2018 12 3"),
("2018 12 3", "tuesday", "2018 12 4"),
("2018 12 3", "wednesday", "2018 12 5"),
("2018 12 3", "thursday", "2018 12 6"),
("2018 12 3", "friday", "2018 11 30"),
("2018 12 3", "saturday", "2018 12 1"),
("2018 12 3", "sunday", "2018 12 2"),
("2018 12 3", "typo", "2018 12 3"),
])
def test_apply_weekday_offset(monday_start, week_start, new_start):
with mock_datetime(_dt(2018, 12, 6), datetime):
original_start = arrow.get(monday_start, "YYYY MM D")
result = arrow.get(new_start, "YYYY MM D")
assert apply_weekday_offset(original_start, week_start) == result
def test_make_json_writer():
fp = StringIO()
writer = make_json_writer(lambda: {'foo': 42})
writer(fp)
assert fp.getvalue() == '{\n "foo": 42\n}'
def test_make_json_writer_with_args():
fp = StringIO()
writer = make_json_writer(lambda x: {'foo': x}, 23)
writer(fp)
assert fp.getvalue() == '{\n "foo": 23\n}'
def test_make_json_writer_with_kwargs():
fp = StringIO()
writer = make_json_writer(lambda foo=None: {'foo': foo}, foo='bar')
writer(fp)
assert fp.getvalue() == '{\n "foo": "bar"\n}'
def test_make_json_writer_with_unicode():
fp = StringIO()
writer = make_json_writer(lambda: {u'ùñï©ôð€': u'εvεrywhεrε'})
writer(fp)
expected = u'{\n "ùñï©ôð€": "εvεrywhεrε"\n}'
if PY2:
expected = expected.encode('utf-8')
assert fp.getvalue() == expected
def test_safe_save(config_dir):
save_file = os.path.join(config_dir, 'test')
backup_file = os.path.join(config_dir, 'test' + '.bak')
assert not os.path.exists(save_file)
safe_save(save_file, lambda f: f.write("Success"))
assert os.path.exists(save_file)
assert not os.path.exists(backup_file)
with open(save_file) as fp:
assert fp.read() == "Success"
safe_save(save_file, "Again")
assert os.path.exists(backup_file)
with open(save_file) as fp:
assert fp.read() == "Again"
with open(backup_file) as fp:
assert fp.read() == "Success"
assert os.path.getmtime(save_file) >= os.path.getmtime(backup_file)
def test_safe_save_tmpfile_on_other_filesystem(config_dir, mocker):
save_file = os.path.join(config_dir, 'test')
backup_file = os.path.join(config_dir, 'test' + '.bak')
assert not os.path.exists(save_file)
safe_save(save_file, lambda f: f.write("Success"))
assert os.path.exists(save_file)
assert not os.path.exists(backup_file)
with open(save_file) as fp:
assert fp.read() == "Success"
# simulate tmpfile being on another file-system
# OSError is caught and handled by shutil.move() used by save_safe()
mocker.patch('os.rename', side_effect=OSError)
safe_save(save_file, "Again")
assert os.path.exists(backup_file)
with open(save_file) as fp:
assert fp.read() == "Again"
def test_safe_save_with_exception(config_dir):
save_file = os.path.join(config_dir, 'test')
backup_file = os.path.join(config_dir, 'test' + '.bak')
def failing_writer(f):
raise RuntimeError("Save failed.")
assert not os.path.exists(save_file)
with pytest.raises(RuntimeError):
safe_save(save_file, failing_writer)
assert not os.path.exists(save_file)
assert not os.path.exists(backup_file)
safe_save(save_file, lambda f: f.write("Success"))
assert os.path.exists(save_file)
assert not os.path.exists(backup_file)
with pytest.raises(RuntimeError):
safe_save(save_file, failing_writer)
with open(save_file) as fp:
assert fp.read() == "Success"
assert not os.path.exists(backup_file)
@pytest.mark.parametrize('args, parsed_tags', [
(['+ham', '+n', '+eggs'], ['ham', 'n', 'eggs']),
(['+ham', 'n', '+eggs'], ['ham n', 'eggs']),
(['ham', 'n', '+eggs'], ['eggs']),
(['ham', '+n', 'eggs'], ['n eggs']),
(['+ham', 'n', 'eggs'], ['ham n eggs']),
])
def test_parse_tags(args, parsed_tags):
tags = parse_tags(args)
assert tags == parsed_tags
def test_confirm_project_existing_project_returns_true():
project = 'foo'
watson_projects = ['foo', 'bar']
assert confirm_project(project, watson_projects)
@patch('click.confirm', return_value=True)
def test_confirm_project_accept_returns_true(confirm):
project = 'baz'
watson_projects = ['foo', 'bar']
assert confirm_project(project, watson_projects)
@patch('watson.utils.click.confirm', side_effect=Abort)
def test_confirm_project_reject_raises_abort(confirm):
project = 'baz'
watson_projects = ['foo', 'bar']
with pytest.raises(Abort):
confirm_project(project, watson_projects)
def test_confirm_tags_existing_tag_returns_true():
tags = ['a']
watson_tags = ['a', 'b']
assert confirm_tags(tags, watson_tags)
@patch('click.confirm', return_value=True)
def test_confirm_tags_accept_returns_true(confirm):
tags = ['c']
watson_tags = ['a', 'b']
assert confirm_tags(tags, watson_tags)
@patch('click.confirm', side_effect=Abort)
def test_confirm_tags_reject_raises_abort(confirm):
tags = ['c']
watson_tags = ['a', 'b']
with pytest.raises(Abort):
confirm_project(tags[0], watson_tags)
# build_csv
def test_build_csv_empty_data():
assert build_csv([]) == ''
def test_build_csv_one_col():
lt = os.linesep
data = [{'col': 'value'}, {'col': 'another value'}]
result = lt.join(['col', 'value', 'another value']) + lt
assert build_csv(data) == result
def test_build_csv_multiple_cols():
lt = os.linesep
dm = csv.get_dialect('excel').delimiter
data = [
co.OrderedDict([('col1', 'value'),
('col2', 'another value'),
('col3', 'more')]),
co.OrderedDict([('col1', 'one value'),
('col2', 'two value'),
('col3', 'three')])
]
result = lt.join([
dm.join(['col1', 'col2', 'col3']),
dm.join(['value', 'another value', 'more']),
dm.join(['one value', 'two value', 'three'])
]) + lt
assert build_csv(data) == result
# sorted_groupby
def test_sorted_groupby(watson):
end = arrow.utcnow()
watson.add('foo', end.shift(hours=-25), end.shift(hours=-24), ['A'])
watson.add('bar', end.shift(hours=-1), end, ['A'])
result = list(sorted_groupby(
watson.frames,
operator.attrgetter('day'),
reverse=False))
assert result[0][0] < result[1][0]
def test_sorted_groupby_reverse(watson):
end = arrow.utcnow()
watson.add('foo', end.shift(hours=-25), end.shift(hours=-24), ['A'])
watson.add('bar', end.shift(hours=-1), end, ['A'])
result = list(sorted_groupby(
watson.frames,
operator.attrgetter('day'),
reverse=True))
assert result[0][0] > result[1][0]
# frames_to_csv
def test_frames_to_csv_empty_data(watson):
assert frames_to_csv(watson.frames) == ''
def test_frames_to_csv(watson):
watson.start('foo', tags=['A', 'B'])
watson.stop()
result = frames_to_csv(watson.frames)
read_csv = list(csv.reader(StringIO(result)))
header = ['id', 'start', 'stop', 'project', 'tags', 'note']
assert len(read_csv) == 2
assert read_csv[0] == header
assert read_csv[1][3] == 'foo'
assert read_csv[1][4] == 'A, B'
# frames_to_json
def test_frames_to_json_empty_data(watson):
assert frames_to_json(watson.frames) == '[]'
def test_frames_to_json(watson):
watson.start('foo', tags=['A', 'B'])
watson.stop()
result = json.loads(frames_to_json(watson.frames))
keys = {'id', 'start', 'stop', 'project', 'tags', 'note'}
assert len(result) == 1
assert set(result[0].keys()) == keys
assert result[0]['project'] == 'foo'
assert result[0]['tags'] == ['A', 'B']
# flatten_report_for_csv
def test_flatten_report_for_csv(watson):
now = arrow.utcnow().ceil('hour')
watson.add('foo', now.shift(hours=-4), now, ['A', 'B'])
watson.add('foo', now.shift(hours=-5), now.shift(hours=-4), ['A'])
watson.add('foo', now.shift(hours=-7), now.shift(hours=-5), ['B'])
start = now.shift(days=-1)
stop = now
result = flatten_report_for_csv(watson.report(start, stop))
assert len(result) == 3
assert result[0]['from'] == start.format('YYYY-MM-DD 00:00:00')
assert result[0]['to'] == stop.format('YYYY-MM-DD 23:59:59')
assert result[0]['project'] == 'foo'
assert result[0]['tag'] == ''
assert result[0]['time'] == (4 + 1 + 2) * 3600
assert result[1]['from'] == start.format('YYYY-MM-DD 00:00:00')
assert result[1]['to'] == stop.format('YYYY-MM-DD 23:59:59')
assert result[1]['project'] == 'foo'
assert result[1]['tag'] == 'A'
assert result[1]['time'] == (4 + 1) * 3600
assert result[2]['from'] == start.format('YYYY-MM-DD 00:00:00')
assert result[2]['to'] == stop.format('YYYY-MM-DD 23:59:59')
assert result[2]['project'] == 'foo'
assert result[2]['tag'] == 'B'
assert result[2]['time'] == (4 + 2) * 3600
def test_json_arrow_encoder():
with pytest.raises(TypeError):
json_arrow_encoder(0)
with pytest.raises(TypeError):
json_arrow_encoder('foo')
with pytest.raises(TypeError):
json_arrow_encoder(None)
now = arrow.utcnow()
assert json_arrow_encoder(now) == now.for_json()