File: test_task.py

package info (click to toggle)
python-taskchampion-py 2.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 360 kB
  • sloc: python: 682; makefile: 15
file content (420 lines) | stat: -rw-r--r-- 11,653 bytes parent folder | download
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import re
from taskchampion import Task, Replica, Status, Tag, Operations, Annotation
from datetime import datetime
import pytest
import uuid


@pytest.fixture
def replica() -> Replica:
    return Replica.new_in_memory()


@pytest.fixture
def new_task_uuid() -> str:
    return str(uuid.uuid4())


@pytest.fixture
def new_task(replica: Replica, new_task_uuid: str) -> Task:
    ops = Operations()
    task = replica.create_task(new_task_uuid, ops)
    task.set_description("a task", ops)
    task.set_status(Status.Pending, ops)
    replica.commit_operations(ops)
    return task


@pytest.fixture
def waiting_task_uuid() -> str:
    return str(uuid.uuid4())


@pytest.fixture
def waiting_task(replica: Replica, waiting_task_uuid: str) -> Task:
    ops = Operations()
    task = replica.create_task(waiting_task_uuid, ops)
    task.set_description("a task", ops)
    task.set_status(Status.Pending, ops)
    task.set_wait(datetime.fromisoformat("2038-01-19T03:14:07+00:00"), ops)
    task.set_priority("10", ops)
    task.add_tag(Tag("example_tag"), ops)
    replica.commit_operations(ops)
    return task


@pytest.fixture
def started_task_uuid() -> str:
    return str(uuid.uuid4())


@pytest.fixture
def started_task(replica: Replica, started_task_uuid: str) -> Task:
    ops = Operations()
    task = replica.create_task(started_task_uuid, ops)
    task.set_description("a task", ops)
    task.set_status(Status.Pending, ops)
    task.start(ops)
    replica.commit_operations(ops)
    return task


@pytest.fixture
def blocked_task_uuid() -> str:
    return str(uuid.uuid4())


@pytest.fixture
def blocked_task(replica: Replica, started_task: Task, blocked_task_uuid: str) -> Task:
    "Create a task blocked on started_task"
    ops = Operations()
    task = replica.create_task(blocked_task_uuid, ops)
    task.set_description("a task", ops)
    task.set_status(Status.Pending, ops)
    task.add_dependency(started_task.get_uuid(), ops)
    replica.commit_operations(ops)
    return task


@pytest.fixture
def due_task_uuid() -> str:
    return str(uuid.uuid4())


@pytest.fixture
def due_task(replica: Replica, due_task_uuid: str) -> Task:
    ops = Operations()
    task = replica.create_task(due_task_uuid, ops)
    task.set_description("a task", ops)
    task.set_status(Status.Pending, ops)
    task.set_due(datetime.fromisoformat("2006-05-13T01:27:27+00:00"), ops)
    replica.commit_operations(ops)
    return task


@pytest.fixture
def annotated_task_uuid() -> str:
    return str(uuid.uuid4())


@pytest.fixture
def annotation_entry() -> datetime:
    return datetime.fromisoformat("2010-05-13T01:27:27+00:00")


@pytest.fixture
def annotated_task(
    replica: Replica, annotated_task_uuid: str, annotation_entry: datetime
):
    ops = Operations()
    task = replica.create_task(annotated_task_uuid, ops)
    task.set_description("a task", ops)
    task.set_status(Status.Pending, ops)
    task.add_annotation(Annotation(annotation_entry, "a thing happened"), ops)
    replica.commit_operations(ops)
    return task


@pytest.fixture
def uda_task_uuid() -> str:
    return str(uuid.uuid4())


@pytest.fixture
def uda_task(replica: Replica, uda_task_uuid: str):
    ops = Operations()
    task = replica.create_task(uda_task_uuid, ops)
    task.set_description("a task", ops)
    task.set_status(Status.Pending, ops)
    task.set_uda("ns", "key", "val", ops)
    replica.commit_operations(ops)
    return task


@pytest.fixture
def legacy_uda_task_uuid() -> str:
    return str(uuid.uuid4())


