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.
12 from .task import TaskWarrior, Task
14 # http://taskwarrior.org/docs/design/task.html , Section: The Attributes
15 TASK_STANDARD_ATTRS = (
38 class TasklibTest(unittest.TestCase):
41 self.tmp = tempfile.mkdtemp(dir='.')
42 self.tw = TaskWarrior(data_location=self.tmp)
45 shutil.rmtree(self.tmp)
48 class TaskFilterTest(TasklibTest):
50 def test_all_empty(self):
51 self.assertEqual(len(self.tw.tasks.all()), 0)
53 def test_all_non_empty(self):
54 Task(self.tw, description="test task").save()
55 self.assertEqual(len(self.tw.tasks.all()), 1)
56 self.assertEqual(self.tw.tasks.all()[0]['description'], 'test task')
57 self.assertEqual(self.tw.tasks.all()[0]['status'], 'pending')
59 def test_pending_non_empty(self):
60 Task(self.tw, description="test task").save()
61 self.assertEqual(len(self.tw.tasks.pending()), 1)
62 self.assertEqual(self.tw.tasks.pending()[0]['description'],
64 self.assertEqual(self.tw.tasks.pending()[0]['status'], 'pending')
66 def test_completed_empty(self):
67 Task(self.tw, description="test task").save()
68 self.assertEqual(len(self.tw.tasks.completed()), 0)
70 def test_completed_non_empty(self):
71 Task(self.tw, description="test task").save()
72 self.assertEqual(len(self.tw.tasks.completed()), 0)
73 self.tw.tasks.all()[0].done()
74 self.assertEqual(len(self.tw.tasks.completed()), 1)
76 def test_filtering_by_attribute(self):
77 Task(self.tw, description="no priority task").save()
78 Task(self.tw, priority="H", description="high priority task").save()
79 self.assertEqual(len(self.tw.tasks.all()), 2)
81 # Assert that the correct number of tasks is returned
82 self.assertEqual(len(self.tw.tasks.filter(priority="H")), 1)
84 # Assert that the correct tasks are returned
85 high_priority_task = self.tw.tasks.get(priority="H")
86 self.assertEqual(high_priority_task['description'], "high priority task")
88 def test_filtering_by_empty_attribute(self):
89 Task(self.tw, description="no priority task").save()
90 Task(self.tw, priority="H", description="high priority task").save()
91 self.assertEqual(len(self.tw.tasks.all()), 2)
93 # Assert that the correct number of tasks is returned
94 self.assertEqual(len(self.tw.tasks.filter(priority=None)), 1)
96 # Assert that the correct tasks are returned
97 no_priority_task = self.tw.tasks.get(priority=None)
98 self.assertEqual(no_priority_task['description'], "no priority task")
100 def test_filter_for_task_with_space_in_descripition(self):
101 task = Task(self.tw, description="test task")
104 filtered_task = self.tw.tasks.get(description="test task")
105 self.assertEqual(filtered_task['description'], "test task")
107 def test_filter_for_task_without_space_in_descripition(self):
108 task = Task(self.tw, description="test")
111 filtered_task = self.tw.tasks.get(description="test")
112 self.assertEqual(filtered_task['description'], "test")
114 def test_filter_for_task_with_space_in_project(self):
115 task = Task(self.tw, description="test", project="random project")
118 filtered_task = self.tw.tasks.get(project="random project")
119 self.assertEqual(filtered_task['project'], "random project")
121 def test_filter_for_task_without_space_in_project(self):
122 task = Task(self.tw, description="test", project="random")
125 filtered_task = self.tw.tasks.get(project="random")
126 self.assertEqual(filtered_task['project'], "random")
129 class TaskTest(TasklibTest):
131 def test_create_unsaved_task(self):
132 # Make sure a new task is not saved unless explicitly called for
133 t = Task(self.tw, description="test task")
134 self.assertEqual(len(self.tw.tasks.all()), 0)
136 # TODO: once python 2.6 compatiblity is over, use context managers here
137 # and in all subsequent tests for assertRaises
139 def test_delete_unsaved_task(self):
140 t = Task(self.tw, description="test task")
141 self.assertRaises(Task.NotSaved, t.delete)
143 def test_complete_unsaved_task(self):
144 t = Task(self.tw, description="test task")
145 self.assertRaises(Task.NotSaved, t.done)
147 def test_refresh_unsaved_task(self):
148 t = Task(self.tw, description="test task")
149 self.assertRaises(Task.NotSaved, t.refresh)
151 def test_delete_deleted_task(self):
152 t = Task(self.tw, description="test task")
156 self.assertRaises(Task.DeletedTask, t.delete)
158 def test_complete_completed_task(self):
159 t = Task(self.tw, description="test task")
163 self.assertRaises(Task.CompletedTask, t.done)
165 def test_complete_deleted_task(self):
166 t = Task(self.tw, description="test task")
170 self.assertRaises(Task.DeletedTask, t.done)
172 def test_modify_simple_attribute_without_space(self):
173 t = Task(self.tw, description="test")
176 self.assertEquals(t['description'], "test")
178 t['description'] = "test-modified"
181 self.assertEquals(t['description'], "test-modified")
183 def test_modify_simple_attribute_with_space(self):
184 # Space can pose problems with parsing
185 t = Task(self.tw, description="test task")
188 self.assertEquals(t['description'], "test task")
190 t['description'] = "test task modified"
193 self.assertEquals(t['description'], "test task modified")
195 def test_empty_dependency_set_of_unsaved_task(self):
196 t = Task(self.tw, description="test task")
197 self.assertEqual(t['depends'], set())
199 def test_empty_dependency_set_of_saved_task(self):
200 t = Task(self.tw, description="test task")
202 self.assertEqual(t['depends'], set())
204 def test_set_unsaved_task_as_dependency(self):
205 # Adds only one dependency to task with no dependencies
206 t = Task(self.tw, description="test task")
207 dependency = Task(self.tw, description="needs to be done first")
209 # We only save the parent task, dependency task is unsaved
211 t['depends'] = set([dependency])
213 self.assertRaises(Task.NotSaved, t.save)
215 def test_set_simple_dependency_set(self):
216 # Adds only one dependency to task with no dependencies
217 t = Task(self.tw, description="test task")
218 dependency = Task(self.tw, description="needs to be done first")
223 t['depends'] = set([dependency])
225 self.assertEqual(t['depends'], set([dependency]))
227 def test_set_complex_dependency_set(self):
228 # Adds two dependencies to task with no dependencies
229 t = Task(self.tw, description="test task")
230 dependency1 = Task(self.tw, description="needs to be done first")
231 dependency2 = Task(self.tw, description="needs to be done second")
237 t['depends'] = set([dependency1, dependency2])
239 self.assertEqual(t['depends'], set([dependency1, dependency2]))
241 def test_remove_from_dependency_set(self):
242 # Removes dependency from task with two dependencies
243 t = Task(self.tw, description="test task")
244 dependency1 = Task(self.tw, description="needs to be done first")
245 dependency2 = Task(self.tw, description="needs to be done second")
250 t['depends'] = set([dependency1, dependency2])
253 t['depends'].remove(dependency2)
256 self.assertEqual(t['depends'], set([dependency1]))
258 def test_add_to_dependency_set(self):
259 # Adds dependency to task with one dependencies
260 t = Task(self.tw, description="test task")
261 dependency1 = Task(self.tw, description="needs to be done first")
262 dependency2 = Task(self.tw, description="needs to be done second")
267 t['depends'] = set([dependency1])
270 t['depends'].add(dependency2)
273 self.assertEqual(t['depends'], set([dependency1, dependency2]))
275 def test_add_to_empty_dependency_set(self):
276 # Adds dependency to task with one dependencies
277 t = Task(self.tw, description="test task")
278 dependency = Task(self.tw, description="needs to be done first")
282 t['depends'].add(dependency)
285 self.assertEqual(t['depends'], set([dependency]))
287 def test_simple_dependency_set_save_repeatedly(self):
288 # Adds only one dependency to task with no dependencies
289 t = Task(self.tw, description="test task")
290 dependency = Task(self.tw, description="needs to be done first")
293 t['depends'] = set([dependency])
296 # We taint the task, but keep depends intact
297 t['description'] = "test task modified"
300 self.assertEqual(t['depends'], set([dependency]))
302 # We taint the task, but assign the same set to the depends
303 t['depends'] = set([dependency])
304 t['description'] = "test task modified again"
307 self.assertEqual(t['depends'], set([dependency]))
309 def test_compare_different_tasks(self):
310 # Negative: compare two different tasks
311 t1 = Task(self.tw, description="test task")
312 t2 = Task(self.tw, description="test task")
317 self.assertEqual(t1 == t2, False)
319 def test_compare_same_task_object(self):
320 # Compare Task object wit itself
321 t = Task(self.tw, description="test task")
324 self.assertEqual(t == t, True)
326 def test_compare_same_task(self):
327 # Compare the same task using two different objects
328 t1 = Task(self.tw, description="test task")
331 t2 = self.tw.tasks.get(uuid=t1['uuid'])
332 self.assertEqual(t1 == t2, True)
334 def test_compare_unsaved_tasks(self):
335 # t1 and t2 are unsaved tasks, considered to be unequal
336 # despite the content of data
337 t1 = Task(self.tw, description="test task")
338 t2 = Task(self.tw, description="test task")
340 self.assertEqual(t1 == t2, False)
342 def test_hash_unsaved_tasks(self):
343 # Considered equal, it's the same object
344 t1 = Task(self.tw, description="test task")
346 self.assertEqual(hash(t1) == hash(t2), True)
348 def test_hash_same_task(self):
349 # Compare the hash of the task using two different objects
350 t1 = Task(self.tw, description="test task")
353 t2 = self.tw.tasks.get(uuid=t1['uuid'])
354 self.assertEqual(t1.__hash__(), t2.__hash__())
356 def test_adding_task_with_priority(self):
357 t = Task(self.tw, description="test task", priority="M")
360 def test_removing_priority_with_none(self):
361 t = Task(self.tw, description="test task", priority="L")
364 # Remove the priority mark
368 # Assert that priority is not there after saving
369 self.assertEqual(t['priority'], None)
371 def test_adding_task_with_due_time(self):
372 t = Task(self.tw, description="test task", due=datetime.datetime.now())
375 def test_removing_due_time_with_none(self):
376 t = Task(self.tw, description="test task", due=datetime.datetime.now())
379 # Remove the due timestamp
383 # Assert that due timestamp is no longer there
384 self.assertEqual(t['due'], None)
386 def test_modified_fields_new_task(self):
389 # This should be empty with new task
390 self.assertEqual(set(t._modified_fields), set())
393 t['description'] = "test task"
394 self.assertEqual(set(t._modified_fields), set(['description']))
396 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
397 self.assertEqual(set(t._modified_fields), set(['description', 'due']))
399 t['project'] = "test project"
400 self.assertEqual(set(t._modified_fields),
401 set(['description', 'due', 'project']))
403 # List of modified fields should clear out when saved
405 self.assertEqual(set(t._modified_fields), set())
407 # Reassigning the fields with the same values now should not produce
409 t['description'] = "test task"
410 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
411 t['project'] = "test project"
412 self.assertEqual(set(t._modified_fields), set())
414 def test_modified_fields_loaded_task(self):
418 t['description'] = "test task"
419 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
420 t['project'] = "test project"
422 dependency = Task(self.tw, description="dependency")
424 t['depends'] = set([dependency])
426 # List of modified fields should clear out when saved
428 self.assertEqual(set(t._modified_fields), set())
430 # Get the task by using a filter by UUID
431 t2 = self.tw.tasks.get(uuid=t['uuid'])
433 # Reassigning the fields with the same values now should not produce
435 t['description'] = "test task"
436 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
437 t['project'] = "test project"
438 t['depends'] = set([dependency])
439 self.assertEqual(set(t._modified_fields), set())
441 def test_modified_fields_not_affected_by_reading(self):
444 for field in TASK_STANDARD_ATTRS:
447 self.assertEqual(set(t._modified_fields), set())
449 def test_setting_read_only_attrs_through_init(self):
450 # Test that we are unable to set readonly attrs through __init__
451 for readonly_key in Task.read_only_fields:
452 kwargs = {'description': 'test task', readonly_key: 'value'}
453 self.assertRaises(RuntimeError,
454 lambda: Task(self.tw, **kwargs))
456 def test_setting_read_only_attrs_through_setitem(self):
457 # Test that we are unable to set readonly attrs through __init__
458 for readonly_key in Task.read_only_fields:
459 t = Task(self.tw, description='test task')
460 self.assertRaises(RuntimeError,
461 lambda: t.__setitem__(readonly_key, 'value'))
463 def test_saving_unmodified_task(self):
464 t = Task(self.tw, description="test task")
468 def test_adding_tag_by_appending(self):
469 t = Task(self.tw, description="test task", tags=['test1'])
471 t['tags'].append('test2')
473 self.assertEqual(t['tags'], ['test1', 'test2'])
475 def test_adding_tag_by_appending_empty(self):
476 t = Task(self.tw, description="test task")
478 t['tags'].append('test')
480 self.assertEqual(t['tags'], ['test'])
482 def test_serializers_returning_empty_string_for_none(self):
483 # Test that any serializer returns '' when passed None
485 serializers = [getattr(t, serializer_name) for serializer_name in
486 filter(lambda x: x.startswith('serialize_'), dir(t))]
487 for serializer in serializers:
488 self.assertEqual(serializer(None), '')
490 def test_deserializer_returning_empty_value_for_empty_string(self):
491 # Test that any deserializer returns empty value when passed ''
493 deserializers = [getattr(t, deserializer_name) for deserializer_name in
494 filter(lambda x: x.startswith('deserialize_'), dir(t))]
495 for deserializer in deserializers:
496 self.assertTrue(deserializer('') in (None, [], set()))
499 class TaskFromHookTest(TasklibTest):
501 input_add_data = six.StringIO(
502 '{"description":"Buy some milk",'
503 '"entry":"20141118T050231Z",'
504 '"status":"pending",'
505 '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
507 input_modify_data = six.StringIO(input_add_data.getvalue() + '\n' +
508 '{"description":"Buy some milk finally",'
509 '"entry":"20141118T050231Z",'
510 '"status":"completed",'
511 '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
513 exported_raw_data = (
515 '"due":"20150101T232323Z",'
516 '"description":"test task"}')
518 def test_setting_up_from_add_hook_input(self):
519 t = Task.from_input(input_file=self.input_add_data)
520 self.assertEqual(t['description'], "Buy some milk")
521 self.assertEqual(t.pending, True)
523 def test_setting_up_from_modified_hook_input(self):
524 t = Task.from_input(input_file=self.input_modify_data, modify=True)
525 self.assertEqual(t['description'], "Buy some milk finally")
526 self.assertEqual(t.pending, False)
527 self.assertEqual(t.completed, True)
529 self.assertEqual(t._original_data['status'], "pending")
530 self.assertEqual(t._original_data['description'], "Buy some milk")
531 self.assertEqual(set(t._modified_fields),
532 set(['status', 'description']))
534 def test_export_data(self):
535 t = Task(self.tw, description="test task",
537 due=pytz.utc.localize(datetime.datetime(2015,1,1,23,23,23)))
539 # Check that the output is a permutation of:
540 # {"project":"Home","description":"test task","due":"20150101232323Z"}
541 allowed_segments = self.exported_raw_data[1:-1].split(',')
543 '{' + ','.join(segments) + '}'
544 for segments in itertools.permutations(allowed_segments)
547 self.assertTrue(any(t.export_data() == expected
548 for expected in allowed_output))
551 class AnnotationTest(TasklibTest):
554 super(AnnotationTest, self).setUp()
555 Task(self.tw, description="test task").save()
557 def test_adding_annotation(self):
558 task = self.tw.tasks.get()
559 task.add_annotation('test annotation')
560 self.assertEqual(len(task['annotations']), 1)
561 ann = task['annotations'][0]
562 self.assertEqual(ann['description'], 'test annotation')
564 def test_removing_annotation(self):
565 task = self.tw.tasks.get()
566 task.add_annotation('test annotation')
567 ann = task['annotations'][0]
569 self.assertEqual(len(task['annotations']), 0)
571 def test_removing_annotation_by_description(self):
572 task = self.tw.tasks.get()
573 task.add_annotation('test annotation')
574 task.remove_annotation('test annotation')
575 self.assertEqual(len(task['annotations']), 0)
577 def test_removing_annotation_by_obj(self):
578 task = self.tw.tasks.get()
579 task.add_annotation('test annotation')
580 ann = task['annotations'][0]
581 task.remove_annotation(ann)
582 self.assertEqual(len(task['annotations']), 0)
584 def test_annotation_after_modification(self):
585 task = self.tw.tasks.get()
586 task['project'] = 'test'
587 task.add_annotation('I should really do this task')
588 self.assertEqual(task['project'], 'test')
590 self.assertEqual(task['project'], 'test')
592 def test_serialize_annotations(self):
593 # Test that serializing annotations is possible
594 t = Task(self.tw, description="test")
597 t.add_annotation("annotation1")
598 t.add_annotation("annotation2")
600 data = t._serialize('annotations', t._data['annotations'])
602 self.assertEqual(len(data), 2)
603 self.assertEqual(type(data[0]), dict)
604 self.assertEqual(type(data[1]), dict)
606 self.assertEqual(data[0]['description'], "annotation1")
607 self.assertEqual(data[1]['description'], "annotation2")
610 class UnicodeTest(TasklibTest):
612 def test_unicode_task(self):
613 Task(self.tw, description="†åßk").save()
616 def test_non_unicode_task(self):
617 Task(self.tw, description="test task").save()