]> git.madduck.net Git - etc/taskwarrior.git/blob - tasklib/tests.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

76a951430a9fc48d9e26cd07fad2f81dfd4995b2
[etc/taskwarrior.git] / tasklib / tests.py
1 # coding=utf-8
2
3 import shutil
4 import tempfile
5 import unittest
6
7 from .task import TaskWarrior, Task
8
9
10 class TasklibTest(unittest.TestCase):
11
12     def setUp(self):
13         self.tmp = tempfile.mkdtemp(dir='.')
14         self.tw = TaskWarrior(data_location=self.tmp)
15
16     def tearDown(self):
17         shutil.rmtree(self.tmp)
18
19
20 class TaskFilterTest(TasklibTest):
21
22     def test_all_empty(self):
23         self.assertEqual(len(self.tw.tasks.all()), 0)
24
25     def test_all_non_empty(self):
26         Task(self.tw, description="test task").save()
27         self.assertEqual(len(self.tw.tasks.all()), 1)
28         self.assertEqual(self.tw.tasks.all()[0]['description'], 'test task')
29         self.assertEqual(self.tw.tasks.all()[0]['status'], 'pending')
30
31     def test_pending_non_empty(self):
32         Task(self.tw, description="test task").save()
33         self.assertEqual(len(self.tw.tasks.pending()), 1)
34         self.assertEqual(self.tw.tasks.pending()[0]['description'],
35                          'test task')
36         self.assertEqual(self.tw.tasks.pending()[0]['status'], 'pending')
37
38     def test_completed_empty(self):
39         Task(self.tw, description="test task").save()
40         self.assertEqual(len(self.tw.tasks.completed()), 0)
41
42     def test_completed_non_empty(self):
43         Task(self.tw, description="test task").save()
44         self.assertEqual(len(self.tw.tasks.completed()), 0)
45         self.tw.tasks.all()[0].done()
46         self.assertEqual(len(self.tw.tasks.completed()), 1)
47
48     def test_filtering_by_attribute(self):
49         Task(self.tw, description="no priority task").save()
50         Task(self.tw, priority="H", description="high priority task").save()
51         self.assertEqual(len(self.tw.tasks.all()), 2)
52
53         # Assert that the correct number of tasks is returned
54         self.assertEqual(len(self.tw.tasks.filter(priority="H")), 1)
55
56         # Assert that the correct tasks are returned
57         high_priority_task = self.tw.tasks.get(priority="H")
58         self.assertEqual(high_priority_task['description'], "high priority task")
59
60     def test_filtering_by_empty_attribute(self):
61         Task(self.tw, description="no priority task").save()
62         Task(self.tw, priority="H", description="high priority task").save()
63         self.assertEqual(len(self.tw.tasks.all()), 2)
64
65         # Assert that the correct number of tasks is returned
66         self.assertEqual(len(self.tw.tasks.filter(priority=None)), 1)
67
68         # Assert that the correct tasks are returned
69         no_priority_task = self.tw.tasks.get(priority=None)
70         self.assertEqual(no_priority_task['description'], "no priority task")
71
72
73 class TaskTest(TasklibTest):
74
75     def test_create_unsaved_task(self):
76         # Make sure a new task is not saved unless explicitly called for
77         t = Task(self.tw, description="test task")
78         self.assertEqual(len(self.tw.tasks.all()), 0)
79
80     # TODO: once python 2.6 compatiblity is over, use context managers here
81     #       and in all subsequent tests for assertRaises
82
83     def test_delete_unsaved_task(self):
84         t = Task(self.tw, description="test task")
85         self.assertRaises(Task.NotSaved, t.delete)
86
87     def test_complete_unsaved_task(self):
88         t = Task(self.tw, description="test task")
89         self.assertRaises(Task.NotSaved, t.done)
90
91     def test_refresh_unsaved_task(self):
92         t = Task(self.tw, description="test task")
93         self.assertRaises(Task.NotSaved, t.refresh)
94
95     def test_delete_deleted_task(self):
96         t = Task(self.tw, description="test task")
97         t.save()
98         t.delete()
99
100         self.assertRaises(Task.DeletedTask, t.delete)
101
102     def test_complete_completed_task(self):
103         t = Task(self.tw, description="test task")
104         t.save()
105         t.done()
106
107         self.assertRaises(Task.CompletedTask, t.done)
108
109     def test_complete_deleted_task(self):
110         t = Task(self.tw, description="test task")
111         t.save()
112         t.delete()
113
114         self.assertRaises(Task.DeletedTask, t.done)
115
116     def test_empty_dependency_set_of_unsaved_task(self):
117         t = Task(self.tw, description="test task")
118         self.assertEqual(t['depends'], set())
119
120     def test_empty_dependency_set_of_saved_task(self):
121         t = Task(self.tw, description="test task")
122         t.save()
123         self.assertEqual(t['depends'], set())
124
125     def test_set_unsaved_task_as_dependency(self):
126         # Adds only one dependency to task with no dependencies
127         t = Task(self.tw, description="test task")
128         dependency = Task(self.tw, description="needs to be done first")
129
130         # We only save the parent task, dependency task is unsaved
131         t.save()
132
133         self.assertRaises(Task.NotSaved,
134                           t.__setitem__, 'depends', set([dependency]))
135
136     def test_set_simple_dependency_set(self):
137         # Adds only one dependency to task with no dependencies
138         t = Task(self.tw, description="test task")
139         dependency = Task(self.tw, description="needs to be done first")
140
141         t.save()
142         dependency.save()
143
144         t['depends'] = set([dependency])
145
146         self.assertEqual(t['depends'], set([dependency]))
147
148     def test_set_complex_dependency_set(self):
149         # Adds two dependencies to task with no dependencies
150         t = Task(self.tw, description="test task")
151         dependency1 = Task(self.tw, description="needs to be done first")
152         dependency2 = Task(self.tw, description="needs to be done second")
153
154         t.save()
155         dependency1.save()
156         dependency2.save()
157
158         t['depends'] = set([dependency1, dependency2])
159
160         self.assertEqual(t['depends'], set([dependency1, dependency2]))
161
162     def test_remove_from_dependency_set(self):
163         # Removes dependency from task with two dependencies
164         t = Task(self.tw, description="test task")
165         dependency1 = Task(self.tw, description="needs to be done first")
166         dependency2 = Task(self.tw, description="needs to be done second")
167
168         dependency1.save()
169         dependency2.save()
170
171         t['depends'] = set([dependency1, dependency2])
172         t.save()
173
174         t['depends'] = t['depends'] - set([dependency2])
175         t.save()
176
177         self.assertEqual(t['depends'], set([dependency1]))
178
179     def test_add_to_dependency_set(self):
180         # Adds dependency to task with one dependencies
181         t = Task(self.tw, description="test task")
182         dependency1 = Task(self.tw, description="needs to be done first")
183         dependency2 = Task(self.tw, description="needs to be done second")
184
185         dependency1.save()
186         dependency2.save()
187
188         t['depends'] = set([dependency1])
189         t.save()
190
191         t['depends'] = t['depends'] | set([dependency2])
192         t.save()
193
194         self.assertEqual(t['depends'], set([dependency1, dependency2]))
195
196     def test_compare_different_tasks(self):
197         # Negative: compare two different tasks
198         t1 = Task(self.tw, description="test task")
199         t2 = Task(self.tw, description="test task")
200
201         t1.save()
202         t2.save()
203
204         self.assertEqual(t1 == t2, False)
205
206     def test_compare_same_task_object(self):
207         # Compare Task object wit itself
208         t = Task(self.tw, description="test task")
209         t.save()
210
211         self.assertEqual(t == t, True)
212
213     def test_compare_same_task(self):
214         # Compare the same task using two different objects
215         t1 = Task(self.tw, description="test task")
216         t1.save()
217
218         t2 = self.tw.tasks.get(uuid=t1['uuid'])
219         self.assertEqual(t1 == t2, True)
220
221     def test_compare_unsaved_tasks(self):
222         # t1 and t2 are unsaved tasks, considered to be unequal
223         # despite the content of data
224         t1 = Task(self.tw, description="test task")
225         t2 = Task(self.tw, description="test task")
226
227         self.assertEqual(t1 == t2, False)
228
229     def test_hash_unsaved_tasks(self):
230         # Considered equal, it's the same object
231         t1 = Task(self.tw, description="test task")
232         t2 = t1
233         self.assertEqual(hash(t1) == hash(t2), True)
234
235     def test_hash_same_task(self):
236         # Compare the hash of the task using two different objects
237         t1 = Task(self.tw, description="test task")
238         t1.save()
239
240         t2 = self.tw.tasks.get(uuid=t1['uuid'])
241         self.assertEqual(t1.__hash__(), t2.__hash__())
242
243 class AnnotationTest(TasklibTest):
244
245     def setUp(self):
246         super(AnnotationTest, self).setUp()
247         Task(self.tw, description="test task").save()
248
249     def test_adding_annotation(self):
250         task = self.tw.tasks.get()
251         task.add_annotation('test annotation')
252         self.assertEqual(len(task['annotations']), 1)
253         ann = task['annotations'][0]
254         self.assertEqual(ann['description'], 'test annotation')
255
256     def test_removing_annotation(self):
257         task = self.tw.tasks.get()
258         task.add_annotation('test annotation')
259         ann = task['annotations'][0]
260         ann.remove()
261         self.assertEqual(len(task['annotations']), 0)
262
263     def test_removing_annotation_by_description(self):
264         task = self.tw.tasks.get()
265         task.add_annotation('test annotation')
266         task.remove_annotation('test annotation')
267         self.assertEqual(len(task['annotations']), 0)
268
269     def test_removing_annotation_by_obj(self):
270         task = self.tw.tasks.get()
271         task.add_annotation('test annotation')
272         ann = task['annotations'][0]
273         task.remove_annotation(ann)
274         self.assertEqual(len(task['annotations']), 0)
275
276
277 class UnicodeTest(TasklibTest):
278
279     def test_unicode_task(self):
280         Task(self.tw, description="†åßk").save()
281         self.tw.tasks.get()
282
283     def test_non_unicode_task(self):
284         Task(self.tw, description="test task").save()
285         self.tw.tasks.get()