All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
10 from .task import TaskWarrior, Task
13 class TasklibTest(unittest.TestCase):
16 self.tmp = tempfile.mkdtemp(dir='.')
17 self.tw = TaskWarrior(data_location=self.tmp)
20 shutil.rmtree(self.tmp)
23 class TaskFilterTest(TasklibTest):
25 def test_all_empty(self):
26 self.assertEqual(len(self.tw.tasks.all()), 0)
28 def test_all_non_empty(self):
29 Task(self.tw, description="test task").save()
30 self.assertEqual(len(self.tw.tasks.all()), 1)
31 self.assertEqual(self.tw.tasks.all()[0]['description'], 'test task')
32 self.assertEqual(self.tw.tasks.all()[0]['status'], 'pending')
34 def test_pending_non_empty(self):
35 Task(self.tw, description="test task").save()
36 self.assertEqual(len(self.tw.tasks.pending()), 1)
37 self.assertEqual(self.tw.tasks.pending()[0]['description'],
39 self.assertEqual(self.tw.tasks.pending()[0]['status'], 'pending')
41 def test_completed_empty(self):
42 Task(self.tw, description="test task").save()
43 self.assertEqual(len(self.tw.tasks.completed()), 0)
45 def test_completed_non_empty(self):
46 Task(self.tw, description="test task").save()
47 self.assertEqual(len(self.tw.tasks.completed()), 0)
48 self.tw.tasks.all()[0].done()
49 self.assertEqual(len(self.tw.tasks.completed()), 1)
51 def test_filtering_by_attribute(self):
52 Task(self.tw, description="no priority task").save()
53 Task(self.tw, priority="H", description="high priority task").save()
54 self.assertEqual(len(self.tw.tasks.all()), 2)
56 # Assert that the correct number of tasks is returned
57 self.assertEqual(len(self.tw.tasks.filter(priority="H")), 1)
59 # Assert that the correct tasks are returned
60 high_priority_task = self.tw.tasks.get(priority="H")
61 self.assertEqual(high_priority_task['description'], "high priority task")
63 def test_filtering_by_empty_attribute(self):
64 Task(self.tw, description="no priority task").save()
65 Task(self.tw, priority="H", description="high priority task").save()
66 self.assertEqual(len(self.tw.tasks.all()), 2)
68 # Assert that the correct number of tasks is returned
69 self.assertEqual(len(self.tw.tasks.filter(priority=None)), 1)
71 # Assert that the correct tasks are returned
72 no_priority_task = self.tw.tasks.get(priority=None)
73 self.assertEqual(no_priority_task['description'], "no priority task")
75 def test_filter_for_task_with_space_in_descripition(self):
76 task = Task(self.tw, description="test task")
79 filtered_task = self.tw.tasks.get(description="test task")
80 self.assertEqual(filtered_task['description'], "test task")
82 def test_filter_for_task_without_space_in_descripition(self):
83 task = Task(self.tw, description="test")
86 filtered_task = self.tw.tasks.get(description="test")
87 self.assertEqual(filtered_task['description'], "test")
89 def test_filter_for_task_with_space_in_project(self):
90 task = Task(self.tw, description="test", project="random project")
93 filtered_task = self.tw.tasks.get(project="random project")
94 self.assertEqual(filtered_task['project'], "random project")
96 def test_filter_for_task_without_space_in_project(self):
97 task = Task(self.tw, description="test", project="random")
100 filtered_task = self.tw.tasks.get(project="random")
101 self.assertEqual(filtered_task['project'], "random")
104 class TaskTest(TasklibTest):
106 def test_create_unsaved_task(self):
107 # Make sure a new task is not saved unless explicitly called for
108 t = Task(self.tw, description="test task")
109 self.assertEqual(len(self.tw.tasks.all()), 0)
111 # TODO: once python 2.6 compatiblity is over, use context managers here
112 # and in all subsequent tests for assertRaises
114 def test_delete_unsaved_task(self):
115 t = Task(self.tw, description="test task")
116 self.assertRaises(Task.NotSaved, t.delete)
118 def test_complete_unsaved_task(self):
119 t = Task(self.tw, description="test task")
120 self.assertRaises(Task.NotSaved, t.done)
122 def test_refresh_unsaved_task(self):
123 t = Task(self.tw, description="test task")
124 self.assertRaises(Task.NotSaved, t.refresh)
126 def test_delete_deleted_task(self):
127 t = Task(self.tw, description="test task")
131 self.assertRaises(Task.DeletedTask, t.delete)
133 def test_complete_completed_task(self):
134 t = Task(self.tw, description="test task")
138 self.assertRaises(Task.CompletedTask, t.done)
140 def test_complete_deleted_task(self):
141 t = Task(self.tw, description="test task")
145 self.assertRaises(Task.DeletedTask, t.done)
147 def test_modify_simple_attribute_without_space(self):
148 t = Task(self.tw, description="test")
151 self.assertEquals(t['description'], "test")
153 t['description'] = "test-modified"
156 self.assertEquals(t['description'], "test-modified")
158 def test_modify_simple_attribute_with_space(self):
159 # Space can pose problems with parsing
160 t = Task(self.tw, description="test task")
163 self.assertEquals(t['description'], "test task")
165 t['description'] = "test task modified"
168 self.assertEquals(t['description'], "test task modified")
170 def test_empty_dependency_set_of_unsaved_task(self):
171 t = Task(self.tw, description="test task")
172 self.assertEqual(t['depends'], set())
174 def test_empty_dependency_set_of_saved_task(self):
175 t = Task(self.tw, description="test task")
177 self.assertEqual(t['depends'], set())
179 def test_set_unsaved_task_as_dependency(self):
180 # Adds only one dependency to task with no dependencies
181 t = Task(self.tw, description="test task")
182 dependency = Task(self.tw, description="needs to be done first")
184 # We only save the parent task, dependency task is unsaved
186 t['depends'] = set([dependency])
188 self.assertRaises(Task.NotSaved, t.save)
190 def test_set_simple_dependency_set(self):
191 # Adds only one dependency to task with no dependencies
192 t = Task(self.tw, description="test task")
193 dependency = Task(self.tw, description="needs to be done first")
198 t['depends'] = set([dependency])
200 self.assertEqual(t['depends'], set([dependency]))
202 def test_set_complex_dependency_set(self):
203 # Adds two dependencies to task with no dependencies
204 t = Task(self.tw, description="test task")
205 dependency1 = Task(self.tw, description="needs to be done first")
206 dependency2 = Task(self.tw, description="needs to be done second")
212 t['depends'] = set([dependency1, dependency2])
214 self.assertEqual(t['depends'], set([dependency1, dependency2]))
216 def test_remove_from_dependency_set(self):
217 # Removes dependency from task with two dependencies
218 t = Task(self.tw, description="test task")
219 dependency1 = Task(self.tw, description="needs to be done first")
220 dependency2 = Task(self.tw, description="needs to be done second")
225 t['depends'] = set([dependency1, dependency2])
228 t['depends'].remove(dependency2)
231 self.assertEqual(t['depends'], set([dependency1]))
233 def test_add_to_dependency_set(self):
234 # Adds dependency to task with one dependencies
235 t = Task(self.tw, description="test task")
236 dependency1 = Task(self.tw, description="needs to be done first")
237 dependency2 = Task(self.tw, description="needs to be done second")
242 t['depends'] = set([dependency1])
245 t['depends'].add(dependency2)
248 self.assertEqual(t['depends'], set([dependency1, dependency2]))
250 def test_add_to_empty_dependency_set(self):
251 # Adds dependency to task with one dependencies
252 t = Task(self.tw, description="test task")
253 dependency = Task(self.tw, description="needs to be done first")
257 t['depends'].add(dependency)
260 self.assertEqual(t['depends'], set([dependency]))
262 def test_simple_dependency_set_save_repeatedly(self):
263 # Adds only one dependency to task with no dependencies
264 t = Task(self.tw, description="test task")
265 dependency = Task(self.tw, description="needs to be done first")
268 t['depends'] = set([dependency])
271 # We taint the task, but keep depends intact
272 t['description'] = "test task modified"
275 self.assertEqual(t['depends'], set([dependency]))
277 # We taint the task, but assign the same set to the depends
278 t['depends'] = set([dependency])
279 t['description'] = "test task modified again"
282 self.assertEqual(t['depends'], set([dependency]))
284 def test_compare_different_tasks(self):
285 # Negative: compare two different tasks
286 t1 = Task(self.tw, description="test task")
287 t2 = Task(self.tw, description="test task")
292 self.assertEqual(t1 == t2, False)
294 def test_compare_same_task_object(self):
295 # Compare Task object wit itself
296 t = Task(self.tw, description="test task")
299 self.assertEqual(t == t, True)
301 def test_compare_same_task(self):
302 # Compare the same task using two different objects
303 t1 = Task(self.tw, description="test task")
306 t2 = self.tw.tasks.get(uuid=t1['uuid'])
307 self.assertEqual(t1 == t2, True)
309 def test_compare_unsaved_tasks(self):
310 # t1 and t2 are unsaved tasks, considered to be unequal
311 # despite the content of data
312 t1 = Task(self.tw, description="test task")
313 t2 = Task(self.tw, description="test task")
315 self.assertEqual(t1 == t2, False)
317 def test_hash_unsaved_tasks(self):
318 # Considered equal, it's the same object
319 t1 = Task(self.tw, description="test task")
321 self.assertEqual(hash(t1) == hash(t2), True)
323 def test_hash_same_task(self):
324 # Compare the hash of the task using two different objects
325 t1 = Task(self.tw, description="test task")
328 t2 = self.tw.tasks.get(uuid=t1['uuid'])
329 self.assertEqual(t1.__hash__(), t2.__hash__())
331 def test_adding_task_with_priority(self):
332 t = Task(self.tw, description="test task", priority="M")
335 def test_removing_priority_with_none(self):
336 t = Task(self.tw, description="test task", priority="L")
339 # Remove the priority mark
343 # Assert that priority is not there after saving
344 self.assertEqual(t['priority'], None)
346 def test_adding_task_with_due_time(self):
347 t = Task(self.tw, description="test task", due=datetime.datetime.now())
350 def test_removing_due_time_with_none(self):
351 t = Task(self.tw, description="test task", due=datetime.datetime.now())
354 # Remove the due timestamp
358 # Assert that due timestamp is no longer there
359 self.assertEqual(t['due'], None)
361 def test_modified_fields_new_task(self):
364 # This should be empty with new task
365 self.assertEqual(set(t._modified_fields), set())
368 t['description'] = "test task"
369 self.assertEqual(set(t._modified_fields), set(['description']))
371 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
372 self.assertEqual(set(t._modified_fields), set(['description', 'due']))
374 t['project'] = "test project"
375 self.assertEqual(set(t._modified_fields),
376 set(['description', 'due', 'project']))
378 # List of modified fields should clear out when saved
380 self.assertEqual(set(t._modified_fields), set())
382 # Reassigning the fields with the same values now should not produce
384 t['description'] = "test task"
385 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
386 t['project'] = "test project"
387 self.assertEqual(set(t._modified_fields), set())
389 def test_modified_fields_loaded_task(self):
393 t['description'] = "test task"
394 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
395 t['project'] = "test project"
397 dependency = Task(self.tw, description="dependency")
399 t['depends'] = set([dependency])
401 # List of modified fields should clear out when saved
403 self.assertEqual(set(t._modified_fields), set())
405 # Get the task by using a filter by UUID
406 t2 = self.tw.tasks.get(uuid=t['uuid'])
408 # Reassigning the fields with the same values now should not produce
410 t['description'] = "test task"
411 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
412 t['project'] = "test project"
413 t['depends'] = set([dependency])
414 self.assertEqual(set(t._modified_fields), set())
416 def test_setting_read_only_attrs_through_init(self):
417 # Test that we are unable to set readonly attrs through __init__
418 for readonly_key in Task.read_only_fields:
419 kwargs = {'description': 'test task', readonly_key: 'value'}
420 self.assertRaises(RuntimeError,
421 lambda: Task(self.tw, **kwargs))
423 def test_setting_read_only_attrs_through_setitem(self):
424 # Test that we are unable to set readonly attrs through __init__
425 for readonly_key in Task.read_only_fields:
426 t = Task(self.tw, description='test task')
427 self.assertRaises(RuntimeError,
428 lambda: t.__setitem__(readonly_key, 'value'))
430 def test_saving_unmodified_task(self):
431 t = Task(self.tw, description="test task")
435 def test_adding_tag_by_appending(self):
436 t = Task(self.tw, description="test task", tags=['test1'])
438 t['tags'].append('test2')
440 self.assertEqual(t['tags'], ['test1', 'test2'])
442 def test_adding_tag_by_appending_empty(self):
443 t = Task(self.tw, description="test task")
445 t['tags'].append('test')
447 self.assertEqual(t['tags'], ['test'])
450 class TaskFromHookTest(TasklibTest):
452 input_add_data = six.StringIO(
453 '{"description":"Buy some milk",'
454 '"entry":"20141118T050231Z",'
455 '"status":"pending",'
456 '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
458 input_modify_data = six.StringIO(input_add_data.getvalue() + '\n' +
459 '{"description":"Buy some milk finally",'
460 '"entry":"20141118T050231Z",'
461 '"status":"completed",'
462 '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
464 exported_raw_data = (
466 '"due":"20150101T232323Z",'
467 '"description":"test task"}')
469 def test_setting_up_from_add_hook_input(self):
470 t = Task.from_input(input_file=self.input_add_data)
471 self.assertEqual(t['description'], "Buy some milk")
472 self.assertEqual(t.pending, True)
474 def test_setting_up_from_modified_hook_input(self):
475 t = Task.from_input(input_file=self.input_modify_data, modify=True)
476 self.assertEqual(t['description'], "Buy some milk finally")
477 self.assertEqual(t.pending, False)
478 self.assertEqual(t.completed, True)
480 self.assertEqual(t._original_data['status'], "pending")
481 self.assertEqual(t._original_data['description'], "Buy some milk")
482 self.assertEqual(set(t._modified_fields),
483 set(['status', 'description']))
485 def test_export_data(self):
486 t = Task(self.tw, description="test task",
487 project="Home", due=datetime.datetime(2015,1,1,23,23,23))
489 # Check that the output is a permutation of:
490 # {"project":"Home","description":"test task","due":"20150101232323Z"}
491 allowed_segments = self.exported_raw_data[1:-1].split(',')
493 '{' + ','.join(segments) + '}'
494 for segments in itertools.permutations(allowed_segments)
497 self.assertTrue(any(t.export_data() == expected
498 for expected in allowed_output))
501 class AnnotationTest(TasklibTest):
504 super(AnnotationTest, self).setUp()
505 Task(self.tw, description="test task").save()
507 def test_adding_annotation(self):
508 task = self.tw.tasks.get()
509 task.add_annotation('test annotation')
510 self.assertEqual(len(task['annotations']), 1)
511 ann = task['annotations'][0]
512 self.assertEqual(ann['description'], 'test annotation')
514 def test_removing_annotation(self):
515 task = self.tw.tasks.get()
516 task.add_annotation('test annotation')
517 ann = task['annotations'][0]
519 self.assertEqual(len(task['annotations']), 0)
521 def test_removing_annotation_by_description(self):
522 task = self.tw.tasks.get()
523 task.add_annotation('test annotation')
524 task.remove_annotation('test annotation')
525 self.assertEqual(len(task['annotations']), 0)
527 def test_removing_annotation_by_obj(self):
528 task = self.tw.tasks.get()
529 task.add_annotation('test annotation')
530 ann = task['annotations'][0]
531 task.remove_annotation(ann)
532 self.assertEqual(len(task['annotations']), 0)
534 def test_annotation_after_modification(self):
535 task = self.tw.tasks.get()
536 task['project'] = 'test'
537 task.add_annotation('I should really do this task')
538 self.assertEqual(task['project'], 'test')
540 self.assertEqual(task['project'], 'test')
543 class UnicodeTest(TasklibTest):
545 def test_unicode_task(self):
546 Task(self.tw, description="†åßk").save()
549 def test_non_unicode_task(self):
550 Task(self.tw, description="test task").save()