-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
536 lines (470 loc) · 17.7 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test suite for tld.py
To run, call
$ python tests.py
Note that this set of tests wants to create a directory 'tests' and will make
files 'tests/task_test', 'tests/.task_test.done', 'integration_task_test', and
'.integration_task_test.done'. If any of these files already exists, these
tests will abort and print an error message.
Other Information
-----------------
This was written by David Lowry-Duda <[email protected]>. This is available
under the MIT License (https://opensource.org/licenses/MIT).
For more information, see tld.py or https://github.com/davidlowryduda/tld.
"""
import contextlib
import datetime
import unittest
import os
from io import StringIO
from tld import TaskDict, _build_parser, main
TASK1_ID = '3fa2e7254e7ce263b186a7ab33dbc492f4138f6d'
TASK2_ID = '3ea913db45595a91c19c50ce6f977444fa69e82a'
TASK3_ID = '417af60a94ee9643bada8dbd01a691af4e064155'
TASK4_ID = '84328fb5212fb9f5a743101d9508414299370217'
class BasicTaskStructure(unittest.TestCase):
"""
A set of tests for each of the basic capabilities of a task dictionary.
"""
def setUp(self):
self.taskdict = TaskDict(name='task_test')
self.taskdict.add_task("test task 1")
self.taskdict.add_task("test task 2")
def tearDown(self):
self.taskdict = None
def test_add(self):
"""
Test basic adding of a task.
"""
goal = {
TASK1_ID: {'id': TASK1_ID, 'text': "test task 1"},
TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"},
}
self.assertEqual(self.taskdict.tasks, goal)
def test_finish(self):
"""
Test finishing of tasks. The task should appear in done.
"""
self.taskdict.finish_task('3f')
task_goal = {TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"}}
done_goal = {TASK1_ID: {'id': TASK1_ID, 'text': "test task 1"}}
self.assertEqual(self.taskdict.tasks, task_goal)
self.assertEqual(self.taskdict.done, done_goal)
def test_remove(self):
"""
Test removal of tasks. The task should not appear in done.
"""
self.taskdict.remove_task('3f')
task_goal = {TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"}}
done_goal = {}
self.assertEqual(self.taskdict.tasks, task_goal)
self.assertEqual(self.taskdict.done, done_goal)
def test_delete_finished(self):
"""
Test deletion of finished tasks.
"""
self.taskdict.finish_task('3f')
self.taskdict.delete_finished()
task_goal = {TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"}}
done_goal = {}
self.assertEqual(self.taskdict.tasks, task_goal)
self.assertEqual(self.taskdict.done, done_goal)
def test_date(self):
"""
Test that dates are saved correctly.
"""
self.taskdict.add_task("test task 3", dated=True)
goal = {
TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"},
TASK1_ID: {'id': TASK1_ID, 'text': "test task 1"},
TASK3_ID: {'id': TASK3_ID,
'text': "test task 3",
'date': datetime.date.today()}
}
self.assertEqual(self.taskdict.tasks, goal)
return
def test_edit(self):
"""
Test that one can edit tasks.
Note that the original ID as a key doesn't change, even though the
embedded subID does. This is corrected on next read.
"""
self.taskdict.edit_task('3f', "test task 3")
goal = {
TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"},
TASK1_ID: {'id': TASK3_ID, 'text': "test task 3"},
}
self.assertEqual(self.taskdict.tasks, goal)
return
def test_tag(self):
"""
Test that adding tags works.
"""
self.taskdict.add_task("test task 3", tags=['This is a test', 'two'])
goal = {
TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"},
TASK1_ID: {'id': TASK1_ID, 'text': "test task 1"},
TASK3_ID: {'id': TASK3_ID,
'text': "test task 3",
'tags': "This is a test,two"}
}
self.assertEqual(self.taskdict.tasks, goal)
return
def test_edit_with_tag(self):
"""
Test that editing a tag while editing changes the tag.
"""
self.taskdict.add_task("test task 3", tags=['This is a test', 'two'])
self.taskdict.edit_task("417", "test task 3", tags=["Repl tag"])
goal = {
TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"},
TASK1_ID: {'id': TASK1_ID, 'text': "test task 1"},
TASK3_ID: {'id': TASK3_ID,
'text': "test task 3",
'tags': "Repl tag"}
}
self.assertEqual(self.taskdict.tasks, goal)
return
def test_sub_replace_edit(self):
"""
Test that one can edit tasks through `s/old/new` notation.
Note that the original ID as a key doesn't change, even through the
embedded subID does. This is corrected on next read.
"""
self.taskdict.edit_task('3f', "s/1/3")
goal = {
TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"},
TASK1_ID: {'id': TASK3_ID, 'text': "test task 3"},
}
self.assertEqual(self.taskdict.tasks, goal)
return
def test_print(self):
"""
Test basic print functionality.
"""
self.taskdict.add_task("test task 3")
tmp_stdout = StringIO()
goal = (
"3e - test task 2\n"
"3f - test task 1\n"
"4 - test task 3\n"
)
with contextlib.redirect_stdout(tmp_stdout):
self.taskdict.print_list()
self.assertEqual(tmp_stdout.getvalue(), goal)
return
def test_print_done(self):
"""
Test the printing of the list of done tasks.
"""
self.taskdict.add_task("test task 3")
self.taskdict.finish_task('3f')
goal_task = (
"3 - test task 2\n"
"4 - test task 3\n"
)
goal_done = (
"3 - test task 1\n"
)
tmp_stdout_task = StringIO()
with contextlib.redirect_stdout(tmp_stdout_task):
self.taskdict.print_list()
self.assertEqual(tmp_stdout_task.getvalue(), goal_task)
tmp_stdout_done = StringIO()
with contextlib.redirect_stdout(tmp_stdout_done):
self.taskdict.print_list(kind='done')
self.assertEqual(tmp_stdout_done.getvalue(), goal_done)
return
def test_quiet_print(self):
"""
Test that quiet printing hides hashes.
"""
tmp_stdout = StringIO()
goal = (
"test task 2\n"
"test task 1\n"
)
with contextlib.redirect_stdout(tmp_stdout):
self.taskdict.print_list(quiet=True)
self.assertEqual(tmp_stdout.getvalue(), goal)
return
def test_grep_print(self):
"""
Test that grep_string correctly filters output.
"""
tmp_stdout = StringIO()
goal = (
"3e - test task 2\n"
)
with contextlib.redirect_stdout(tmp_stdout):
self.taskdict.print_list(grep_string='2')
self.assertEqual(tmp_stdout.getvalue(), goal)
return
def test_print_with_tags(self):
"""
Test that tags are printed when showtags=True.
"""
tmp_stdout = StringIO()
self.taskdict.add_task("test task 3", tags=['This is a test', 'two'])
goal = (
"3e - test task 2\n"
"3f - test task 1\n"
"4 - test task 3 | tags: This is a test, two\n"
)
with contextlib.redirect_stdout(tmp_stdout):
self.taskdict.print_list(showtags=True)
self.assertEqual(tmp_stdout.getvalue(), goal)
return
class IOTests(unittest.TestCase):
"""
A set of tests centered on writing to the taskfile and reading the taskfile.
"""
def setUp(self):
if os.path.isfile('tests'):
raise IOError("tests is not a directory.")
if not os.path.exists('tests'):
os.mkdir('tests')
return
def test_write_tasks_to_file(self):
"""
Check that task dictionaries are written to the task file in the
expected way.
"""
taskdict = TaskDict(taskdir='tests', name='task_test')
taskdict.add_task("test task 1")
taskdict.add_task("test task 2")
taskdict.add_task("test task 3")
taskdict.finish_task('41')
expected_line1 = f"test task 2 | id:{TASK2_ID}"
expected_line2 = f"test task 1 | id:{TASK1_ID}"
expected_done_line = f"test task 3 | id:{TASK3_ID}"
taskdict.write()
with open('tests/task_test', 'r') as test_file:
lines = test_file.readlines()
self.assertEqual(lines[0].strip(), expected_line1)
self.assertEqual(lines[1].strip(), expected_line2)
with open('tests/.task_test.done', 'r') as test_file:
lines = test_file.readlines()
self.assertEqual(lines[0].strip(), expected_done_line)
return
def test_delete_if_empty(self):
"""
Check that delete_if_empty really deletes empty taskfiles.
"""
taskdict = TaskDict(taskdir='tests', name='task_test')
taskdict.write(True)
self.assertFalse(os.path.exists('tests/task_test'))
self.assertFalse(os.path.exists('tests/.task_test.done'))
return
def test_read_tasks_from_file(self):
"""
Check that the tasks read from a taskfile are converted into a
task dictionary correctly.
"""
line1 = f"test task 2 | id:{TASK2_ID}"
line2 = f"test task 1 | id:{TASK1_ID}"
with open('tests/task_test', 'w') as test_file:
test_file.write(line1 + '\n' + line2)
taskdict = TaskDict(taskdir='tests', name='task_test')
goal = {
TASK1_ID: {'id': TASK1_ID, 'text': "test task 1"},
TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"},
}
self.assertEqual(taskdict.tasks, goal)
return
def test_read_plain_tasks_from_file(self):
"""
Check that tasks manually written to taskfile, without hash, are
interpreted and hashed upon read.
"""
line1 = "test task 1"
line2 = "test task 2"
with open('tests/task_test', 'w') as test_file:
test_file.write(line1 + '\n' + line2)
taskdict = TaskDict(taskdir='tests', name='task_test')
goal = {
TASK1_ID: {'id': TASK1_ID, 'text': "test task 1"},
TASK2_ID: {'id': TASK2_ID, 'text': "test task 2"},
}
self.assertEqual(taskdict.tasks, goal)
def tearDown(self):
if os.path.exists('tests/task_test'):
os.remove('tests/task_test')
if os.path.exists('tests/.task_test.done'):
os.remove('tests/.task_test.done')
if os.path.isdir('tests'):
os.rmdir('tests')
class BasicParserOperation(unittest.TestCase):
"""
A set of tests for the parser.
Note: these were more helpful when setting up the program, and are less
useful now. These tests probably won't grow.
"""
def test_list(self):
"Check that -l is captured"
input_args = ["-l", "othertasks"]
options = _build_parser().parse_args(input_args)
self.assertTrue(options.name == "othertasks")
def test_finish(self):
"Check that -f is captured"
input_args = ["-f", "3f"]
options = _build_parser().parse_args(input_args)
self.assertTrue(options.finish == "3f")
def test_delete_finished(self):
"Check that -D is captured"
input_args = ["-D"]
options = _build_parser().parse_args(input_args)
self.assertTrue(options.delete_finished)
class IntegrationTests(unittest.TestCase):
"""
A set of end-to-end tests.
Calls are placed by simulating main() calls on the function. The status of
the taskfile and the printed statements are checked.
"""
def tearDown(self):
if os.path.exists('integration_task_test'):
os.remove('integration_task_test')
if os.path.exists('.integration_task_test.done'):
os.remove('.integration_task_test.done')
def test_sample_run(self):
"""
Perform a sample run, testing the file status and print status at each
step.
The tasks performed are the following:
1. Add a task to the file
2. Add a second task to the file
3. Mark the second task done.
4. Edit the first task.
5. Add a task with a tag.
"""
tmp_stdout = StringIO()
print_args = ["-l", "integration_task_test"]
# Add a task to the file
input_args = ["-l", "integration_task_test", "test task 1"]
main(input_args=input_args)
with open("integration_task_test", "r") as tfile:
lines = tfile.readlines()
expected_line = f"test task 1 | id:{TASK1_ID}"
self.assertTrue(lines[0].strip() == expected_line)
self.compare_to_output(
tmp_stdout,
print_args,
expected=(
"3 - test task 1\n"
),
msg="Adding first task to file failed."
)
# Add a second task
input_args = ["-l", "integration_task_test", "test task 2"]
main(input_args=input_args)
with open("integration_task_test", "r") as tfile:
lines = tfile.readlines()
expected_line1 = f"test task 2 | id:{TASK2_ID}"
expected_line2 = f"test task 1 | id:{TASK1_ID}"
self.assertTrue(lines[0].strip() == expected_line1)
self.assertTrue(lines[1].strip() == expected_line2)
error_msg = "Adding second task failed."
expected = (
"3e - test task 2\n"
"3f - test task 1\n"
)
self.compare_to_output(tmp_stdout, print_args, expected, msg=error_msg)
# Mark second task done
input_args = ["-l", "integration_task_test", "-f", "3e"]
main(input_args=input_args)
with open("integration_task_test", "r") as tfile:
lines = tfile.readlines()
expected_line2 = f"test task 1 | id:{TASK1_ID}"
self.assertEqual(lines[0].strip(), expected_line2)
with open(".integration_task_test.done", 'r') as tfiledone:
lines = tfiledone.readlines()
expected_line1 = f"test task 2 | id:{TASK2_ID}"
self.assertEqual(lines[0].strip(), expected_line1)
self.compare_to_output(
tmp_stdout, print_args,
expected=(
"3 - test task 1\n"
),
msg="Marking second task as done failed."
)
self.compare_to_output(
tmp_stdout,
print_args + ['--done'],
expected=(
"3 - test task 2\n"
),
msg="Printing done list failed."
)
# Edit first task to fourth task
input_args = ['-l', 'integration_task_test', '-e', '3', 'test task 4']
main(input_args=input_args)
with open("integration_task_test", "r") as tfile:
lines = tfile.readlines()
expected_line = f"test task 4 | id:{TASK4_ID}"
self.assertEqual(lines[0].strip(), expected_line, msg="Edit failed.")
self.compare_to_output(
tmp_stdout,
print_args,
expected=(
"8 - test task 4\n"
),
msg="Editing of task failed."
)
# Add first task with tag
input_args = ['-l', 'integration_task_test',
'test task 1', '--tag', 'testtag']
main(input_args=input_args)
with open("integration_task_test", "r") as tfile:
lines = tfile.readlines()
expected_line1 = f"test task 1 | id:{TASK1_ID}; tags:testtag"
expected_line2 = f"test task 4 | id:{TASK4_ID}"
self.assertEqual(lines[0].strip(), expected_line1)
self.assertEqual(lines[1].strip(), expected_line2)
self.compare_to_output(
tmp_stdout,
print_args + ['--showtags'],
expected=(
"3 - test task 1 | tags: testtag\n"
"8 - test task 4\n"
),
msg="Print with showtag failed."
)
self.compare_to_output(
tmp_stdout,
print_args,
expected=(
"3 - test task 1\n"
"8 - test task 4\n"
),
msg="Print without showtag failed."
)
return
def compare_to_output(self, io_, print_args, expected, msg=''):
"""
Run main with print args and assert that the result is what is
expected. Note that this is full of side-effects. It advances main, and
populates the StringIO instance.
"""
# Empty StringIO by going to beginning and truncating the rest away
io_.seek(0)
io_.truncate(0)
with contextlib.redirect_stdout(io_):
main(input_args=print_args)
self.assertEqual(io_.getvalue(), expected, msg)
return
def do_tests():
"""
Run the test suite.
A few files are created for the test suite. If and of these files exist,
this aborts and exits with an error.
"""
FILENAMES = ['integration_task_test', '.integration_task_test.done',
'tests/task_test', 'tests/.task_test.done']
if any(os.path.exists(filename) for filename in FILENAMES):
raise IOError("One of the test_task files already exists. "
"Aborting to not overwrite.")
unittest.main(verbosity=2)
if __name__ == "__main__":
do_tests()