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
11 class TasklibTest(unittest.TestCase):
14 self.tmp = tempfile.mkdtemp(dir='.')
15 self.tw = TaskWarrior(data_location=self.tmp)
18 shutil.rmtree(self.tmp)
21 class TaskFilterTest(TasklibTest):
23 def test_all_empty(self):
24 self.assertEqual(len(self.tw.tasks.all()), 0)
26 def test_all_non_empty(self):
27 Task(self.tw, description="test task").save()
28 self.assertEqual(len(self.tw.tasks.all()), 1)
29 self.assertEqual(self.tw.tasks.all()[0]['description'], 'test task')
30 self.assertEqual(self.tw.tasks.all()[0]['status'], 'pending')
32 def test_pending_non_empty(self):
33 Task(self.tw, description="test task").save()
34 self.assertEqual(len(self.tw.tasks.pending()), 1)
35 self.assertEqual(self.tw.tasks.pending()[0]['description'],
37 self.assertEqual(self.tw.tasks.pending()[0]['status'], 'pending')
39 def test_completed_empty(self):
40 Task(self.tw, description="test task").save()
41 self.assertEqual(len(self.tw.tasks.completed()), 0)
43 def test_completed_non_empty(self):
44 Task(self.tw, description="test task").save()
45 self.assertEqual(len(self.tw.tasks.completed()), 0)
46 self.tw.tasks.all()[0].done()
47 self.assertEqual(len(self.tw.tasks.completed()), 1)
49 def test_filtering_by_attribute(self):
50 Task(self.tw, description="no priority task").save()
51 Task(self.tw, priority="H", description="high priority task").save()
52 self.assertEqual(len(self.tw.tasks.all()), 2)
54 # Assert that the correct number of tasks is returned
55 self.assertEqual(len(self.tw.tasks.filter(priority="H")), 1)
57 # Assert that the correct tasks are returned
58 high_priority_task = self.tw.tasks.get(priority="H")
59 self.assertEqual(high_priority_task['description'], "high priority task")
61 def test_filtering_by_empty_attribute(self):
62 Task(self.tw, description="no priority task").save()
63 Task(self.tw, priority="H", description="high priority task").save()
64 self.assertEqual(len(self.tw.tasks.all()), 2)
66 # Assert that the correct number of tasks is returned
67 self.assertEqual(len(self.tw.tasks.filter(priority=None)), 1)
69 # Assert that the correct tasks are returned
70 no_priority_task = self.tw.tasks.get(priority=None)
71 self.assertEqual(no_priority_task['description'], "no priority task")
73 def test_filter_for_task_with_space_in_descripition(self):
74 task = Task(self.tw, description="test task")
77 filtered_task = self.tw.tasks.get(description="test task")
78 self.assertEqual(filtered_task['description'], "test task")
80 def test_filter_for_task_without_space_in_descripition(self):
81 task = Task(self.tw, description="test")
84 filtered_task = self.tw.tasks.get(description="test")
85 self.assertEqual(filtered_task['description'], "test")
87 def test_filter_for_task_with_space_in_project(self):
88 task = Task(self.tw, description="test", project="random project")
91 filtered_task = self.tw.tasks.get(project="random project")
92 self.assertEqual(filtered_task['project'], "random project")
94 def test_filter_for_task_without_space_in_project(self):
95 task = Task(self.tw, description="test", project="random")
98 filtered_task = self.tw.tasks.get(project="random")
99 self.assertEqual(filtered_task['project'], "random")
102 class TaskTest(TasklibTest):
104 def test_create_unsaved_task(self):
105 # Make sure a new task is not saved unless explicitly called for
106 t = Task(self.tw, description="test task")
107 self.assertEqual(len(self.tw.tasks.all()), 0)
109 # TODO: once python 2.6 compatiblity is over, use context managers here
110 # and in all subsequent tests for assertRaises
112 def test_delete_unsaved_task(self):
113 t = Task(self.tw, description="test task")
114 self.assertRaises(Task.NotSaved, t.delete)
116 def test_complete_unsaved_task(self):
117 t = Task(self.tw, description="test task")
118 self.assertRaises(Task.NotSaved, t.done)
120 def test_refresh_unsaved_task(self):
121 t = Task(self.tw, description="test task")
122 self.assertRaises(Task.NotSaved, t.refresh)
124 def test_delete_deleted_task(self):
125 t = Task(self.tw, description="test task")
129 self.assertRaises(Task.DeletedTask, t.delete)
131 def test_complete_completed_task(self):
132 t = Task(self.tw, description="test task")
136 self.assertRaises(Task.CompletedTask, t.done)
138 def test_complete_deleted_task(self):
139 t = Task(self.tw, description="test task")
143 self.assertRaises(Task.DeletedTask, t.done)
145 def test_modify_simple_attribute_without_space(self):
146 t = Task(self.tw, description="test")
149 self.assertEquals(t['description'], "test")
151 t['description'] = "test-modified"
154 self.assertEquals(t['description'], "test-modified")
156 def test_modify_simple_attribute_with_space(self):
157 # Space can pose problems with parsing
158 t = Task(self.tw, description="test task")
161 self.assertEquals(t['description'], "test task")
163 t['description'] = "test task modified"
166 self.assertEquals(t['description'], "test task modified")
168 def test_empty_dependency_set_of_unsaved_task(self):
169 t = Task(self.tw, description="test task")
170 self.assertEqual(t['depends'], set())
172 def test_empty_dependency_set_of_saved_task(self):
173 t = Task(self.tw, description="test task")
175 self.assertEqual(t['depends'], set())
177 def test_set_unsaved_task_as_dependency(self):
178 # Adds only one dependency to task with no dependencies
179 t = Task(self.tw, description="test task")
180 dependency = Task(self.tw, description="needs to be done first")
182 # We only save the parent task, dependency task is unsaved
184 t['depends'] = set([dependency])
186 self.assertRaises(Task.NotSaved, t.save)
188 def test_set_simple_dependency_set(self):
189 # Adds only one dependency to task with no dependencies
190 t = Task(self.tw, description="test task")
191 dependency = Task(self.tw, description="needs to be done first")
196 t['depends'] = set([dependency])
198 self.assertEqual(t['depends'], set([dependency]))
200 def test_set_complex_dependency_set(self):
201 # Adds two dependencies to task with no dependencies
202 t = Task(self.tw, description="test task")
203 dependency1 = Task(self.tw, description="needs to be done first")
204 dependency2 = Task(self.tw, description="needs to be done second")
210 t['depends'] = set([dependency1, dependency2])
212 self.assertEqual(t['depends'], set([dependency1, dependency2]))
214 def test_remove_from_dependency_set(self):
215 # Removes dependency from task with two dependencies
216 t = Task(self.tw, description="test task")
217 dependency1 = Task(self.tw, description="needs to be done first")
218 dependency2 = Task(self.tw, description="needs to be done second")
223 t['depends'] = set([dependency1, dependency2])
226 t['depends'].remove(dependency2)
229 self.assertEqual(t['depends'], set([dependency1]))
231 def test_add_to_dependency_set(self):
232 # Adds dependency to task with one dependencies
233 t = Task(self.tw, description="test task")
234 dependency1 = Task(self.tw, description="needs to be done first")
235 dependency2 = Task(self.tw, description="needs to be done second")
240 t['depends'] = set([dependency1])
243 t['depends'].add(dependency2)
246 self.assertEqual(t['depends'], set([dependency1, dependency2]))
248 def test_add_to_empty_dependency_set(self):
249 # Adds dependency to task with one dependencies
250 t = Task(self.tw, description="test task")
251 dependency = Task(self.tw, description="needs to be done first")
255 t['depends'].add(dependency)
258 self.assertEqual(t['depends'], set([dependency]))
260 def test_simple_dependency_set_save_repeatedly(self):
261 # Adds only one dependency to task with no dependencies
262 t = Task(self.tw, description="test task")
263 dependency = Task(self.tw, description="needs to be done first")
266 t['depends'] = set([dependency])
269 # We taint the task, but keep depends intact
270 t['description'] = "test task modified"
273 self.assertEqual(t['depends'], set([dependency]))
275 # We taint the task, but assign the same set to the depends
276 t['depends'] = set([dependency])
277 t['description'] = "test task modified again"
280 self.assertEqual(t['depends'], set([dependency]))
282 def test_compare_different_tasks(self):
283 # Negative: compare two different tasks
284 t1 = Task(self.tw, description="test task")
285 t2 = Task(self.tw, description="test task")
290 self.assertEqual(t1 == t2, False)
292 def test_compare_same_task_object(self):
293 # Compare Task object wit itself
294 t = Task(self.tw, description="test task")
297 self.assertEqual(t == t, True)
299 def test_compare_same_task(self):
300 # Compare the same task using two different objects
301 t1 = Task(self.tw, description="test task")
304 t2 = self.tw.tasks.get(uuid=t1['uuid'])
305 self.assertEqual(t1 == t2, True)
307 def test_compare_unsaved_tasks(self):
308 # t1 and t2 are unsaved tasks, considered to be unequal
309 # despite the content of data
310 t1 = Task(self.tw, description="test task")
311 t2 = Task(self.tw, description="test task")
313 self.assertEqual(t1 == t2, False)
315 def test_hash_unsaved_tasks(self):
316 # Considered equal, it's the same object
317 t1 = Task(self.tw, description="test task")
319 self.assertEqual(hash(t1) == hash(t2), True)
321 def test_hash_same_task(self):
322 # Compare the hash of the task using two different objects
323 t1 = Task(self.tw, description="test task")
326 t2 = self.tw.tasks.get(uuid=t1['uuid'])
327 self.assertEqual(t1.__hash__(), t2.__hash__())
329 def test_adding_task_with_priority(self):
330 t = Task(self.tw, description="test task", priority="M")
333 def test_removing_priority_with_none(self):
334 t = Task(self.tw, description="test task", priority="L")
337 # Remove the priority mark
341 # Assert that priority is not there after saving
342 self.assertEqual(t['priority'], None)
344 def test_adding_task_with_due_time(self):
345 t = Task(self.tw, description="test task", due=datetime.datetime.now())
348 def test_removing_due_time_with_none(self):
349 t = Task(self.tw, description="test task", due=datetime.datetime.now())
352 # Remove the due timestamp
356 # Assert that due timestamp is no longer there
357 self.assertEqual(t['due'], None)
359 def test_modified_fields_new_task(self):
362 # This should be empty with new task
363 self.assertEqual(set(t._modified_fields), set())
366 t['description'] = "test task"
367 self.assertEqual(set(t._modified_fields), set(['description']))
369 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
370 self.assertEqual(set(t._modified_fields), set(['description', 'due']))
372 t['project'] = "test project"
373 self.assertEqual(set(t._modified_fields),
374 set(['description', 'due', 'project']))
376 # List of modified fields should clear out when saved
378 self.assertEqual(set(t._modified_fields), set())
380 # Reassigning the fields with the same values now should not produce
382 t['description'] = "test task"
383 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
384 t['project'] = "test project"
385 self.assertEqual(set(t._modified_fields), set())
387 def test_modified_fields_loaded_task(self):
391 t['description'] = "test task"
392 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
393 t['project'] = "test project"
395 dependency = Task(self.tw, description="dependency")
397 t['depends'] = set([dependency])
399 # List of modified fields should clear out when saved
401 self.assertEqual(set(t._modified_fields), set())
403 # Get the task by using a filter by UUID
404 t2 = self.tw.tasks.get(uuid=t['uuid'])
406 # Reassigning the fields with the same values now should not produce
408 t['description'] = "test task"
409 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
410 t['project'] = "test project"
411 t['depends'] = set([dependency])
412 self.assertEqual(set(t._modified_fields), set())
414 def test_setting_read_only_attrs_through_init(self):
415 # Test that we are unable to set readonly attrs through __init__
416 for readonly_key in Task.read_only_fields:
417 kwargs = {'description': 'test task', readonly_key: 'value'}
418 self.assertRaises(RuntimeError,
419 lambda: Task(self.tw, **kwargs))
421 def test_setting_read_only_attrs_through_setitem(self):
422 # Test that we are unable to set readonly attrs through __init__
423 for readonly_key in Task.read_only_fields:
424 t = Task(self.tw, description='test task')
425 self.assertRaises(RuntimeError,
426 lambda: t.__setitem__(readonly_key, 'value'))
428 def test_saving_unmodified_task(self):
429 t = Task(self.tw, description="test task")
433 def test_adding_tag_by_appending(self):
434 t = Task(self.tw, description="test task", tags=['test1'])
436 t['tags'].append('test2')
438 self.assertEqual(t['tags'], ['test1', 'test2'])
440 def test_adding_tag_by_appending_empty(self):
441 t = Task(self.tw, description="test task")
443 t['tags'].append('test')
445 self.assertEqual(t['tags'], ['test'])
448 class AnnotationTest(TasklibTest):
451 super(AnnotationTest, self).setUp()
452 Task(self.tw, description="test task").save()
454 def test_adding_annotation(self):
455 task = self.tw.tasks.get()
456 task.add_annotation('test annotation')
457 self.assertEqual(len(task['annotations']), 1)
458 ann = task['annotations'][0]
459 self.assertEqual(ann['description'], 'test annotation')
461 def test_removing_annotation(self):
462 task = self.tw.tasks.get()
463 task.add_annotation('test annotation')
464 ann = task['annotations'][0]
466 self.assertEqual(len(task['annotations']), 0)
468 def test_removing_annotation_by_description(self):
469 task = self.tw.tasks.get()
470 task.add_annotation('test annotation')
471 task.remove_annotation('test annotation')
472 self.assertEqual(len(task['annotations']), 0)
474 def test_removing_annotation_by_obj(self):
475 task = self.tw.tasks.get()
476 task.add_annotation('test annotation')
477 ann = task['annotations'][0]
478 task.remove_annotation(ann)
479 self.assertEqual(len(task['annotations']), 0)
482 class UnicodeTest(TasklibTest):
484 def test_unicode_task(self):
485 Task(self.tw, description="†åßk").save()
488 def test_non_unicode_task(self):
489 Task(self.tw, description="test task").save()