forked from foobuzz/todo
/
tests.py
184 lines (154 loc) · 5.7 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import unittest, sys
from datetime import datetime, timedelta, timezone
import todo
from todo import Task
NOW = todo.NOW
class TestDatetimeParsing(unittest.TestCase):
input_cases = {
'2016-02-03': datetime(2016, 2, 3),
'2016-02-03T19:30:04': datetime(2016, 2, 3, 19, 30, 4),
'03/02/2016': None,
'42s': NOW + timedelta(seconds=42),
'42m': NOW + timedelta(seconds=42*60),
'42h': NOW + timedelta(days=1, seconds=18*3600),
'42d': NOW + timedelta(days=42),
'42w': NOW + timedelta(days=42*7),
'42n': None,
'dkfnjdfkfd': None,
'lol42n': None,
'-42s': None,
'1888888s': NOW + timedelta(seconds=1888888),
'1.25s': None,
'20l5-01-00': None
}
remaining_cases = {
timedelta(seconds=15): '15 seconds',
timedelta(seconds=65): '65 seconds',
timedelta(seconds=126): '2 minutes',
timedelta(seconds=60*44): '44 minutes',
timedelta(seconds=60*44+18): '44 minutes',
timedelta(seconds=3600): '60 minutes',
timedelta(seconds=2*3600): '2 hours',
timedelta(seconds=3600*4+60*10): '4 hours',
timedelta(days=1): '24 hours',
timedelta(days=1, seconds=8*3600): '32 hours',
timedelta(days=1, seconds=8*3600+450): '32 hours',
timedelta(days=2): '2 days',
timedelta(days=2, seconds=3600*8): '2 days',
timedelta(days=547545): '547545 days'
}
def test_parse_user_date(self):
for u_input, expected in TestDatetimeParsing.input_cases.items():
result = todo.get_datetime(u_input)
if expected is not None:
# Converting expected manually-entered datetime into UTC
expected = datetime.utcfromtimestamp(expected.timestamp())
expected = expected.replace(tzinfo=timezone.utc)
# The conversions to and back from timestamps introduce errors
# in the range of 10-nanoseconds, but we only need a precision
# of the second
result = result.replace(microsecond=0)
expected = expected.replace(microsecond=0)
self.assertEqual(result, expected)
else:
self.assertEqual(result, None)
def test_parse_remaining(self):
for dt, expected in TestDatetimeParsing.remaining_cases.items():
result = todo.parse_remaining(dt)
self.assertEqual(result, expected)
class TestContextAnalysis(unittest.TestCase):
context_cases = {
'': [],
'hello': ['hello'],
'hello.world': ['hello', 'world']
}
# (task's context, query context) : (hidden, discreet, wide)
relevancy_cases = {
('', ''): (True, True, True),
('hello', 'hello'): (True, True, True),
('hello.world', 'hello'): (False, True, True),
('hello.world.yeah', 'hello.world'): (False, True, True),
('hello.world.yeah', 'hello'): (False, True, True),
('hello', ''): (False, True, True),
('hello.world', ''): (False, False, True),
('hello.world', 'hello.world'): (True, True, True),
('hello', 'bonjour'): (False, False, False),
('hello.bonjour', 'bonjour'): (False, False, False),
('hello.bonjour', 'hey.bonjour'): (False, False, False)
}
def test_get_namespaces(self):
for context, expected in TestContextAnalysis.context_cases.items():
result = todo.get_namespaces(context)
self.assertEqual(result, expected)
def test_relevancy_test(self):
task = Task(1, '')
for i_vis, vis in enumerate(['hidden', 'discreet', 'wide']):
task.visibility = vis
for (t_ctx, q_ctx), expected in \
TestContextAnalysis.relevancy_cases.items():
task.context = t_ctx
result = task.is_relevant_to_context(q_ctx)
self.assertEqual(result, expected[i_vis])
class TestTasksSort(unittest.TestCase):
# A list of correctly sorted tasks. The test consists in sorting them
# and checking that the sorted list is the same as the original
tasks = [
Task(0, '', created=NOW, priority=3),
Task(1, '', created=NOW-timedelta(minutes=1), priority=2),
Task(2, '', created=NOW-timedelta(minutes=2), deadline=NOW+timedelta(days=3)),
Task(3, '', created=NOW-timedelta(minutes=3), deadline=NOW+timedelta(days=4)),
Task(4, '', created=NOW-timedelta(minutes=6), context='world'),
Task(5, '', created=NOW-timedelta(minutes=7)),
Task(6, '', created=NOW-timedelta(minutes=5), context='hello')
]
contexts = {'world': {'p': 2}}
def test_task_sort(self):
tasks_cpy = sorted(TestTasksSort.tasks,
key=lambda t: t.order_infos(TestTasksSort.contexts))
#tasks_cpy.sort(key=lambda t: t.order_infos(TestTasksSort.contexts))
self.assertEqual(tasks_cpy, TestTasksSort.tasks)
class TestDispatch(unittest.TestCase):
cases = {
'': [('show', {'context': ''})],
'-c hello': [('show', {'context': 'hello'})],
'-a hello': [('add_task', {'content': 'hello'})],
'-t 1 -p 3': [('get_task_by_id', {'id_': '1'}),
('apply_mutator', {'mutator': 'priority', 'value': 3})],
'-d 1': [('get_task_by_id', {'id_': '1'}),
('set_done', {})],
'-c hello -p 2': [('apply_context_mutator', {'context': 'hello',
'mutator': 'priority', 'value': 2})]
}
todolist = todo.TodoList([Task(id_=1, content='')], {})
def test_dispatch(self):
for line, sequence in TestDispatch.cases.items():
argv = line.split()
args = todo.parse_args(argv)
calls = []
handler = get_trace_handler(calls)
sys.settrace(handler)
todo.dispatch(args, TestDispatch.todolist)
sys.settrace(None)
calls = calls[1:]
for (name, args), (e_name, e_args) in zip(sequence, calls):
self.assertEqual(name, e_name)
for arg_name, arg_value in args.items():
self.assertIn(arg_name, e_args)
self.assertEqual(arg_value, e_args[arg_name])
def get_trace_handler(list_):
def handler(frame, event, arg):
if event != 'call':
return
f_name = frame.f_code.co_name
local = frame.f_locals
list_.append((f_name, local))
return handler
if __name__ == '__main__':
calls = []
args = todo.parse_args(['-c', 'hello'])
todolist = todo.TodoList([], {})
sys.settrace(get_trace_handler(calls))
todo.dispatch(args, todolist)
sys.settrace(None)
for c in calls:
print(c)