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_simple_dependency_set_save_repeatedly(self):
249 # Adds only one dependency to task with no dependencies
250 t = Task(self.tw, description="test task")
251 dependency = Task(self.tw, description="needs to be done first")
254 t['depends'] = set([dependency])
257 # We taint the task, but keep depends intact
258 t['description'] = "test task modified"
261 self.assertEqual(t['depends'], set([dependency]))
263 # We taint the task, but assign the same set to the depends
264 t['depends'] = set([dependency])
265 t['description'] = "test task modified again"
268 self.assertEqual(t['depends'], set([dependency]))
270 def test_compare_different_tasks(self):
271 # Negative: compare two different tasks
272 t1 = Task(self.tw, description="test task")
273 t2 = Task(self.tw, description="test task")
278 self.assertEqual(t1 == t2, False)
280 def test_compare_same_task_object(self):
281 # Compare Task object wit itself
282 t = Task(self.tw, description="test task")
285 self.assertEqual(t == t, True)
287 def test_compare_same_task(self):
288 # Compare the same task using two different objects
289 t1 = Task(self.tw, description="test task")
292 t2 = self.tw.tasks.get(uuid=t1['uuid'])
293 self.assertEqual(t1 == t2, True)
295 def test_compare_unsaved_tasks(self):
296 # t1 and t2 are unsaved tasks, considered to be unequal
297 # despite the content of data
298 t1 = Task(self.tw, description="test task")
299 t2 = Task(self.tw, description="test task")
301 self.assertEqual(t1 == t2, False)
303 def test_hash_unsaved_tasks(self):
304 # Considered equal, it's the same object
305 t1 = Task(self.tw, description="test task")
307 self.assertEqual(hash(t1) == hash(t2), True)
309 def test_hash_same_task(self):
310 # Compare the hash of the task using two different objects
311 t1 = Task(self.tw, description="test task")
314 t2 = self.tw.tasks.get(uuid=t1['uuid'])
315 self.assertEqual(t1.__hash__(), t2.__hash__())
317 def test_adding_task_with_priority(self):
318 t = Task(self.tw, description="test task", priority="M")
321 def test_removing_priority_with_none(self):
322 t = Task(self.tw, description="test task", priority="L")
325 # Remove the priority mark
329 # Assert that priority is not there after saving
330 self.assertEqual(t['priority'], None)
332 def test_adding_task_with_due_time(self):
333 t = Task(self.tw, description="test task", due=datetime.datetime.now())
336 def test_removing_due_time_with_none(self):
337 t = Task(self.tw, description="test task", due=datetime.datetime.now())
340 # Remove the due timestamp
344 # Assert that due timestamp is no longer there
345 self.assertEqual(t['due'], None)
347 def test_modified_fields_new_task(self):
350 # This should be empty with new task
351 self.assertEqual(set(t._modified_fields), set())
354 t['description'] = "test task"
355 self.assertEqual(set(t._modified_fields), set(['description']))
357 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
358 self.assertEqual(set(t._modified_fields), set(['description', 'due']))
360 t['project'] = "test project"
361 self.assertEqual(set(t._modified_fields),
362 set(['description', 'due', 'project']))
364 # List of modified fields should clear out when saved
366 self.assertEqual(set(t._modified_fields), set())
368 # Reassigning the fields with the same values now should not produce
370 t['description'] = "test task"
371 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
372 t['project'] = "test project"
373 self.assertEqual(set(t._modified_fields), set())
375 def test_modified_fields_loaded_task(self):
379 t['description'] = "test task"
380 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
381 t['project'] = "test project"
383 dependency = Task(self.tw, description="dependency")
385 t['depends'] = set([dependency])
387 # List of modified fields should clear out when saved
389 self.assertEqual(set(t._modified_fields), set())
391 # Get the task by using a filter by UUID
392 t2 = self.tw.tasks.get(uuid=t['uuid'])
394 # Reassigning the fields with the same values now should not produce
396 t['description'] = "test task"
397 t['due'] = datetime.datetime(2014, 2, 14, 14, 14, 14) # <3
398 t['project'] = "test project"
399 t['depends'] = set([dependency])
400 self.assertEqual(set(t._modified_fields), set())
402 def test_setting_read_only_attrs_through_init(self):
403 # Test that we are unable to set readonly attrs through __init__
404 for readonly_key in Task.read_only_fields:
405 kwargs = {'description': 'test task', readonly_key: 'value'}
406 self.assertRaises(RuntimeError,
407 lambda: Task(self.tw, **kwargs))
409 def test_setting_read_only_attrs_through_setitem(self):
410 # Test that we are unable to set readonly attrs through __init__
411 for readonly_key in Task.read_only_fields:
412 t = Task(self.tw, description='test task')
413 self.assertRaises(RuntimeError,
414 lambda: t.__setitem__(readonly_key, 'value'))
416 def test_saving_unmodified_task(self):
417 t = Task(self.tw, description="test task")
422 def test_adding_tag_by_appending(self):
423 t = Task(self.tw, description="test task", tags=['test1'])
425 t['tags'].append('test2')
428 self.assertEqual(t['tags'], ['test1', 'test2'])
431 class AnnotationTest(TasklibTest):
434 super(AnnotationTest, self).setUp()
435 Task(self.tw, description="test task").save()
437 def test_adding_annotation(self):
438 task = self.tw.tasks.get()
439 task.add_annotation('test annotation')
440 self.assertEqual(len(task['annotations']), 1)
441 ann = task['annotations'][0]
442 self.assertEqual(ann['description'], 'test annotation')
444 def test_removing_annotation(self):
445 task = self.tw.tasks.get()
446 task.add_annotation('test annotation')
447 ann = task['annotations'][0]
449 self.assertEqual(len(task['annotations']), 0)
451 def test_removing_annotation_by_description(self):
452 task = self.tw.tasks.get()
453 task.add_annotation('test annotation')
454 task.remove_annotation('test annotation')
455 self.assertEqual(len(task['annotations']), 0)
457 def test_removing_annotation_by_obj(self):
458 task = self.tw.tasks.get()
459 task.add_annotation('test annotation')
460 ann = task['annotations'][0]
461 task.remove_annotation(ann)
462 self.assertEqual(len(task['annotations']), 0)
465 class UnicodeTest(TasklibTest):
467 def test_unicode_task(self):
468 Task(self.tw, description="†åßk").save()
471 def test_non_unicode_task(self):
472 Task(self.tw, description="test task").save()