@pytest.fixture
def legacy_uda_task(replica: Replica, legacy_uda_task_uuid: str):
    ops = Operations()
    task = replica.create_task(legacy_uda_task_uuid, ops)
    task.set_description("a task", ops)
    task.set_status(Status.Pending, ops)
    task.set_legacy_uda("legacy-key", "legacy-val", ops)
    replica.commit_operations(ops)
    return task


def test_repr(new_task: Task):
    # The Rust Debug output contains lots of internal details that we do not
    # need to check for here.
    assert re.match(r"^Task { .* }$", repr(new_task))


def test_into_task_data(new_task: Task, new_task_uuid: str):
    new_task = new_task.into_task_data()
    assert new_task.get_uuid() == new_task_uuid


def test_get_uuid(new_task: Task, new_task_uuid: str):
    assert new_task.get_uuid() == new_task_uuid


def test_get_set_status(new_task: Task):
    status = new_task.get_status()
    assert status == Status.Pending


def test_get_set_description(new_task: Task):
    assert new_task.get_description() == "a task"


def test_get_set_entry(replica: Replica, new_task: Task):
    entry = datetime.fromisoformat("2038-01-19T03:14:07+00:00")
    ops = Operations()
    new_task.set_entry(entry, ops)
    replica.commit_operations(ops)
    new_task = replica.get_task(new_task.get_uuid())
    assert new_task.get_entry() == entry


def test_get_set_entry_none(replica: Replica, new_task: Task):
    ops = Operations()
    new_task.set_entry(None, ops)
    replica.commit_operations(ops)
    new_task = replica.get_task(new_task.get_uuid())
    assert new_task.get_entry() == None


def test_get_set_priority(waiting_task: Task):
    priority = waiting_task.get_priority()
    assert priority == "10"


def test_get_priority_missing(new_task: Task):
    priority = new_task.get_priority()
    assert priority == ""


def test_get_wait(waiting_task: Task):
    assert waiting_task.get_wait() == datetime.fromisoformat(
        "2038-01-19T03:14:07+00:00"
    )


def test_is_waiting(new_task: Task, waiting_task: Task):
    assert not new_task.is_waiting()
    assert waiting_task.is_waiting()


def test_is_active(new_task: Task, started_task: Task):
    assert not new_task.is_active()
    assert started_task.is_active()


def test_is_blocked(replica: Replica, new_task: Task, blocked_task: Task):
    # Re-fetch tasks to get updated dependency map.
    new_task = replica.get_task(new_task.get_uuid())
    blocked_task = replica.get_task(blocked_task.get_uuid())
    assert not new_task.is_blocked()
    assert blocked_task.is_blocked()


def test_is_blocking(replica: Replica, blocked_task: Task, started_task: Task):
    # Re-fetch tasks to get updated dependency map.
    blocked_task = replica.get_task(blocked_task.get_uuid())
    started_task = replica.get_task(started_task.get_uuid())
    assert not blocked_task.is_blocking()
    assert started_task.is_blocking()


def test_has_tag_none(new_task: Task):
    assert not new_task.has_tag(Tag("sample_tag"))


def test_has_tag_synthetic(replica: Replica, started_task: Task):
    assert started_task.has_tag(Tag("PENDING"))


def test_has_tag_user(replica: Replica, new_task: Task):
    ops = Operations()
    new_task.add_tag(Tag("foo"), ops)
    replica.commit_operations(ops)
    assert new_task.has_tag(Tag("foo"))


def test_get_tags(replica: Replica, new_task: Task):
    ops = Operations()
    new_task.add_tag(Tag("foo"), ops)
    replica.commit_operations(ops)
    tags = new_task.get_tags()
    # TaskChampion may add synthetic tags, so just assert a few we expect.
    assert Tag("foo") in tags
    assert Tag("PENDING") in tags


def test_remove_tag(replica: Replica, waiting_task: Task):
    assert Tag("example_tag") in waiting_task.get_tags()

    ops = Operations()
    waiting_task.remove_tag(Tag("example_tag"), ops)
    replica.commit_operations(ops)

    assert Tag("example_tag") not in waiting_task.get_tags()


def test_get_annotations(
    replica: Replica, annotated_task: Task, annotation_entry: datetime
):
    annotations = annotated_task.get_annotations()
    assert len(annotations) == 1
    assert annotations[0].entry == annotation_entry
    assert annotations[0].description == "a thing happened"


