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, local_zone, DATE_FORMAT
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()))
498 def test_normalizers_returning_empty_string_for_none(self):
499 # Test that any normalizer can handle None as a valid value
501 normalizers = [getattr(t, normalizer_name) for normalizer_name in
502 filter(lambda x: x.startswith('normalize_'), dir(t))]
503 for normalizer in normalizers:
507 class TaskFromHookTest(TasklibTest):
509 input_add_data = six.StringIO(
510 '{"description":"Buy some milk",'
511 '"entry":"20141118T050231Z",'
512 '"status":"pending",'
513 '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
515 input_modify_data = six.StringIO(input_add_data.getvalue() + '\n' +
516 '{"description":"Buy some milk finally",'
517 '"entry":"20141118T050231Z",'
518 '"status":"completed",'
519 '"uuid":"a360fc44-315c-4366-b70c-ea7e7520b749"}')
521 exported_raw_data = (
523 '"due":"20150101T232323Z",'
524 '"description":"test task"}')
526 def test_setting_up_from_add_hook_input(self):
527 t = Task.from_input(input_file=self.input_add_data)
528 self.assertEqual(t['description'], "Buy some milk")
529 self.assertEqual(t.pending, True)
531 def test_setting_up_from_modified_hook_input(self):
532 t = Task.from_input(input_file=self.input_modify_data, modify=True)
533 self.assertEqual(t['description'], "Buy some milk finally")
534 self.assertEqual(t.pending, False)
535 self.assertEqual(t.completed, True)
537 self.assertEqual(t._original_data['status'], "pending")
538 self.assertEqual(t._original_data['description'], "Buy some milk")
539 self.assertEqual(set(t._modified_fields),
540 set(['status', 'description']))
542 def test_export_data(self):
543 t = Task(self.tw, description="test task",
545 due=pytz.utc.localize(datetime.datetime(2015,1,1,23,23,23)))
547 # Check that the output is a permutation of:
548 # {"project":"Home","description":"test task","due":"20150101232323Z"}
549 allowed_segments = self.exported_raw_data[1:-1].split(',')
551 '{' + ','.join(segments) + '}'
552 for segments in itertools.permutations(allowed_segments)
555 self.assertTrue(any(t.export_data() == expected
556 for expected in allowed_output))
558 class TimezoneAwareDatetimeTest(TasklibTest):
561 super(TimezoneAwareDatetimeTest, self).setUp()
562 self.zone = local_zone
563 self.localdate_naive = datetime.datetime(2015,2,2)
564 self.localtime_naive = datetime.datetime(2015,2,2,0,0,0)
565 self.localtime_aware = self.zone.localize(self.localtime_naive)
566 self.utctime_aware = self.localtime_aware.astimezone(pytz.utc)
568 def test_timezone_naive_datetime_setitem(self):
569 t = Task(self.tw, description="test task")
570 t['due'] = self.localtime_naive
571 self.assertEqual(t['due'], self.localtime_aware)
573 def test_timezone_naive_datetime_using_init(self):
574 t = Task(self.tw, description="test task", due=self.localtime_naive)
575 self.assertEqual(t['due'], self.localtime_aware)
577 def test_filter_by_naive_datetime(self):
578 t = Task(self.tw, description="task1", due=self.localtime_naive)
580 matching_tasks = self.tw.tasks.filter(due=self.localtime_naive)
581 self.assertEqual(len(matching_tasks), 1)
583 def test_serialize_naive_datetime(self):
584 t = Task(self.tw, description="task1", due=self.localtime_naive)
585 self.assertEqual(json.loads(t.export_data())['due'],
586 self.utctime_aware.strftime(DATE_FORMAT))
588 def test_timezone_naive_date_setitem(self):
589 t = Task(self.tw, description="test task")
590 t['due'] = self.localdate_naive
591 self.assertEqual(t['due'], self.localtime_aware)
593 def test_timezone_naive_date_using_init(self):
594 t = Task(self.tw, description="test task", due=self.localdate_naive)
595 self.assertEqual(t['due'], self.localtime_aware)
597 def test_filter_by_naive_date(self):
598 t = Task(self.tw, description="task1", due=self.localdate_naive)
600 matching_tasks = self.tw.tasks.filter(due=self.localdate_naive)
601 self.assertEqual(len(matching_tasks), 1)
603 def test_serialize_naive_date(self):
604 t = Task(self.tw, description="task1", due=self.localdate_naive)
605 self.assertEqual(json.loads(t.export_data())['due'],
606 self.utctime_aware.strftime(DATE_FORMAT))
608 def test_timezone_aware_datetime_setitem(self):
609 t = Task(self.tw, description="test task")
610 t['due'] = self.localtime_aware
611 self.assertEqual(t['due'], self.localtime_aware)
613 def test_timezone_aware_datetime_using_init(self):
614 t = Task(self.tw, description="test task", due=self.localtime_aware)
615 self.assertEqual(t['due'], self.localtime_aware)
617 def test_filter_by_aware_datetime(self):
618 t = Task(self.tw, description="task1", due=self.localtime_aware)
620 matching_tasks = self.tw.tasks.filter(due=self.localtime_aware)
621 self.assertEqual(len(matching_tasks), 1)
623 def test_serialize_aware_datetime(self):
624 t = Task(self.tw, description="task1", due=self.localtime_aware)
625 self.assertEqual(json.loads(t.export_data())['due'],
626 self.utctime_aware.strftime(DATE_FORMAT))
628 class AnnotationTest(TasklibTest):
631 super(AnnotationTest, self).setUp()
632 Task(self.tw, description="test task").save()
634 def test_adding_annotation(self):
635 task = self.tw.tasks.get()
636 task.add_annotation('test annotation')
637 self.assertEqual(len(task['annotations']), 1)
638 ann = task['annotations'][0]
639 self.assertEqual(ann['description'], 'test annotation')
641 def test_removing_annotation(self):
642 task = self.tw.tasks.get()
643 task.add_annotation('test annotation')
644 ann = task['annotations'][0]
646 self.assertEqual(len(task['annotations']), 0)
648 def test_removing_annotation_by_description(self):
649 task = self.tw.tasks.get()
650 task.add_annotation('test annotation')
651 task.remove_annotation('test annotation')
652 self.assertEqual(len(task['annotations']), 0)
654 def test_removing_annotation_by_obj(self):
655 task = self.tw.tasks.get()
656 task.add_annotation('test annotation')
657 ann = task['annotations'][0]
658 task.remove_annotation(ann)
659 self.assertEqual(len(task['annotations']), 0)
661 def test_annotation_after_modification(self):
662 task = self.tw.tasks.get()
663 task['project'] = 'test'
664 task.add_annotation('I should really do this task')
665 self.assertEqual(task['project'], 'test')
667 self.assertEqual(task['project'], 'test')
669 def test_serialize_annotations(self):
670 # Test that serializing annotations is possible
671 t = Task(self.tw, description="test")
674 t.add_annotation("annotation1")
675 t.add_annotation("annotation2")
677 data = t._serialize('annotations', t._data['annotations'])
679 self.assertEqual(len(data), 2)
680 self.assertEqual(type(data[0]), dict)
681 self.assertEqual(type(data[1]), dict)
683 self.assertEqual(data[0]['description'], "annotation1")
684 self.assertEqual(data[1]['description'], "annotation2")
687 class UnicodeTest(TasklibTest):
689 def test_unicode_task(self):
690 Task(self.tw, description="†åßk").save()
693 def test_non_unicode_task(self):
694 Task(self.tw, description="test task").save()