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.
8 from .task import TaskWarrior, Task
10 # http://taskwarrior.org/docs/design/task.html , Section: The Attributes
11 TASK_STANDARD_ATTRS = (
34 class TasklibTest(unittest.TestCase):
37 self.tmp = tempfile.mkdtemp(dir='.')
38 self.tw = TaskWarrior(data_location=self.tmp)
41 shutil.rmtree(self.tmp)
44 class TaskFilterTest(TasklibTest):
46 def test_all_empty(self):
47 self.assertEqual(len(self.tw.tasks.all()), 0)
49 def test_all_non_empty(self):
50 Task(self.tw, description="test task").save()
51 self.assertEqual(len(self.tw.tasks.all()), 1)
52 self.assertEqual(self.tw.tasks.all()[0]['description'], 'test task')
53 self.assertEqual(self.tw.tasks.all()[0]['status'], 'pending')
55 def test_pending_non_empty(self):
56 Task(self.tw, description="test task").save()
57 self.assertEqual(len(self.tw.tasks.pending()), 1)
58 self.assertEqual(self.tw.tasks.pending()[0]['description'],
60 self.assertEqual(self.tw.tasks.pending()[0]['status'], 'pending')
62 def test_completed_empty(self):
63 Task(self.tw, description="test task").save()
64 self.assertEqual(len(self.tw.tasks.completed()), 0)
66 def test_completed_non_empty(self):
67 Task(self.tw, description="test task").save()
68 self.assertEqual(len(self.tw.tasks.completed()), 0)
69 self.tw.tasks.all()[0].done()
70 self.assertEqual(len(self.tw.tasks.completed()), 1)
72 def test_filtering_by_attribute(self):
73 Task(self.tw, description="no priority task").save()
74 Task(self.tw, priority="H", description="high priority task").save()
75 self.assertEqual(len(self.tw.tasks.all()), 2)
77 # Assert that the correct number of tasks is returned
78 self.assertEqual(len(self.tw.tasks.filter(priority="H")), 1)
80 # Assert that the correct tasks are returned
81 high_priority_task = self.tw.tasks.get(priority="H")
82 self.assertEqual(high_priority_task['description'], "high priority task")
84 def test_filtering_by_empty_attribute(self):
85 Task(self.tw, description="no priority task").save()
86 Task(self.tw, priority="H", description="high priority task").save()
87 self.assertEqual(len(self.tw.tasks.all()), 2)
89 # Assert that the correct number of tasks is returned
90 self.assertEqual(len(self.tw.tasks.filter(priority=None)), 1)
92 # Assert that the correct tasks are returned
93 no_priority_task = self.tw.tasks.get(priority=None)
94 self.assertEqual(no_priority_task['description'], "no priority task")
96 def test_filter_for_task_with_space_in_descripition(self):
97 task = Task(self.tw, description="test task")
100 filtered_task = self.tw.tasks.get(description="test task")
101 self.assertEqual(filtered_task['description'], "test task")
103 def test_filter_for_task_without_space_in_descripition(self):
104 task = Task(self.tw, description="test")
107 filtered_task = self.tw.tasks.get(description="test")
108 self.assertEqual(filtered_task['description'], "test")
110 def test_filter_for_task_with_space_in_project(self):
111 task = Task(self.tw, description="test", project="random project")
114 filtered_task = self.tw.tasks.get(project="random project")
115 self.assertEqual(filtered_task['project'], "random project")
117 def test_filter_for_task_without_space_in_project(self):
118 task = Task(self.tw, description="test", project="random")
121 filtered_task = self.tw.tasks.get(project="random")
122 self.assertEqual(filtered_task['project'], "random")
125 class TaskTest(TasklibTest):
127 def test_create_unsaved_task(self):
128 # Make sure a new task is not saved unless explicitly called for
129 t = Task(self.tw, description="test task")
130 self.assertEqual(len(self.tw.tasks.all()), 0)
132 # TODO: once python 2.6 compatiblity is over, use context managers here
133 # and in all subsequent tests for assertRaises
135 def test_delete_unsaved_task(self):
136 t = Task(self.tw, description="test task")
137 self.assertRaises(Task.NotSaved, t.delete)
139 def test_complete_unsaved_task(self):
140 t = Task(self.tw, description="test task")
141 self.assertRaises(Task.NotSaved, t.done)
143 def test_refresh_unsaved_task(self):
144 t = Task(self.tw, description="test task")
145 self.assertRaises(Task.NotSaved, t.refresh)
147 def test_delete_deleted_task(self):
148 t = Task(self.tw, description="test task")
152 self.assertRaises(Task.DeletedTask, t.delete)
154 def test_complete_completed_task(self):
155 t = Task(self.tw, description="test task")
159 self.assertRaises(Task.CompletedTask, t.done)
161 def test_complete_deleted_task(self):
162 t = Task(self.tw, description="test task")
166 self.assertRaises(Task.DeletedTask, t.done)
168 def test_modify_simple_attribute_without_space(self):
169 t = Task(self.tw, description="test")
172 self.assertEquals(t['description'], "test")
174 t['description'] = "test-modified"
177 self.assertEquals(t['description'], "test-modified")
179 def test_modify_simple_attribute_with_space(self):
180 # Space can pose problems with parsing
181 t = Task(self.tw, description="test task")
184 self.assertEquals(t['description'], "test task")
186 t['description'] = "test task modified"
189 self.assertEquals(t['description'], "test task modified")
191 def test_empty_dependency_set_of_unsaved_task(self):
192 t = Task(self.tw, description="test task")
193 self.assertEqual(t['depends'], set())
195 def test_empty_dependency_set_of_saved_task(self):
196 t = Task(self.tw, description="test task")
198 self.assertEqual(t['depends'], set())
200 def test_set_unsaved_task_as_dependency(self):
201 # Adds only one dependency to task with no dependencies
202 t = Task(self.tw, description="test task")
203 dependency = Task(self.tw, description="needs to be done first")
205 # We only save the parent task, dependency task is unsaved
207 t['depends'] = set([dependency])
209 self.assertRaises(Task.NotSaved, t.save)
211 def test_set_simple_dependency_set(self):
212 # Adds only one dependency to task with no dependencies
213 t = Task(self.tw, description="test task")
214 dependency = Task(self.tw, description="needs to be done first")
219 t['depends'] = set([dependency])
221 self.assertEqual(t['depends'], set([dependency]))
223 def test_set_complex_dependency_set(self):
224 # Adds two dependencies to task with no dependencies
225 t = Task(self.tw, description="test task")
226 dependency1 = Task(self.tw, description="needs to be done first")
227 dependency2 = Task(self.tw, description="needs to be done second")
233 t['depends'] = set([dependency1, dependency2])
235 self.assertEqual(t['depends'], set([dependency1, dependency2]))
237 def test_remove_from_dependency_set(self):
238 # Removes dependency from task with two dependencies
239 t = Task(self.tw, description="test task")
240 dependency1 = Task(self.tw, description="needs to be done first")
241 dependency2 = Task(self.tw, description="needs to be done second")
246 t['depends'] = set([dependency1, dependency2])
249 t['depends'].remove(dependency2)
252 self.assertEqual(t['depends'], set([dependency1]))
254 def test_add_to_dependency_set(self):
255 # Adds dependency to task with one dependencies
256 t = Task(self.tw, description="test task")
257 dependency1 = Task(self.tw, description="needs to be done first")
258 dependency2 = Task(self.tw, description="needs to be done second")
263 t['depends'] = set([dependency1])
266 t['depends'].add(dependency2)
269 self.assertEqual(t['depends'], set([dependency1, dependency2]))
271 def test_add_to_empty_dependency_set(self):
272 # Adds dependency to task with one dependencies
273 t = Task(self.tw, description="test task")
274 dependency = Task(self.tw, description="needs to be done first")
278 t['depends'].add(dependency)
281 self.assertEqual(t['depends'], set([dependency]))
283 def test_simple_dependency_set_save_repeatedly(self):
284 # Adds only one dependency to task with no dependencies
285 t = Task(self.tw, description="test task")
286 dependency = Task(self.tw, description="needs to be done first")
289 t['depends'] = set([dependency])
292 # We taint the task, but keep depends intact
293 t['description'] = "test task modified"
296 self.assertEqual(t['depends'], set([dependency]))
298 # We taint the task, but assign the same set to the depends
299 t['depends'] = set([dependency])
300 t['description'] = "test task modified again"
303 self.assertEqual(t['depends'], set([dependency]))
305 def test_compare_different_tasks(self):
306 # Negative: compare two different tasks
307 t1 = Task(self.tw, description="test task")
308 t2 = Task(self.tw, description="test task")
313 self.assertEqual(t1 == t2, False)
315 def test_compare_same_task_object(self):
316 # Compare Task object wit itself
317 t = Task(self.tw, description="test task")
320 self.assertEqual(t == t, True)
322 def test_compare_same_task(self):
323 # Compare the same task using two different objects
324 t1 = Task(self.tw, description="test task")
327 t2 = self.tw.tasks.get(uuid=t1['uuid'])
328 self.assertEqual(t1 == t2, True)
330 def test_compare_unsaved_tasks(self):
331 # t1 and t2 are unsaved tasks, considered to be unequal
332 # despite the content of data
333 t1 = Task(self.tw, description="test task")
334 t2 = Task(self.tw, description="test task")
336 self.assertEqual(t1 == t2, False)
338 def test_hash_unsaved_tasks(self):
339 # Considered equal, it's the same object
340 t1 = Task(self.tw, description="test task")
342 self.assertEqual(hash(t1) == hash(t2), True)
344 def test_hash_same_task(self):
345 # Compare the hash of the task using two different objects
346 t1 = Task(self.tw, description="test task")
349 t2 = self.tw.tasks.get(uuid=t1['uuid'])
350 self.assertEqual(t1.__hash__(), t2.__hash__())
352 def test_adding_task_with_priority(self):
353 t = Task(self.tw, description="test task", priority="M")
356 def test_removing_priority_with_none(self):
357 t = Task(self.tw, description="test task", priority="L")
360 # Remove the priority mark
364 # Assert that priority is not there after saving
365 self.assertEqual(t['priority'], None)
367 def test_adding_task_with_due_time(self):
368 t = Task(self.tw, description="test task", due=datetime.datetime.now())
371 def test_removing_due_time_with_none(self):
372 t = Task(self.tw, description="test task", due=datetime.datetime.now())
375 # Remove the due timestamp
379 # Assert that due timestamp is no longer there
380 self.assertEqual(t['due'], None)
382 def test_modified_fields_new_task(self):
385 # This should be empty with new task
386 self.assertEqual(set(t._modified_fields), set())
389 t['description'] = "test task"
390 self.assertEqual(set(t._modified_fields), set(['description']))
392 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
393 self.assertEqual(set(t._modified_fields), set(['description', 'due']))
395 t['project'] = "test project"
396 self.assertEqual(set(t._modified_fields),
397 set(['description', 'due', 'project']))
399 # List of modified fields should clear out when saved
401 self.assertEqual(set(t._modified_fields), set())
403 # Reassigning the fields with the same values now should not produce
405 t['description'] = "test task"
406 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
407 t['project'] = "test project"
408 self.assertEqual(set(t._modified_fields), set())
410 def test_modified_fields_loaded_task(self):
414 t['description'] = "test task"
415 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
416 t['project'] = "test project"
418 dependency = Task(self.tw, description="dependency")
420 t['depends'] = set([dependency])
422 # List of modified fields should clear out when saved
424 self.assertEqual(set(t._modified_fields), set())
426 # Get the task by using a filter by UUID
427 t2 = self.tw.tasks.get(uuid=t['uuid'])
429 # Reassigning the fields with the same values now should not produce
431 t['description'] = "test task"
432 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
433 t['project'] = "test project"
434 t['depends'] = set([dependency])
435 self.assertEqual(set(t._modified_fields), set())
437 def test_modified_fields_not_affected_by_reading(self):
440 for field in TASK_STANDARD_ATTRS:
443 self.assertEqual(set(t._modified_fields), set())
445 def test_setting_read_only_attrs_through_init(self):
446 # Test that we are unable to set readonly attrs through __init__
447 for readonly_key in Task.read_only_fields:
448 kwargs = {'description': 'test task', readonly_key: 'value'}
449 self.assertRaises(RuntimeError,
450 lambda: Task(self.tw, **kwargs))
452 def test_setting_read_only_attrs_through_setitem(self):
453 # Test that we are unable to set readonly attrs through __init__
454 for readonly_key in Task.read_only_fields:
455 t = Task(self.tw, description='test task')
456 self.assertRaises(RuntimeError,
457 lambda: t.__setitem__(readonly_key, 'value'))
459 def test_saving_unmodified_task(self):
460 t = Task(self.tw, description="test task")
464 def test_adding_tag_by_appending(self):
465 t = Task(self.tw, description="test task", tags=['test1'])
467 t['tags'].append('test2')
469 self.assertEqual(t['tags'], ['test1', 'test2'])
471 def test_adding_tag_by_appending_empty(self):
472 t = Task(self.tw, description="test task")
474 t['tags'].append('test')
476 self.assertEqual(t['tags'], ['test'])
479 class AnnotationTest(TasklibTest):
482 super(AnnotationTest, self).setUp()
483 Task(self.tw, description="test task").save()
485 def test_adding_annotation(self):
486 task = self.tw.tasks.get()
487 task.add_annotation('test annotation')
488 self.assertEqual(len(task['annotations']), 1)
489 ann = task['annotations'][0]
490 self.assertEqual(ann['description'], 'test annotation')
492 def test_removing_annotation(self):
493 task = self.tw.tasks.get()
494 task.add_annotation('test annotation')
495 ann = task['annotations'][0]
497 self.assertEqual(len(task['annotations']), 0)
499 def test_removing_annotation_by_description(self):
500 task = self.tw.tasks.get()
501 task.add_annotation('test annotation')
502 task.remove_annotation('test annotation')
503 self.assertEqual(len(task['annotations']), 0)
505 def test_removing_annotation_by_obj(self):
506 task = self.tw.tasks.get()
507 task.add_annotation('test annotation')
508 ann = task['annotations'][0]
509 task.remove_annotation(ann)
510 self.assertEqual(len(task['annotations']), 0)
512 def test_annotation_after_modification(self):
513 task = self.tw.tasks.get()
514 task['project'] = 'test'
515 task.add_annotation('I should really do this task')
516 self.assertEqual(task['project'], 'test')
518 self.assertEqual(task['project'], 'test')
521 class UnicodeTest(TasklibTest):
523 def test_unicode_task(self):
524 Task(self.tw, description="†åßk").save()
527 def test_non_unicode_task(self):
528 Task(self.tw, description="test task").save()