def test_remove_annotation(
    replica: Replica, annotated_task: Task, annotation_entry: datetime
):
    ops = Operations()
    annotated_task.remove_annotation(annotation_entry, ops)
    replica.commit_operations(ops)

    annotations = annotated_task.get_annotations()
    assert len(annotations) == 0


def test_get_udas(uda_task: Task):
    [((ns, key), val)] = uda_task.get_udas()
    assert ns == "ns"
    assert key == "key"
    assert val == "val"


def test_get_udas_legacy(legacy_uda_task: Task):
    [((ns, key), val)] = legacy_uda_task.get_udas()
    assert ns == ""
    assert key == "legacy-key"
    assert val == "legacy-val"


def test_get_udas_none(new_task: Task):
    [] = new_task.get_udas()


def test_remove_uda(replica: Replica, uda_task: Task):
    ops = Operations()
    uda_task.remove_uda("ns", "key", ops)
    replica.commit_operations(ops)
    uda_task = replica.get_task(uda_task.get_uuid())
    [] = uda_task.get_udas()


def test_remove_uda_no_such(replica: Replica, uda_task: Task):
    ops = Operations()
    uda_task.remove_uda("no", "such", ops)
    replica.commit_operations(ops)
    uda_task = replica.get_task(uda_task.get_uuid())
    assert len(uda_task.get_udas()) == 1


def test_remove_legacy_uda(replica: Replica, legacy_uda_task: Task):
    ops = Operations()
    legacy_uda_task.remove_legacy_uda("legacy-key", ops)
    replica.commit_operations(ops)
    legacy_uda_task = replica.get_task(legacy_uda_task.get_uuid())
    [] = legacy_uda_task.get_udas()


def test_get_modified(replica: Replica, new_task: Task):
    ops = Operations()
    mod = datetime.fromisoformat("2006-05-13T01:27:27+00:00")
    new_task.set_modified(mod, ops)
    replica.commit_operations(ops)
    assert new_task.get_modified() == mod


def test_get_modified_not_set(replica: Replica, new_task_uuid: Task):
    ops = Operations()
    task = replica.create_task(new_task_uuid, ops)
    replica.commit_operations(ops)
    assert task.get_modified() == None


def test_get_due(due_task: Task):
    assert due_task.get_due() == datetime.fromisoformat("2006-05-13T01:27:27+00:00")


def test_get_due_not_set(new_task: Task):
    assert new_task.get_due() == None


def test_set_due(replica: Replica, new_task: Task):
    due = datetime.fromisoformat("2006-05-13T01:27:27+00:00")
    ops = Operations()
    new_task.set_due(due, ops)
    replica.commit_operations(ops)
    assert new_task.get_due() == due


def test_set_due_none(replica: Replica, new_task: Task):
    ops = Operations()
    new_task.set_due(None, ops)
    replica.commit_operations(ops)
    assert new_task.get_due() == None


def test_get_dependencies(
    blocked_task: Task, started_task: Task, started_task_uuid: str
):
    assert started_task.get_dependencies() == []
    assert blocked_task.get_dependencies() == [started_task_uuid]


def test_remove_dependencies(
    replica: Replica,
    blocked_task: Task,
    started_task: Task,
    started_task_uuid: str,
    new_task_uuid: str,
):
    ops = Operations()
    blocked_task.remove_dependency(started_task_uuid, ops)
    blocked_task.remove_dependency(
        new_task_uuid, ops
    )  # Doesn't exist, and doesn't fail.
    replica.commit_operations(ops)
    assert blocked_task.get_dependencies() == []


def test_get_value(new_task: Task):
    assert new_task.get_value("status") == "pending"


def test_get_value_not_set(new_task: Task):
    assert new_task.get_value("nosuchthing") == None


def test_start_stop(replica: Replica, new_task: Task):
    assert not new_task.is_active()

    ops = Operations()
    new_task.start(ops)
    replica.commit_operations(ops)

    assert new_task.is_active()

    ops = Operations()
    new_task.stop(ops)
    replica.commit_operations(ops)

    assert not new_task.is_active()


def test_done(replica: Replica, new_task: Task):
    ops = Operations()
    new_task.done(ops)
    replica.commit_operations(ops)

    assert new_task.get_status() == Status.Completed