]> 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:

tests: Add basic Task tests
[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     def test_delete_unsaved_task(self):
81         with self.assertRaises(Task.NotSaved):
82             t = Task(self.tw, description="test task")
83             t.delete()
84
85     def test_complete_unsaved_task(self):
86         with self.assertRaises(Task.NotSaved):
87             t = Task(self.tw, description="test task")
88             t.done()
89
90     def test_refresh_unsaved_task(self):
91         with self.assertRaises(Task.NotSaved):
92             t = Task(self.tw, description="test task")
93             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         with self.assertRaises(Task.DeletedTask):
101             t.delete()
102
103     def test_complete_completed_task(self):
104         t = Task(self.tw, description="test task")
105         t.save()
106         t.done()
107
108         with self.assertRaises(Task.CompletedTask):
109             t.done()
110
111     def test_complete_deleted_task(self):
112         t = Task(self.tw, description="test task")
113         t.save()
114         t.delete()
115
116         with self.assertRaises(Task.DeletedTask):
117             t.done()
118
119
120 class AnnotationTest(TasklibTest):
121
122     def setUp(self):
123         super(AnnotationTest, self).setUp()
124         Task(self.tw, description="test task").save()
125
126     def test_adding_annotation(self):
127         task = self.tw.tasks.get()
128         task.add_annotation('test annotation')
129         self.assertEqual(len(task['annotations']), 1)
130         ann = task['annotations'][0]
131         self.assertEqual(ann['description'], 'test annotation')
132
133     def test_removing_annotation(self):
134         task = self.tw.tasks.get()
135         task.add_annotation('test annotation')
136         ann = task['annotations'][0]
137         ann.remove()
138         self.assertEqual(len(task['annotations']), 0)
139
140     def test_removing_annotation_by_description(self):
141         task = self.tw.tasks.get()
142         task.add_annotation('test annotation')
143         task.remove_annotation('test annotation')
144         self.assertEqual(len(task['annotations']), 0)
145
146     def test_removing_annotation_by_obj(self):
147         task = self.tw.tasks.get()
148         task.add_annotation('test annotation')
149         ann = task['annotations'][0]
150         task.remove_annotation(ann)
151         self.assertEqual(len(task['annotations']), 0)
152
153
154 class UnicodeTest(TasklibTest):
155
156     def test_unicode_task(self):
157         Task(self.tw, description="†åßk").save()
158         self.tw.tasks.get()
159
160     def test_non_unicode_task(self):
161         Task(self.tw, description="test task").save()
162         self.tw.tasks.get()